View Javadoc

1   /*
2    * @(#) ObjectTableModel.java Apr 15, 2004
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.introspection.gui;
12  
13  import javax.swing.table.AbstractTableModel;
14  
15  import nl.tudelft.simulation.introspection.Introspector;
16  import nl.tudelft.simulation.introspection.Property;
17  import nl.tudelft.simulation.introspection.beans.BeanIntrospector;
18  import nl.tudelft.simulation.logger.Logger;
19  
20  /***
21   * The ObjectTableModel.
22   * <p>
23   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
24   * University of Technology </a>, the Netherlands. <br>
25   * See for project information <a
26   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
27   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
28   * License (GPL) </a>, no warranty <br>
29   * 
30   * @author <a
31   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
32   *         Lang </a><a
33   *         href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
34   *         Jacobs </a>
35   * @version 1.1 Apr 15, 2004
36   * @since 1.4
37   */
38  public class ObjectTableModel extends AbstractTableModel implements
39  		IntrospectingTableModelInterface
40  {
41  	/*** the properties */
42  	protected Property[] properties = new Property[0];
43  
44  	/*** the columns */
45  	private static String[] columns = {"Property", "+", "Value"};
46  
47  	/*** the expand buttons */
48  	private ExpandButton[] buttons;
49  
50  	/*** the introspector */
51  	private Introspector introspector = null;
52  
53  	/*** The model manager */
54  	private ModelManager manager = new DefaultModelManager();
55  
56  	/***
57  	 * Creates an ObjectTableModel utilizing a {see
58  	 * nl.tudelft.simulation.introspection.beans.BeanIntrospector}.
59  	 * 
60  	 * @param bean The object to be introspected according to the bean
61  	 *        property-paradigm.
62  	 */
63  	public ObjectTableModel(final Object bean)
64  	{
65  		this(bean, new BeanIntrospector());
66  	}
67  
68  	/***
69  	 * Creates an ObjectTableModel utilizing a custom introspector.
70  	 * 
71  	 * @param object The object to be introspected.
72  	 * @param introspector The introspector instance utilized.
73  	 */
74  	public ObjectTableModel(final Object object, final Introspector introspector)
75  	{
76  		this.properties = introspector.getProperties(object);
77  		this.buttons = new ExpandButton[this.properties.length];
78  		for (int i = 0; i < this.buttons.length; i++)
79  		{
80  			this.buttons[i] = new ExpandButton(this.properties[i], this);
81  		}
82  		this.introspector = introspector;
83  	}
84  
85  	/***
86  	 * @see javax.swing.table.TableModel#getRowCount()
87  	 */
88  	public int getRowCount()
89  	{
90  		return this.properties.length;
91  	}
92  
93  	/***
94  	 * @see javax.swing.table.TableModel#getColumnCount()
95  	 */
96  	public int getColumnCount()
97  	{
98  		return columns.length;
99  	}
100 
101 	/***
102 	 * @see javax.swing.table.TableModel#getValueAt(int, int)
103 	 */
104 	public Object getValueAt(final int rowIndex, final int columnIndex)
105 	{
106 		Property requested = this.properties[rowIndex];
107 		if (columnIndex == 0)
108 		{
109 			return requested.getName();
110 		}
111 		if (columnIndex == 1)
112 		{
113 			return this.buttons[rowIndex];
114 		}
115 		if (columnIndex == 2)
116 		{
117 			return requested.getValue();
118 		}
119 		return null;
120 	}
121 
122 	/***
123 	 * @see javax.swing.table.TableModel#getColumnName(int)
124 	 */
125 	public String getColumnName(final int columnIndex)
126 	{
127 		return columns[columnIndex];
128 	}
129 
130 	/***
131 	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
132 	 */
133 	public boolean isCellEditable(final int rowIndex, final int columnIndex)
134 	{
135 		if (columnIndex == 1)
136 		{
137 			return true;
138 		}
139 		if (columnIndex == 2)
140 		{
141 			return (this.properties[rowIndex].isEditable() && !this.properties[rowIndex]
142 					.getType().isArray());
143 		}
144 		return false;
145 	}
146 
147 	/***
148 	 * @see javax.swing.table.TableModel#setValueAt(Object, int, int)
149 	 */
150 	public void setValueAt(final Object aValue, final int rowIndex,
151 			final int columnIndex)
152 	{
153 		if ((columnIndex != 2) || (!isCellEditable(rowIndex, columnIndex)))
154 		{
155 			return;
156 		}
157 		Property requested = this.properties[rowIndex];
158 		try
159 		{
160 			requested.setValue(aValue);
161 		} catch (IllegalArgumentException exception)
162 		{
163 			Logger.warning(this, "setValueAt", exception);
164 		}
165 	}
166 
167 	/***
168 	 * @see javax.swing.table.TableModel#getColumnClass(int)
169 	 */
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 }