1
2
3
4
5
6
7
8
9
10
11 package nl.tudelft.simulation.introspection.gui;
12
13 import java.awt.BorderLayout;
14 import java.awt.Dimension;
15 import java.awt.FlowLayout;
16 import java.awt.Frame;
17 import java.awt.Toolkit;
18 import java.awt.Window;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.ActionListener;
21
22 import javax.swing.JButton;
23 import javax.swing.JDialog;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTable;
27 import javax.swing.ScrollPaneConstants;
28 import javax.swing.WindowConstants;
29
30 import nl.tudelft.simulation.introspection.table.DynamicTableModel;
31
32 /***
33 * A GUI element for presentation and manipulation of an introspected object.
34 * The dialog is 'powered' by an instance of {see ObjectJTable}. The dialog is
35 * positioned to a 'parent' window, or displayed centered if no parent window is
36 * available.
37 * <p>
38 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
39 * University of Technology </a>, the Netherlands. <br>
40 * See for project information <a
41 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
42 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
43 * License (GPL) </a>, no warranty <br>
44 *
45 * @author <a
46 * href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
47 * Lang </a><a
48 * href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
49 * Jacobs </a>
50 * @version 1.1 Apr 15, 2004
51 * @since 1.4
52 */
53 public class IntroSpectionDialog extends JDialog
54 {
55 /*** the table, set during initialization */
56 private JTable table;
57
58 /*** the parent window, set during initialization */
59 private Window parent;
60
61 /***
62 * Constructs a new IntroSpectionDialog.
63 *
64 * @param introspected The introspected object
65 */
66 public IntroSpectionDialog(final Object introspected)
67 {
68 this(null, introspected);
69 }
70
71 /***
72 * Constructs a new IntroSpectionDialog.
73 *
74 * @param parent The parent window, used for locating the dialog
75 * @param introspected The introspected object
76 */
77 public IntroSpectionDialog(final Window parent, final Object introspected)
78 {
79 this(parent, introspected.toString(), new ObjectJTable(
80 new ObjectTableModel(introspected)));
81 }
82
83 /***
84 * Constructs a new IntroSpectionDialog.
85 *
86 * @param title The title of the frame
87 * @param introspected The introspected object
88 */
89 public IntroSpectionDialog(final Object introspected, final String title)
90 {
91 this(null, title, new ObjectJTable(new ObjectTableModel(introspected)));
92 }
93
94 /***
95 * Constructs a new IntroSpectionDialog.
96 *
97 * @param title The title of the dialog
98 * @param content The object table-model containing the data of the
99 * introspected object
100 */
101 public IntroSpectionDialog(final String title,
102 final IntrospectingTableModelInterface content)
103 {
104 this(null, title, content);
105 }
106
107 /***
108 * Constructs a new IntroSpectionDialog.
109 *
110 * @param parent The parent window, used for locating the dialog
111 * @param title The title of the dialog
112 * @param content The object table-model containing the data of the
113 * introspected object
114 */
115 public IntroSpectionDialog(final Window parent, final String title,
116 final IntrospectingTableModelInterface content)
117 {
118 this(parent, title, new ObjectJTable(content));
119 }
120
121 /***
122 * Constructs a new IntroSpectionDialog.
123 *
124 * @param parent The parent window, used for locating the dialog
125 * @param title The title of the dialog
126 * @param introspected The introspected object
127 */
128 public IntroSpectionDialog(final Frame parent, final Object introspected,
129 final String title)
130 {
131 this(parent, title,
132 new ObjectJTable(new ObjectTableModel(introspected)));
133 }
134
135 /***
136 * Constructs a new IntroSpectionDialog.
137 *
138 * @param title The title of the dialog
139 * @param content The table displaying the data of the introspected object
140 */
141 public IntroSpectionDialog(final String title, final JTable content)
142 {
143 this(null, title, content);
144 }
145
146 /***
147 * Constructs a new IntroSpectionDialog.
148 *
149 * @param parent The parent window, used for locating the dialog
150 * @param title The title of the dialog
151 * @param content The table displaying the data of the introspected object
152 */
153 public IntroSpectionDialog(final Window parent, final String title,
154 final JTable content)
155 {
156 super();
157 this.parent = parent;
158 this.init(title, content);
159 }
160
161 /***
162 * initializes the dialog
163 *
164 * @param title the title of the dialog
165 * @param table the table to display
166 */
167 private void init(final String title, final JTable table)
168 {
169 this.table = table;
170 this.setModal(false);
171 this.setTitle(title);
172 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
173 this.getContentPane().setLayout(new BorderLayout());
174 JScrollPane pane = new JScrollPane(table,
175 ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
176 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
177 this.getContentPane().add(pane, BorderLayout.CENTER);
178 if (table instanceof ObjectJTableInterface)
179 {
180 if (((ObjectJTableInterface) table).getIntrospectingTableModel() instanceof DynamicTableModel)
181 {
182 DynamicTableModel model = (DynamicTableModel) ((ObjectJTableInterface) table)
183 .getIntrospectingTableModel();
184 this.getContentPane().add(new ButtonPanel(model, table),
185 BorderLayout.SOUTH);
186 }
187 }
188 this.pack();
189 setRelativeLocation();
190 this.setVisible(true);
191 }
192
193 /***
194 * Reformats this dialog to reflect changes in the table displayed.
195 */
196 protected void formatDialog()
197 {
198
199
200 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
201 if (this.table.getPreferredSize().height >= 0.5 * d.height
202 || this.table.getPreferredSize().height + getLocation().y >= 0.9 * d.height)
203 {
204 return;
205 }
206 this.table.setPreferredScrollableViewportSize(this.table
207 .getPreferredSize());
208 pack();
209 }
210
211 /***
212 * Initializes the location of this dialog relative to its parent window if
213 * any.
214 */
215 protected void setRelativeLocation()
216 {
217 setLocationRelativeTo(this.parent);
218 }
219
220 /***
221 * The ButtonPanel adds functionality for adding and removing rows in a
222 * table.
223 */
224 class ButtonPanel extends JPanel
225 {
226 /*** model */
227 private DynamicTableModel model;
228
229 /*** the viewer */
230 private JTable viewer;
231
232 /***
233 * Constructs a new ButtonPanel
234 *
235 * @param model the model to control
236 * @param viewer the viewer to control
237 */
238 public ButtonPanel(final DynamicTableModel model, final JTable viewer)
239 {
240 this.model = model;
241 this.viewer = viewer;
242 this.setLayout(new BorderLayout());
243 JPanel buttons = new JPanel();
244 FlowLayout manager = new FlowLayout();
245 manager.setHgap(0);
246 manager.setVgap(0);
247 buttons.setLayout(manager);
248 add(buttons, BorderLayout.CENTER);
249 JButton addButton = new JButton("Add row");
250 JButton delButton = new JButton("Delete rows");
251 buttons.add(addButton);
252 buttons.add(delButton);
253 addButton.addActionListener(new ActionListener()
254 {
255 public void actionPerformed(ActionEvent e)
256 {
257 ButtonPanel.this.addRow();
258 formatDialog();
259 }
260 });
261 delButton.addActionListener(new ActionListener()
262 {
263 public void actionPerformed(ActionEvent e)
264 {
265 ButtonPanel.this.delRows();
266 formatDialog();
267 }
268 });
269 }
270
271 /***
272 * Adds a row
273 */
274 protected void addRow()
275 {
276 this.model.createRow();
277 }
278
279 /***
280 * Deletes the rows currently selected from the table model.
281 */
282 protected void delRows()
283 {
284 this.model.deleteRows(this.viewer.getSelectedRows());
285 }
286 }
287 }