1
2
3
4
5
6
7
8 package nl.tudelft.simulation.introspection.gui;
9
10 import java.awt.Insets;
11 import java.awt.Window;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.lang.reflect.Constructor;
15
16 import javax.swing.JButton;
17 import javax.swing.JTable;
18 import javax.swing.SwingUtilities;
19
20 import nl.tudelft.simulation.introspection.Introspector;
21 import nl.tudelft.simulation.introspection.Property;
22 import nl.tudelft.simulation.introspection.mapping.CellPresentationConfiguration;
23 import nl.tudelft.simulation.introspection.mapping.DefaultConfiguration;
24 import nl.tudelft.simulation.logger.Logger;
25
26 /***
27 * A table-element that spawns an introspection dialog for a property. In the
28 * new dialog, the property has become the introspected object.
29 * <p>
30 * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
31 * University of Technology </a>, the Netherlands. <br>
32 * See for project information <a
33 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
34 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
35 * General Public License (LGPL) </a>, no warranty.
36 *
37 * @author <a
38 * href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
39 * Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
40 * Jacobs </a>
41 * @version 1.1 Apr 15, 2004
42 * @since 1.5
43 */
44 public class ExpandButton extends JButton
45 {
46 /*** the JTable in which this button is actually displayed */
47 private JTable myTable;
48
49 /*** the property */
50 private final Property PROPERTY;
51
52 /*** the model */
53 private final IntrospectingTableModelInterface MODEL;
54
55 /***
56 * constructs a new ExpandButton
57 *
58 * @param property the property
59 * @param model the model
60 */
61 public ExpandButton(final Property property,
62 final IntrospectingTableModelInterface model)
63 {
64 super("+");
65 this.setMargin(new Insets(0, 0, 0, 0));
66 this.PROPERTY = property;
67 this.MODEL = model;
68 this.addActionListener(new ActionListener()
69 {
70 /***
71 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
72 */
73 public void actionPerformed(ActionEvent e)
74 {
75 showTable();
76 }
77 });
78 }
79
80 /***
81 * Sets the JTable in which this button is actually displayed. The reference
82 * is used to facilitate dialog creation.
83 *
84 * @param table the table
85 */
86 public void setMyJTable(final JTable table)
87 {
88 this.myTable = table;
89 }
90
91 /***
92 * Shows a new table introspecing the property.
93 */
94 public void showTable()
95 {
96 if (this.PROPERTY.getValue() == null)
97 {
98 return;
99 }
100 if (this.myTable != null)
101 {
102 Window parentWindow = SwingUtilities.getWindowAncestor(this);
103 new IntroSpectionDialog(parentWindow, this.PROPERTY.getName()
104 + ", " + this.PROPERTY.getValue(), instantiateTable());
105 } else
106 {
107 new IntroSpectionDialog(this.PROPERTY.getName() + ", "
108 + this.PROPERTY.getValue(), instantiateTable());
109 }
110 }
111
112 /***
113 * instantiates a JTable with an object model of the property.
114 *
115 * @return the JTable
116 */
117 private JTable instantiateTable()
118 {
119 IntrospectingTableModelInterface newModel = null;
120 ModelManager manager = this.MODEL.getModelManager();
121 Introspector introspector = this.MODEL.getIntrospector();
122 try
123 {
124 Class< ? > modelClass = null;
125 if (this.PROPERTY.isCollection())
126 {
127 modelClass = manager.getDefaultCollectionObjectTableModel();
128 Constructor< ? > c = modelClass.getConstructor(new Class[] {
129 Property.class, Introspector.class });
130 newModel = (IntrospectingTableModelInterface) c
131 .newInstance(new Object[] { this.PROPERTY, introspector });
132 } else
133 {
134 modelClass = manager.getDefaultObjectTableModel();
135 Constructor< ? > c = modelClass.getConstructor(new Class[] {
136 Object.class, Introspector.class });
137 newModel = (IntrospectingTableModelInterface) c
138 .newInstance(new Object[] { this.PROPERTY.getValue(),
139 introspector });
140 }
141 } catch (Exception exception)
142 {
143 Logger.warning(this, "instantiate",
144 " could not instantiate parent tablemodel, using default. Exception:"
145 + exception.getMessage());
146 if (this.PROPERTY.isCollection())
147 {
148 newModel = new CollectionTableModel(this.PROPERTY);
149 } else
150 {
151 newModel = new ObjectTableModel(this.PROPERTY.getValue());
152 }
153 }
154
155 CellPresentationConfiguration config = DefaultConfiguration
156 .getDefaultConfiguration();
157 if (this.myTable instanceof ICellPresentationConfigProvider)
158 config = ((ICellPresentationConfigProvider) this.myTable)
159 .getCellPresentationConfiguration();
160 JTable result = new ObjectJTable(newModel, config);
161
162 newModel.getModelManager().setDefaultCollectionObjectTableModel(
163 manager.getDefaultCollectionObjectTableModel());
164 newModel.getModelManager().setDefaultObjectTableModel(
165 manager.getDefaultObjectTableModel());
166 return result;
167 }
168 }