1
2
3
4
5
6
7
8 package nl.tudelft.simulation.introspection.gui;
9
10 import javax.swing.table.AbstractTableModel;
11
12 import nl.tudelft.simulation.introspection.Introspector;
13 import nl.tudelft.simulation.introspection.Property;
14 import nl.tudelft.simulation.introspection.beans.BeanIntrospector;
15 import nl.tudelft.simulation.logger.Logger;
16
17 /***
18 * The ObjectTableModel.
19 * <p>
20 * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
21 * University of Technology </a>, the Netherlands. <br>
22 * See for project information <a
23 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
24 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
25 * General Public License (LGPL) </a>, no warranty.
26 *
27 * @author <a
28 * href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
29 * Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
30 * Jacobs </a>
31 * @version 1.1 Apr 15, 2004
32 * @since 1.5
33 */
34 public class ObjectTableModel extends AbstractTableModel implements
35 IntrospectingTableModelInterface
36 {
37 /*** the properties */
38 protected Property[] properties = new Property[0];
39
40 /*** the columns */
41 private static String[] columns = { "Property", "+", "Value" };
42
43 /*** the expand buttons */
44 private ExpandButton[] buttons;
45
46 /*** the introspector */
47 private Introspector introspector = null;
48
49 /*** The model manager */
50 private ModelManager manager = new DefaultModelManager();
51
52 /***
53 * Creates an ObjectTableModel utilizing a {see
54 * nl.tudelft.simulation.introspection.beans.BeanIntrospector}.
55 *
56 * @param bean The object to be introspected according to the bean
57 * property-paradigm.
58 */
59 public ObjectTableModel(final Object bean)
60 {
61 this(bean, new BeanIntrospector());
62 }
63
64 /***
65 * Creates an ObjectTableModel utilizing a custom introspector.
66 *
67 * @param object The object to be introspected.
68 * @param introspector The introspector instance utilized.
69 */
70 public ObjectTableModel(final Object object, final Introspector introspector)
71 {
72 this.properties = introspector.getProperties(object);
73 this.buttons = new ExpandButton[this.properties.length];
74 for (int i = 0; i < this.buttons.length; i++)
75 {
76 this.buttons[i] = new ExpandButton(this.properties[i], this);
77 }
78 this.introspector = introspector;
79 }
80
81 /***
82 * @see javax.swing.table.TableModel#getRowCount()
83 */
84 public int getRowCount()
85 {
86 return this.properties.length;
87 }
88
89 /***
90 * @see javax.swing.table.TableModel#getColumnCount()
91 */
92 public int getColumnCount()
93 {
94 return columns.length;
95 }
96
97 /***
98 * @see javax.swing.table.TableModel#getValueAt(int, int)
99 */
100 public Object getValueAt(final int rowIndex, final int columnIndex)
101 {
102 Property requested = this.properties[rowIndex];
103 if (columnIndex == 0)
104 {
105 return requested.getName();
106 }
107 if (columnIndex == 1)
108 {
109 return this.buttons[rowIndex];
110 }
111 if (columnIndex == 2)
112 {
113 return requested.getValue();
114 }
115 return null;
116 }
117
118 /***
119 * @see javax.swing.table.TableModel#getColumnName(int)
120 */
121 @Override
122 public String getColumnName(final int columnIndex)
123 {
124 return columns[columnIndex];
125 }
126
127 /***
128 * @see javax.swing.table.TableModel#isCellEditable(int, int)
129 */
130 @Override
131 public boolean isCellEditable(final int rowIndex, final int columnIndex)
132 {
133 if (columnIndex == 1)
134 {
135 return true;
136 }
137 if (columnIndex == 2)
138 {
139 return (this.properties[rowIndex].isEditable() && !this.properties[rowIndex]
140 .getType().isArray());
141 }
142 return false;
143 }
144
145 /***
146 * @see javax.swing.table.TableModel#setValueAt(Object, int, int)
147 */
148 @Override
149 public void setValueAt(final Object aValue, final int rowIndex,
150 final int columnIndex)
151 {
152 if ((columnIndex != 2) || (!isCellEditable(rowIndex, columnIndex)))
153 {
154 return;
155 }
156 Property requested = this.properties[rowIndex];
157 try
158 {
159 requested.setValue(aValue);
160 } catch (IllegalArgumentException exception)
161 {
162 Logger.warning(this, "setValueAt", exception);
163 }
164 }
165
166 /***
167 * @see javax.swing.table.TableModel#getColumnClass(int)
168 */
169 @Override
170 public Class< ? > getColumnClass(final int columnIndex)
171 {
172 if (columnIndex == 1)
173 {
174 return ExpandButton.class;
175 }
176 return Object.class;
177 }
178
179 /***
180 * @see nl.tudelft.simulation.introspection.gui.IntrospectingTableModelInterface
181 * #getTypeAt(int,int)
182 */
183 public Class getTypeAt(final int rowIndex, final int columnIndex)
184 {
185 Property requested = this.properties[rowIndex];
186 if (columnIndex == 0)
187 {
188 return String.class;
189 }
190 if (columnIndex == 1)
191 {
192 return ExpandButton.class;
193 }
194 if (columnIndex == 2)
195 {
196 return requested.getType();
197 }
198 return null;
199 }
200
201 /***
202 * @param property the property
203 * @return Returns the index of the property in this tablemodel which name
204 * matches 'property'.
205 */
206 protected int getPropertyIndex(final String property)
207 {
208 for (int i = 0; i < this.properties.length; i++)
209 {
210 if (this.properties[i].getName().equalsIgnoreCase(property))
211 {
212 return i;
213 }
214 }
215 return -1;
216 }
217
218 /***
219 * @see nl.tudelft.simulation.introspection.gui.IntrospectingTableModelInterface
220 * #getProperty(java.lang.String)
221 */
222 public Property getProperty(final String propertyName)
223 {
224 int index = getPropertyIndex(propertyName);
225 if (index == -1)
226 {
227 return null;
228 }
229 return this.properties[index];
230 }
231
232 /***
233 * @see nl.tudelft.simulation.introspection.gui.IntrospectingTableModelInterface
234 * #getIntrospector()
235 */
236 public Introspector getIntrospector()
237 {
238 return this.introspector;
239 }
240
241 /***
242 * Sets the modelmanager. By default, a {see DefaultModelManager}is used.
243 *
244 * @param manager the manager
245 */
246 public void setModelManager(final ModelManager manager)
247 {
248 this.manager = manager;
249 }
250
251 /***
252 * By default, a {see DefaultModelManager}returned.
253 *
254 * @see nl.tudelft.simulation.introspection.gui.IntrospectingTableModelInterface
255 * #getModelManager()
256 */
257 public ModelManager getModelManager()
258 {
259 return this.manager;
260 }
261 }