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