View Javadoc

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