DecoratorDemoPanel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:17k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DecoratorDemoPanel.java,v 1.22 2005/09/06 09:29:12 kleopatra Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.demo.swingx;
  8. import java.awt.Color;
  9. import java.beans.IntrospectionException;
  10. import java.beans.PropertyChangeEvent;
  11. import java.beans.PropertyChangeListener;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. import java.util.regex.Pattern;
  16. import javax.swing.DefaultComboBoxModel;
  17. import javax.swing.JButton;
  18. import javax.swing.JComponent;
  19. import javax.swing.JLabel;
  20. import javax.swing.JScrollPane;
  21. import javax.swing.JTextField;
  22. import javax.swing.ListModel;
  23. import javax.swing.table.TableModel;
  24. import javax.swing.tree.TreeModel;
  25. import org.jdesktop.binding.JavaBeanDataModel;
  26. import org.jdesktop.binding.metadata.EnumeratedMetaData;
  27. import org.jdesktop.binding.metadata.MetaData;
  28. import org.jdesktop.binding.swingx.BindingFactory;
  29. import org.jdesktop.binding.swingx.BindingHandler;
  30. import org.jdesktop.demo.DemoPanel;
  31. import org.jdesktop.demo.swingx.common.ActionMapTableModel;
  32. import org.jdesktop.demo.swingx.common.ActionMapTreeTableModel;
  33. import org.jdesktop.demo.swingx.common.MarginHighlighter;
  34. import org.jdesktop.swingx.JXList;
  35. import org.jdesktop.swingx.JXPanel;
  36. import org.jdesktop.swingx.JXRadioGroup;
  37. import org.jdesktop.swingx.JXTable;
  38. import org.jdesktop.swingx.JXTree;
  39. import org.jdesktop.swingx.PatternModel;
  40. import org.jdesktop.swingx.action.BoundAction;
  41. import org.jdesktop.swingx.decorator.Filter;
  42. import org.jdesktop.swingx.decorator.FilterPipeline;
  43. import org.jdesktop.swingx.decorator.Highlighter;
  44. import org.jdesktop.swingx.decorator.HighlighterPipeline;
  45. import org.jdesktop.swingx.decorator.PatternFilter;
  46. import org.jdesktop.swingx.decorator.RolloverHighlighter;
  47. import org.jdesktop.swingx.decorator.SearchHighlighter;
  48. import com.jgoodies.forms.builder.PanelBuilder;
  49. import com.jgoodies.forms.factories.Borders;
  50. import com.jgoodies.forms.layout.CellConstraints;
  51. import com.jgoodies.forms.layout.ConstantSize;
  52. import com.jgoodies.forms.layout.FormLayout;
  53. import com.jgoodies.forms.util.LayoutStyle;
  54. /**
  55.  * Demonstrates usage of Decorators.
  56.  * 
  57.  * @author Jeanette Winzenburg
  58.  */
  59. public class DecoratorDemoPanel extends DemoPanel {
  60.     // ----------------- demo components
  61.     private JXTree tree;
  62.     private JXList list;
  63.     private JXTable table;
  64.     // ----------------- demo decorators
  65.     /** shared highlighter for rollover effect. */
  66.     private Highlighter rolloverHighlighter;
  67.     /** shared highlighter for matches. */
  68.     private SearchHighlighter searchHighlighter;
  69.     
  70.     /** hacking highlighter for list's border adjustment. */
  71.     private Highlighter marginHighlighter;
  72.     /** Filter for matching entries used in list. */
  73.     private PatternFilter listPatternFilter;
  74.     private FilterPipeline listFilterPipeline;
  75.     /** Filter for matching entries used in table. */
  76.     private PatternFilter tablePatternFilter;
  77.     private FilterPipeline tableFilterPipeline;
  78.     
  79.     // ---------------- controlling components/models
  80.     private JXRadioGroup radioGroup;
  81.     private JTextField inputText;
  82.     private JButton nextButton;
  83.     private JTextField patternText;
  84.     private PatternModel patternModel;
  85.     private String[] findModi = new String[] { "highlight", "filter", "search" };
  86.     private String findModus = findModi[0];
  87.     private BoundAction findNextAction;
  88.     private JLabel radioGroupLabel;
  89.     private JLabel inputTextLabel;
  90.     private JLabel patternLabel;
  91.     /** Creates new form DecoratorDemoPanel */
  92.     public DecoratorDemoPanel() {
  93.         setName("Decorator Demo");
  94.         createDecorators();
  95.         initComponents();
  96.         configureComponents();
  97.         build();
  98.         bind();
  99.     }
  100.     /**
  101.      * called after pattern changed.
  102.      * 
  103.      * @param pattern
  104.      */
  105.     protected void updatePattern(Pattern pattern) {
  106.         searchHighlighter.setPattern(pattern);
  107.         tablePatternFilter.setPattern(pattern);
  108.         listPatternFilter.setPattern(pattern);
  109.    //     repaint();
  110.     }
  111.     /**
  112.      * called after find modus changed.
  113.      * 
  114.      */
  115.     private void updateDecorators() {
  116.         boolean highlightMatches = findModi[0].equals(getFindModus());
  117.         // update the highlighter pipelines
  118.         updatePipeline(table.getHighlighters(), highlightMatches);
  119.         updatePipeline(list.getHighlighters(), highlightMatches);
  120.         
  121.         // supported in highlight mode only
  122. //        list.setEnabled(highlightMatches);
  123.         tree.setEnabled(highlightMatches);
  124.         // filter/search support is implemented for JXList 
  125.         FilterPipeline listFilters = findModi[1].equals(getFindModus()) ? 
  126.                 getFilterPipeline("list"): null;
  127.         list.setFilters(listFilters);
  128.         // filter/search support is implemented for JXTable only
  129.         FilterPipeline filters = findModi[1].equals(getFindModus()) ? 
  130.                 getFilterPipeline("table"): null;
  131.         table.setFilters(filters);
  132.         findNextAction.setEnabled(findModi[2].equals(getFindModus()));
  133.        // repaint();
  134.     }
  135.     private void updatePipeline(HighlighterPipeline highlighters, boolean highlightMatches) {
  136.         if (highlightMatches) {
  137.             highlighters.addHighlighter(rolloverHighlighter, true);
  138.             highlighters.addHighlighter(searchHighlighter, true);
  139.         } else {
  140.             highlighters.removeHighlighter(searchHighlighter);
  141.             highlighters.removeHighlighter(rolloverHighlighter);
  142.         }
  143.         
  144.     }
  145. //--------------------------- init decorators
  146.     
  147.     private void createDecorators() {
  148.         createHighlighters();
  149.         createFilters();
  150.         findNextAction = new BoundAction("Find Next");
  151.     }
  152. //--------------------------- Highlighters    
  153.     /** 
  154.      * create all highlighters.
  155.      * 
  156.      *
  157.      */
  158.     private void createHighlighters() {
  159.         // rollover effect - blue foreground
  160.         rolloverHighlighter = new RolloverHighlighter(null, Color.BLUE);
  161.         // highlight matching effect - magenta foreground
  162.         searchHighlighter = new SearchHighlighter(null, Color.MAGENTA);
  163.         // highlight matches in all columns
  164.         searchHighlighter.setHighlightAll();
  165.         // hacking a right/left margin
  166.         marginHighlighter = new MarginHighlighter(marginBorder);
  167.         
  168.     }
  169. //----------------------- Filters
  170.     
  171.     private void createFilters() {
  172.         // filtering on first column
  173.         // JW: need option to filter all columns?
  174.         tablePatternFilter = new PatternFilter(null, 0, 0);
  175.         listPatternFilter = new PatternFilter(null, 0, 0);
  176.     }
  177.     private FilterPipeline getFilterPipeline(String viewKey) {
  178.         if ("table".equals(viewKey)) {
  179.             // JW: we have to store the filter pipeline
  180.             // because filters can be bound once only
  181.             if (tableFilterPipeline == null) {
  182.                 tableFilterPipeline = new FilterPipeline(
  183.                         new Filter[] { tablePatternFilter });
  184.             }
  185.             return tableFilterPipeline;
  186.         } else if ("list".equals(viewKey)) {
  187.             if (listFilterPipeline == null) {
  188.                 listFilterPipeline = new FilterPipeline( new Filter[] { listPatternFilter });
  189.             }
  190.             return listFilterPipeline;
  191.         }
  192.         return null;
  193.     }
  194.     // ----------------------- initial configure components
  195.     private void configureComponents() {
  196.         // show column control
  197.         table.setColumnControlVisible(true);
  198.         
  199.         // enable rollover in collection views
  200.         table.setRolloverEnabled(true);
  201.         list.setRolloverEnabled(true);
  202.         tree.setRolloverEnabled(true);
  203.         
  204.         // share highlighterPipeline in table/tree
  205.         table.setHighlighters(new HighlighterPipeline());
  206.         tree.setHighlighters(table.getHighlighters());
  207.         // use different pipeline for list 
  208.         list.setHighlighters(new HighlighterPipeline(new Highlighter[] { marginHighlighter}));
  209.         // enable list filtering
  210.         list.setFilterEnabled(true);
  211.         // initial update of decorations for collection views
  212.         updateDecorators();
  213.     }
  214.     // ------------------------ control find modus
  215.     /**
  216.      * PRE: getFindModi() contains value.
  217.      */
  218.     public void setFindModus(String value) {
  219.         if (value.equals(getFindModus()))
  220.             return;
  221.         Object old = getFindModus();
  222.         this.findModus = value;
  223.         updateDecorators();
  224.         firePropertyChange("findModus", old, getFindModus());
  225.     }
  226.     /**
  227.      * returns the current find modus.
  228.      * 
  229.      * @return
  230.      */
  231.     public String getFindModus() {
  232.         return findModus;
  233.     }
  234.     /**
  235.      * returns array of available find modi.
  236.      * 
  237.      * @return
  238.      */
  239.     public String[] getFindModi() {
  240.         // @todo: return copy!
  241.         return findModi;
  242.     }
  243.     /**
  244.      * callback for findNext action.
  245.      * 
  246.      */
  247.     public void findNext() {
  248.         int foundIndex = table.getSearchable().search(patternModel.getPattern(), patternModel
  249.                 .getFoundIndex());
  250.         patternModel.setFoundIndex(foundIndex);
  251.     }
  252.     // ----------------- create control bindings
  253.     private void bind() {
  254.         BindingHandler bindings = new BindingHandler();
  255.         bindings.setAutoCommit(true);
  256.         bindModusControl(bindings);
  257.         bindPatternModel(bindings);
  258.         bindActions(bindings);
  259.         bindings.pull();
  260.     }
  261.     private void bindActions(BindingHandler bindings) {
  262.         findNextAction.registerCallback(this, "findNext");
  263.         nextButton.setAction(findNextAction);
  264.     }
  265.     private void bindModusControl(BindingHandler bindings) {
  266.         // Enumeration not working correctly with classes
  267.         // different from string (?)
  268.         JavaBeanDataModel dataModel = null;
  269.         try {
  270.             EnumeratedMetaData meta = new EnumeratedMetaData("findModus",
  271.                     String.class, "Find Modus");
  272.             meta.setEnumeration(getFindModi());
  273.             dataModel = new JavaBeanDataModel(getClass(), this,
  274.                     new MetaData[] { meta });
  275.         } catch (IntrospectionException e) {
  276.             // TODO Auto-generated catch block
  277.             e.printStackTrace();
  278.         }
  279.         bindings.add(BindingFactory.getInstance().createBinding(
  280.                 radioGroup, dataModel, "findModus"));
  281.         bindings.add(BindingFactory.getInstance().createMetaBinding(
  282.                 radioGroupLabel, dataModel, "findModus"));
  283.     }
  284.     private void bindPatternModel(BindingHandler bindings) {
  285.         patternModel = new PatternModel();
  286.         // init DataModel
  287.         JavaBeanDataModel dataModel = null;
  288.         try {
  289.             dataModel = new JavaBeanDataModel(patternModel);
  290.             dataModel.getMetaData("pattern").setReadOnly(true);
  291.             dataModel.getMetaData("pattern").setLabel("Pattern");
  292.             dataModel.getMetaData("rawText").setLabel("Input Text");
  293.         } catch (IntrospectionException e) {
  294.             // TODO Auto-generated catch block
  295.             e.printStackTrace();
  296.         }
  297.         // ------------------------ create control bindings
  298.         bindings.add(BindingFactory.getInstance().createBinding(
  299.                 inputText, dataModel, "rawText"));
  300.         bindings.add(BindingFactory.getInstance().createMetaBinding(
  301.                 inputTextLabel, dataModel, "rawText"));
  302.         bindings.add(BindingFactory.getInstance().createBinding(
  303.                 patternText, dataModel, "pattern"));
  304.         bindings.add(BindingFactory.getInstance().createMetaBinding(
  305.                 patternLabel, dataModel, "pattern"));
  306.         // ------- wire control value changes to presentation changes
  307.         patternModel.addPropertyChangeListener(new PropertyChangeListener() {
  308.             public void propertyChange(PropertyChangeEvent evt) {
  309.                 if ("pattern".equals(evt.getPropertyName())) {
  310.                     updatePattern((Pattern) evt.getNewValue());
  311.                 }
  312.             }
  313.         });
  314.     }
  315.     // ----------------------- create models
  316.     private TableModel createTableModel() {
  317.         return new ActionMapTableModel(new JXTable().getActionMap());
  318.     }
  319.     
  320.     private ListModel createListModel() {
  321.         JXList list = new JXList();
  322.         return new DefaultComboBoxModel(list.getActionMap().allKeys());
  323.     }
  324.     private TreeModel createTreeModel() {
  325.         // return new FileSystemModel();
  326.         return new ActionMapTreeTableModel(new JXTree());
  327.     }
  328.     // ---------------------- init UI
  329.     /**
  330.      * This method is called from within the constructor to initialize the form.
  331.      */
  332.     private void initComponents() {
  333.         table = new JXTable(createTableModel());
  334.         // table = new JXTreeTable(new ActionMapTreeTableModel(new
  335.         // JXTreeTable()));
  336.         list = new JXList(createListModel());
  337.         tree = new JXTree(createTreeModel());
  338.         // control components
  339.         nextButton = new JButton("Find next");
  340.         ConstantSize size = LayoutStyle.getCurrent().getRelatedComponentsPadX();
  341.         radioGroup = new JXRadioGroup(size.getPixelSize(nextButton));
  342.         inputText = new JTextField();
  343.         patternText = new JTextField();
  344.     }
  345.     private void build() {
  346.         // COLUMN SPECS:
  347.         // f:p:g, l:4dluX:n, f:d:g, l:4dluX:n, f:d:g
  348.         // ROW SPECS:
  349.         // c:d:n, t:3dluY:n, f:d:g, t:4dluY:n, c:d:n
  350.         //
  351.         // COLUMN GROUPS: {}
  352.         // ROW GROUPS: {}
  353.         //
  354.         // COMPONENT CONSTRAINTS
  355.         // ( 1, 1, 1, 1, "d=f, d=c"); javax.swing.JLabel "table";
  356.         // name=tableLabel
  357.         // ( 3, 1, 1, 1, "d=f, d=c"); javax.swing.JLabel "list"; name=listLabel
  358.         // ( 5, 1, 1, 1, "d=f, d=c"); javax.swing.JLabel "tree"; name=treeLabel
  359.         // ( 1, 3, 1, 1, "d=f, d=f"); javax.swing.JScrollPane; name=table
  360.         // ( 3, 3, 1, 1, "d=f, d=f"); javax.swing.JScrollPane; name=list
  361.         // ( 5, 3, 1, 1, "d=f, d=f"); javax.swing.JScrollPane; name=tree
  362.         // ( 1, 5, 5, 1, "d=f, d=c"); javax.swing.JPanel; name=decoratorcontrol2
  363.         //
  364.         FormLayout formLayout = new FormLayout(
  365.                 "f:p:g, l:4dlu:n, f:d:g, l:4dlu:n, f:d:g", // columns
  366.                 "c:d:n,  t:3dlu:n, f:d:g, t:4dlu:n, c:d:n"); // rows
  367.         PanelBuilder builder = new PanelBuilder(this, formLayout);
  368.         builder.setDefaultDialogBorder();
  369.         CellConstraints cc = new CellConstraints();
  370.         CellConstraints cl = new CellConstraints();
  371.         // builder.add(new JLabel("JXTable:"), cl.xywh(1, 1, 1, 1));
  372.         builder.addLabel("JXTable:", cl.xywh(1, 1, 1, 1),
  373.                 new JScrollPane(table), cc.xywh(1, 3, 1, 1));
  374.         builder.addLabel("JXList:", cl.xywh(3, 1, 1, 1), new JScrollPane(list),
  375.                 cc.xywh(3, 3, 1, 1));
  376.         builder.addLabel("JXTree:", cl.xywh(5, 1, 1, 1), new JScrollPane(tree),
  377.                 cc.xywh(5, 3, 1, 1));
  378.         builder.add(buildControl(), cc.xywh(1, 5, 5, 1));
  379.         list.setBorder(listBorder);
  380.         tree.setBorder(treeBorder);
  381.     }
  382.     private JComponent buildControl() {
  383.         // COLUMN SPECS:
  384.         // r:p:n, l:4dluX:n, f:max(p;100dluX):n, l:4dluX:n, f:max(p;50dluX):n
  385.         // ROW SPECS:
  386.         // c:d:n, t:4dluY:n, c:d:n
  387.         //
  388.         // COLUMN GROUPS: {}
  389.         // ROW GROUPS: {}
  390.         //
  391.         // COMPONENT CONSTRAINTS
  392.         // ( 1, 1, 1, 1, "d=r, d=c"); javax.swing.JLabel "Modus"; name=modus
  393.         // ( 3, 1, 3, 1, "d=f, d=c"); javax.swing.JPanel; name=moduscontrol
  394.         // ( 1, 3, 1, 1, "d=r, d=c"); javax.swing.JLabel "Input"; name=input
  395.         // ( 3, 3, 1, 1, "d=f, d=c"); javax.swing.JTextField; name=searchText
  396.         // ( 5, 3, 1, 1, "d=f, d=c"); de.kleopatra.view.JButton; name=next
  397.         //
  398.         FormLayout formLayout = new FormLayout(
  399.                 "r:p:n, l:4dlu:n, f:max(p;100dlu):n, l:4dlu:n, f:max(p;50dlu):n", // columns
  400.                 "c:d:n, t:4dlu:n, c:d:n, t:4dlu:n, c:d:n"); // rows
  401.         JXPanel control = new JXPanel();
  402.         PanelBuilder builder = new PanelBuilder(control, formLayout);
  403.         builder.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
  404.         CellConstraints cl = new CellConstraints();
  405.         CellConstraints cc = new CellConstraints();
  406.         radioGroupLabel = builder.addLabel("", cl.xywh(1, 1, 1, 1), radioGroup, cc.xywh(3,
  407.                 1, 3, 1));
  408.         inputTextLabel = builder.addLabel("", cl.xywh(1, 3, 1, 1), inputText, cc
  409.                 .xywh(3, 3, 1, 1));
  410.         patternLabel = builder.addLabel("", cl.xywh(1, 5, 1, 1), patternText, cc.xywh(
  411.                 3, 5, 1, 1));
  412.         builder.add(nextButton, cc.xywh(5, 3, 1, 1));
  413.         return control;
  414.     }
  415.     // --------------- super overrides
  416.     public String getName() {
  417.         return "Decorators";
  418.     }
  419.     public String getInformationTitle() {
  420.         return "Highlighters :: Filters";
  421.     }
  422.     
  423.     public String getHtmlDescription() {
  424.         return "<html> "
  425.                 + " Demonstrates the use Decorators (Highlighters, Filters)"
  426.                 + " and searching "
  427.                 + " in collection components JXTable, JXList, JXTree. "
  428.                 + " "
  429.                 + "<p> <b>Find matching text </b> - this demo supports three find modes: "
  430.                 + " <ul>"
  431.                 + " <li> highlight all matching occurrences  "
  432.                 + " <li> filter away all non-matching rows (JXTable only - match is tested on first "
  433.                 + " column)"
  434.                 + " <li> search and select next occurrence (JXTable only) "
  435.                 + " </ul>"
  436.                 + "<p> <b>Mouse rollover</b> - this demo highlights the foreground in the row below "
  437.                 + " the current mouse position if the find mode is set to highlight. "
  438.                 + " </html>";
  439.     }
  440. }