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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXTableDemoPanel.java,v 1.17 2005/06/29 09:54:50 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.Dimension;
  9. import java.awt.Point;
  10. import java.beans.IntrospectionException;
  11. import java.util.ArrayList;
  12. import java.util.Collections;
  13. import java.util.Comparator;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. import javax.swing.BorderFactory;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JComponent;
  19. import javax.swing.JLabel;
  20. import javax.swing.JScrollPane;
  21. import javax.swing.SwingUtilities;
  22. import javax.swing.table.DefaultTableCellRenderer;
  23. import javax.swing.table.TableCellRenderer;
  24. import org.jdesktop.binding.JavaBeanDataModel;
  25. import org.jdesktop.binding.metadata.EnumeratedMetaData;
  26. import org.jdesktop.binding.metadata.MetaData;
  27. import org.jdesktop.binding.swingx.BindingFactory;
  28. import org.jdesktop.binding.swingx.BindingHandler;
  29. import org.jdesktop.demo.DemoPanel;
  30. import org.jdesktop.demo.swingx.common.ComponentTableModel;
  31. import org.jdesktop.demo.swingx.common.ComponentTreeTableModel;
  32. import org.jdesktop.demo.swingx.common.XYComparator;
  33. import org.jdesktop.swingx.JXPanel;
  34. import org.jdesktop.swingx.JXRadioGroup;
  35. import org.jdesktop.swingx.JXTable;
  36. import org.jdesktop.swingx.JXTreeTable;
  37. import org.jdesktop.swingx.decorator.AlternateRowHighlighter;
  38. import org.jdesktop.swingx.decorator.Highlighter;
  39. import org.jdesktop.swingx.decorator.HighlighterPipeline;
  40. import com.jgoodies.forms.builder.PanelBuilder;
  41. import com.jgoodies.forms.factories.Borders;
  42. import com.jgoodies.forms.layout.CellConstraints;
  43. import com.jgoodies.forms.layout.FormLayout;
  44. /**
  45.  * Demonstrates the JXTable/TreeTable components
  46.  *
  47.  * @author  Jeanette Winzenburg
  48.  */
  49. public class JXTableDemoPanel extends DemoPanel {
  50.     
  51.     // ----------------- demo components
  52.     private JXTable table;
  53.     private JXTreeTable treeTable;
  54.     // ---------------- demo models
  55.     private ComponentTreeTableModel treeTableModel;
  56.     private ComponentTableModel tableModel;
  57.     
  58.     
  59.     // ----------------- demo decorators
  60.     private Highlighter currentHighlighter;
  61.     private String currentHighlighterKey;
  62.     private String currentComparatorKey;
  63.     // ---------------- controlling components/models
  64.     private JComboBox highlighterCombo;
  65.     private Map highlighterMap;
  66.     private ArrayList highlighterKeys;
  67.     private JLabel highlighterLabel;
  68.     private JXRadioGroup sorterGroup;
  69.     private JLabel sorterGroupLabel;
  70.     private HashMap comparatorMap;
  71.     private ArrayList comparatorKeys;
  72.     /** Creates new form JXTableDemoPanel */
  73.     public JXTableDemoPanel() {
  74.         setName("JXTable Demo");
  75.         initDemoModels();
  76.         initDecorators();
  77.         initComponents();
  78.         configureComponents();
  79.         build();
  80.         bind();
  81.     }
  82.     private void bind() {
  83.         // setting tree/table models manually
  84.         treeTable.setTreeTableModel(treeTableModel);
  85.         table.setModel(tableModel);
  86.         // use binding for control panel interaction
  87.         BindingHandler bindings = new BindingHandler();
  88.         bindings.setAutoCommit(true);
  89.         bindHighlighterControl(bindings);
  90.         bindings.pull();
  91.     }
  92.     private void bindHighlighterControl(BindingHandler bindings) {
  93.         
  94.         // Enumeration not working correctly with classes
  95.         // different from string (?)
  96.         JavaBeanDataModel dataModel = null;
  97.         try {
  98.             EnumeratedMetaData highlighterMeta = new EnumeratedMetaData("highlighterKey",
  99.                     String.class, "Highlighter");
  100.             highlighterMeta.setEnumeration(getHighlighterKeys());
  101.             EnumeratedMetaData comparatorMeta = new EnumeratedMetaData("comparatorKey",
  102.                     String.class, "Sorter");
  103.             comparatorMeta.setEnumeration(getComparatorKeys());
  104.             dataModel = new JavaBeanDataModel(getClass(), this,
  105.                     new MetaData[] { highlighterMeta, comparatorMeta });
  106.         } catch (IntrospectionException e) {
  107.             // TODO Auto-generated catch block
  108.             e.printStackTrace();
  109.         }
  110.         bindings.add(BindingFactory.getInstance().createBinding(
  111.                 highlighterCombo, dataModel, "highlighterKey"));
  112.         bindings.add(BindingFactory.getInstance().createMetaBinding(
  113.                 highlighterLabel, dataModel, "highlighterKey"));
  114.         bindings.add(BindingFactory.getInstance().createBinding(
  115.                 sorterGroup, dataModel, "comparatorKey"));
  116.         bindings.add(BindingFactory.getInstance().createMetaBinding(
  117.                 sorterGroupLabel, dataModel, "comparatorKey"));
  118.     }
  119.     // ------------------------ control find modus
  120.     /**
  121.      * PRE: getFindModi() contains value.
  122.      */
  123.     public void setHighlighterKey(String value) {
  124. //        if (value.equals(getFindModus()))
  125. //            return;
  126.         Object old = getHighlighterKey();
  127.         this.currentHighlighterKey = value;
  128.         updateHighlighter(old, value);
  129.         firePropertyChange("highlighterKey", old, getHighlighterKey());
  130.     }
  131.     /**
  132.      * returns the current find modus.
  133.      * 
  134.      * @return
  135.      */
  136.     public String getHighlighterKey() {
  137.         return currentHighlighterKey;
  138.     }
  139.     /**
  140.      * returns array of available find modi.
  141.      * 
  142.      * @return
  143.      */
  144.     public String[] getHighlighterKeys() {
  145.         // @todo: return copy!
  146.         return (String[] )highlighterKeys.toArray(new String[0]);
  147.     }
  148.     private void updateHighlighter(Object oldHighlighterKey, Object highlighterKey) {
  149.         Highlighter oldHighlighter = (Highlighter) highlighterMap.get(oldHighlighterKey);
  150.         if (oldHighlighter !=  null) {
  151.             table.getHighlighters().removeHighlighter(oldHighlighter);
  152.         }
  153.         Highlighter highlighter = (Highlighter) highlighterMap.get(highlighterKey);
  154.         if (highlighter != null) {
  155.             table.getHighlighters().addHighlighter(highlighter);
  156.         }
  157.       //  repaint();
  158.     }
  159.     public void setComparatorKey(String value) {
  160.         Object old = getComparatorKey();
  161.         this.currentComparatorKey = value;
  162.         updateComparator(value);
  163.         firePropertyChange("comparatorKey", old, getComparatorKey());
  164.         
  165.     }
  166.     
  167.     private void updateComparator(String value) {
  168.         Comparator comparator = (Comparator) comparatorMap.get(value);
  169.         // use a custom interactive sorter for the size column
  170.         table.getColumnExt("Size").getSorter().setComparator(comparator);
  171.      
  172.     }
  173.     public String getComparatorKey() {
  174.         return currentComparatorKey;
  175.     }
  176.     
  177.     public String[] getComparatorKeys() {
  178.         return (String[]) comparatorKeys.toArray(new String[0]);
  179.     }
  180.     
  181.     private void initDecorators() {
  182.         highlighterMap = new HashMap();
  183.         highlighterMap.put("ledgerBackground", Highlighter.ledgerBackground);
  184.         highlighterMap.put("notePadBackground", Highlighter.notePadBackground);
  185.         highlighterMap.put("alternate.beige", AlternateRowHighlighter.beige);
  186.         highlighterMap.put("altenate.classicLinePrinter", AlternateRowHighlighter.classicLinePrinter);
  187.         highlighterMap.put("altenate.floralWhite", AlternateRowHighlighter.floralWhite);
  188.         highlighterMap.put("altenate.linePrinter", AlternateRowHighlighter.linePrinter);
  189.         highlighterMap.put("altenate.quickSilver", AlternateRowHighlighter.quickSilver);
  190.         
  191.         highlighterKeys = new ArrayList();
  192.         highlighterKeys.addAll(highlighterMap.keySet());
  193.         Collections.sort(highlighterKeys);
  194.         highlighterKeys.add(0, null);
  195.         
  196.         comparatorMap = new HashMap();
  197.         comparatorMap.put("Point/Dimension", new XYComparator());
  198.         comparatorKeys = new ArrayList();
  199.         comparatorKeys.addAll(comparatorMap.keySet());
  200.         comparatorKeys.add("Lexical (default)");
  201.         Collections.sort(comparatorKeys);
  202.         currentComparatorKey = (String) comparatorKeys.get(0);
  203.         
  204.     }
  205.     private void configureComponents() {
  206.         // configure table
  207.         configureCommonTableProperties(table);
  208.         table.getTableHeader().setToolTipText("<html>" +
  209.                 "<b> Sorting: </b> <p> " +
  210.                 "Click to toggle sorting <br>" +
  211.                 "Ctrl-Shift-Click to unsort <p>" +
  212.                 "<b> Auto-Resizing: </b> <p>" +
  213.                 "Double-Click in resize region" +
  214.                 "</html>");
  215.         // configure treeTable
  216.         configureCommonTableProperties(treeTable);
  217.         treeTable.getTableHeader().setToolTipText("<html>" +
  218.                 "<b> Sorting: </b> <p> " +
  219.                 "Not supported <p>" +
  220.                 "<b> Auto-Resizing: </b> <p>" +
  221.                 "Double-Click in resize region" +
  222.                 "</html>");
  223.         treeTable.setRootVisible(true);
  224.         treeTable.setShowsRootHandles(false);
  225.         // share highlighter pipeline
  226.         table.setHighlighters(new HighlighterPipeline());
  227.         treeTable.setHighlighters(table.getHighlighters());
  228.     }
  229.     private void configureCommonTableProperties(JXTable table) {
  230.         table.setColumnControlVisible(true);
  231.   //      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  232.         table.setRolloverEnabled(true);
  233. //        table.packTable(0);
  234.         TableCellRenderer renderer = new DefaultTableCellRenderer() {
  235.             public void setValue(Object value) {
  236.                 if (value instanceof Point) {
  237.                     Point p = (Point) value;
  238.                     value = createString(p.x, p.y);
  239.                 } else if (value instanceof Dimension) {
  240.                     Dimension dim = (Dimension) value;
  241.                     value = createString(dim.width, dim.height);
  242.                 }
  243.                 super.setValue(value);
  244.             }
  245.             private Object createString(int width, int height) {
  246.                 return "(" + width + ", " + height + ")";
  247.             }
  248.         };
  249.         table.setDefaultRenderer(Point.class, renderer);
  250.         table.setDefaultRenderer(Dimension.class, renderer);
  251.         
  252.     }
  253. //------------------ create models
  254.     
  255.     private void initDemoModels() {
  256.         treeTableModel = new ComponentTreeTableModel(null);
  257.         tableModel = new ComponentTableModel();
  258.         tableModel.updateComponentList(treeTableModel);
  259.     }
  260.     public void addNotify() {
  261.         super.addNotify();
  262.         // a quick hack to get a static snapshot of the
  263.         // container hierarchy
  264.         treeTableModel.setRoot(SwingUtilities.windowForComponent(this));
  265.         treeTable.expandAll();
  266.         tableModel.updateComponentList(treeTableModel);
  267.     }
  268.     
  269. //-------------------- init UI
  270.     
  271.     /** This method is called from within the constructor to
  272.      * initialize the form.
  273.      */
  274.     private void initComponents() {
  275.         treeTable = new JXTreeTable();
  276.         table = new JXTable();
  277.         highlighterCombo = new JComboBox();
  278.         sorterGroup = new JXRadioGroup();
  279.         
  280.     }
  281.     
  282.     private void build() {
  283. //        COLUMN SPECS:
  284. //            f:d:g, l:4dluX:n, f:d:g
  285. //            ROW SPECS:   
  286. //            c:d:n, t:3dluY:n, f:d:g, t:4dluY:n, c:d:n
  287. //
  288. //            COLUMN GROUPS:  {}
  289. //            ROW GROUPS:     {}
  290. //
  291. //            COMPONENT CONSTRAINTS
  292. //            ( 1,  1,  1,  1, "d=f, d=c"); javax.swing.JLabel      "table"; name=tableLabel
  293. //            ( 3,  1,  1,  1, "d=f, d=c"); javax.swing.JLabel      "treeTable"; name=treeTableLabel
  294. //            ( 1,  3,  1,  1, "d=f, d=f"); javax.swing.JScrollPane; name=table
  295. //            ( 3,  3,  1,  1, "d=f, d=f"); javax.swing.JScrollPane; name=treeTable
  296. //            ( 1,  5,  3,  1, "d=f, d=c"); javax.swing.JPanel; name=tablecontrol
  297. //
  298.         
  299.         FormLayout formLayout = new FormLayout(
  300.                 "f:p:g, l:4dlu:n, f:p:g", // columns
  301.                 "c:d:n, t:3dlu:n, f:d:g, t:4dlu:n, c:d:n");  // rows
  302.         PanelBuilder builder = new PanelBuilder(this, formLayout);
  303.         builder.setDefaultDialogBorder();
  304.         CellConstraints cl = new CellConstraints();
  305.         CellConstraints cc = new CellConstraints();
  306.         
  307.         builder.addLabel("JXTable:", cl.xywh(1, 1, 1, 1), new JScrollPane(table), cc.xywh(1,  3,  1,  1) );
  308.         builder.addLabel("JXTreeTable:", cl.xywh(3, 1, 1, 1), new JScrollPane(treeTable), cc.xywh(3,  3,  1,  1));
  309.         builder.add(buildControl(), cc.xywh(1, 5, 3, 1));
  310.     }
  311.    
  312.     private JComponent buildControl() {
  313. //        COLUMN SPECS:
  314. //            r:p:n, l:3dluX:n, f:max(p;50dluX):n, f:0px:g
  315. //            ROW SPECS:   
  316. //            c:d:n, t:4dluY:n, c:d:n
  317. //
  318. //            COLUMN GROUPS:  {}
  319. //            ROW GROUPS:     {}
  320. //
  321. //            COMPONENT CONSTRAINTS
  322. //            ( 1,  1,  1,  1, "d=f, d=c"); javax.swing.JLabel      "highlighter"; name=highlighterLabel
  323. //            ( 3,  1,  1,  1, "d=f, d=c"); javax.swing.JComboBox; name=highlighter
  324. //            ( 1,  3,  1,  1, "d=f, d=c"); javax.swing.JLabel      "sorterGroup"; name=sorterGroupLabel
  325. //            ( 3,  3,  2,  1, "d=f, d=c"); de.kleopatra.view.JTitledPlaceHolder; name=sorterGroup
  326. //
  327.         FormLayout formLayout = new FormLayout(
  328.                 "r:p:n, l:3dlu:n, f:max(p;50dlu):n, f:0px:g", // columns
  329.                 "c:d:n, t:4dlu:n, c:d:n"); // rows
  330.         JXPanel control = new JXPanel();
  331.         PanelBuilder builder = new PanelBuilder(control, formLayout);
  332.         builder.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
  333.         CellConstraints cl = new CellConstraints();
  334.         CellConstraints cc = new CellConstraints();
  335.         highlighterLabel = builder.addLabel("", cl.xywh(1, 1, 1, 1), highlighterCombo, 
  336.                 cc.xywh(3, 1, 1, 1));
  337.         sorterGroupLabel = builder.addLabel("", cl.xywh(1, 3, 1, 1), sorterGroup, 
  338.                 cc.xywh(3, 3, 2, 1));
  339.         return control;
  340.     }
  341.     // --------------- super overrides
  342.     public String getHtmlDescription() {
  343.         return "<html>" +
  344.                 "With the SwingX <strong>JXTable</strong> and <strong>JXTreeTable</strong> " +
  345.                 "controls your users can <b>select displayed columns</b> at runtime, " +
  346.                 "<b>rearrange columns</b> via drag and drop, <b>highlight rows</b> with " +
  347.                 "pluggable background highlighters, <b>sort their data</b> via clickable headers " +
  348.                 "and more.<br><br>" +
  349.                 "In this demo, try rearranging columns by dragging the the column " +
  350.                 "headers with your mouse. Select the columns shown in the table by " +
  351.                 "clicking on the column control in the top right corner of the tables. " +
  352.                 "Change background color highlighting using the Highlighter combobox. " +
  353.                 "Sort rows by clicking on the column headers, and change the " +
  354.                 "sort characteristics using the Sorter radio buttons. Note the you can " +
  355.                 "also re-size table columns automatically from the column control, " +
  356.                 "as well as apply horizontal scrolling. " +
  357.                // "<a href="" + getHowToURLString() + "">Read More</a>" +
  358.                 "</html>";
  359.     }
  360.     public String getHowToURLString() {
  361.         return "resources/howto/JXTableHowTo.html";
  362.     }
  363.     
  364.     public String getInformationTitle() {
  365.          return "JXTable/JXTreeTable";
  366.     }
  367.     
  368.     public String getName() {
  369.         return "Extended Tables and Trees";
  370.     }
  371. }