View Javadoc

1   /*
2    * @(#) ObjectJTable.java Apr 15, 2004 Copyright (c) 2002-2005 Delft University
3    * of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights
4    * reserved. This software is proprietary information of Delft University of
5    * Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.introspection.gui;
8   
9   import java.awt.event.HierarchyEvent;
10  import java.awt.event.HierarchyListener;
11  import java.util.Collection;
12  
13  import javax.swing.JTable;
14  import javax.swing.ListSelectionModel;
15  import javax.swing.table.JTableHeader;
16  import javax.swing.table.TableCellEditor;
17  import javax.swing.table.TableCellRenderer;
18  import javax.swing.table.TableColumnModel;
19  
20  import nl.tudelft.simulation.introspection.mapping.CellPresentationConfiguration;
21  import nl.tudelft.simulation.introspection.mapping.DefaultConfiguration;
22  import nl.tudelft.simulation.introspection.sortable.SortDefinition;
23  import nl.tudelft.simulation.introspection.sortable.SortingTableHeader;
24  
25  /***
26   * * A customization of a standard JTable to allow the display of an
27   * introspected object. The behaviour of the ObjectJTable depends on the
28   * contained TableModel. {see ObjectTableModel}provides a view of the properties
29   * and values of a single introspected object. {see
30   * CollectionTableModel}provides a view on a collection of instances: usually
31   * the value of a composite property.
32   * <p>
33   * A configuration mechanism is implemented to load the editors and renders to
34   * be used by this JTable. See {see
35   * #setConfig(nl.tudelft.simulation.introspection.mapping.CellPresentationConfiguration)}
36   * for details.
37   * <p>
38   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
39   * University of Technology </a>, the Netherlands. <br>
40   * See for project information <a
41   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
42   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
43   * General Public License (LGPL) </a>, no warranty.
44   * 
45   * @author <a
46   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
47   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
48   *         Jacobs </a>
49   * @version 1.1 Apr 15, 2004
50   * @since 1.5
51   */
52  public class ObjectJTable extends JTable implements ObjectJTableInterface,
53          ICellPresentationConfigProvider
54  {
55      /*** the updateTimer */
56      private static UpdateTimer updateTimer = new UpdateTimer(100L);
57  
58      /*** hasShown? */
59      protected boolean hasShown = false;
60  
61      /*** the introspectionTableModel */
62      private IntrospectingTableModelInterface introspectionTableModel;
63  
64      /***
65       * The configuration used to assign renderers and editors to cells.
66       */
67      private final CellPresentationConfiguration CONFIG;
68  
69      /***
70       * constructs a new ObjectJTable
71       * 
72       * @param dm the defaultTableModel
73       */
74      public ObjectJTable(final IntrospectingTableModelInterface dm)
75      {
76          this(dm, DefaultConfiguration.getDefaultConfiguration());
77      }
78  
79      /***
80       * constructs a new ObjectJTable
81       * 
82       * @param dm the defaultTableModel
83       * @param config the CellPresentationConfiguration
84       */
85      public ObjectJTable(final IntrospectingTableModelInterface dm,
86              final CellPresentationConfiguration config)
87      {
88          super(new SortingObjectTableModel(dm));
89          this.CONFIG = config;
90          init(dm);
91      }
92  
93      /***
94       * Constructor for ObjectJTable.
95       * 
96       * @param dm the defaultTableModel
97       * @param cm the tableColumnModel
98       */
99      public ObjectJTable(final IntrospectingTableModelInterface dm,
100             final TableColumnModel cm)
101     {
102         super(new SortingObjectTableModel(dm), cm);
103         this.CONFIG = DefaultConfiguration.getDefaultConfiguration();
104         init(dm);
105     }
106 
107     /***
108      * Constructor for ObjectJTable.
109      * 
110      * @param dm the defaultTableModel
111      * @param cm the tableColumnModel
112      * @param sm the listSelectionModel
113      */
114     public ObjectJTable(final IntrospectingTableModelInterface dm,
115             final TableColumnModel cm, final ListSelectionModel sm)
116     {
117         super(new SortingObjectTableModel(dm), cm, sm);
118         this.CONFIG = DefaultConfiguration.getDefaultConfiguration();
119         init(dm);
120     }
121 
122     /***
123      * @see nl.tudelft.simulation.introspection.gui.ICellPresentationConfigProvider#getCellPresentationConfiguration()
124      */
125     public CellPresentationConfiguration getCellPresentationConfiguration()
126     {
127         return this.CONFIG;
128     }
129 
130     /***
131      * initializes the objectJTable
132      * 
133      * @param model the model
134      */
135     private void init(IntrospectingTableModelInterface model)
136     {
137         this.introspectionTableModel = model;
138         initConfig();
139         setPreferredScrollableViewportSize(this.getPreferredSize());
140         JTableHeader header = new SortingTableHeader(
141                 new SortDefinition[] { new SortDefinition(0, true) });
142         this.setTableHeader(header);
143         header.setColumnModel(this.getColumnModel());
144         ObjectJTable.updateTimer.add(this);
145     }
146 
147     /***
148      * Enables the installation of a special renderer for arrays and
149      * Collections.
150      * 
151      * @see javax.swing.JTable#getDefaultRenderer(java.lang.Class)
152      */
153     @Override
154     public TableCellRenderer getDefaultRenderer(Class columnClass)
155     {
156         if (columnClass.isArray())
157             return super.getDefaultRenderer(Object[].class);
158         if (Collection.class.isAssignableFrom(columnClass))
159             return super.getDefaultRenderer(Collection.class);
160         return super.getDefaultRenderer(columnClass);
161     }
162 
163     /***
164      * the ParentListener
165      */
166     private class ParentListener implements HierarchyListener
167     {
168         /***
169          * @see java.awt.event.HierarchyListener#hierarchyChanged(HierarchyEvent)
170          */
171         public void hierarchyChanged(final HierarchyEvent e)
172         {
173             if (e.getChangeFlags() == HierarchyEvent.DISPLAYABILITY_CHANGED)
174             {
175                 if (!ObjectJTable.this.hasShown && isDisplayable())
176                 {
177                     ObjectJTable.this.hasShown = true;
178                     return;
179                 }
180                 if (ObjectJTable.this.hasShown && !isDisplayable())
181                 {
182                     ObjectJTable.this.getModel().removeTableModelListener(
183                             ObjectJTable.this);
184                 }
185             }
186         }
187     }
188 
189     /***
190      * Initializes the configuration, by propagating its settings to the table's
191      * set of default renderers/editors.
192      */
193     private void initConfig()
194     {
195         addHierarchyListener(new ParentListener());
196         Class< ? >[][] renderers = this.CONFIG.getRenderers();
197         Class< ? >[][] editors = this.CONFIG.getEditors();
198         try
199         {
200             for (int i = 0; i < renderers.length; i++)
201             {
202                 this.setDefaultRenderer(renderers[i][0],
203                         (TableCellRenderer) renderers[i][1].newInstance());
204             }
205             for (int i = 0; i < editors.length; i++)
206             {
207                 this.setDefaultEditor(editors[i][0],
208                         (TableCellEditor) editors[i][1].newInstance());
209             }
210         } catch (Exception e)
211         {
212             throw new IllegalArgumentException("Configuration " + this.CONFIG
213                     + "failed, " + "probably invalid classes.");
214         }
215         this.getColumn(getColumnName(0)).setPreferredWidth(70);
216         this.getColumn(getColumnName(1)).setMaxWidth(25);
217         this.getColumn(getColumnName(2)).setPreferredWidth(450);
218         this.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
219     }
220 
221     /***
222      * @see nl.tudelft.simulation.introspection.gui.ObjectJTableInterface
223      *      #getIntrospectingTableModel()
224      */
225     public IntrospectingTableModelInterface getIntrospectingTableModel()
226     {
227         return this.introspectionTableModel;
228     }
229 }