1 package nl.tudelft.simulation.dsol.swing.introspection.gui;
2
3 import java.awt.BorderLayout;
4 import java.awt.Dimension;
5 import java.awt.FlowLayout;
6 import java.awt.Frame;
7 import java.awt.Toolkit;
8 import java.awt.Window;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11
12 import javax.swing.JButton;
13 import javax.swing.JDialog;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JTable;
17 import javax.swing.ScrollPaneConstants;
18 import javax.swing.WindowConstants;
19
20 import nl.tudelft.simulation.dsol.swing.introspection.table.DynamicTableModel;
21 import nl.tudelft.simulation.introspection.DelegateIntrospection;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 public class IntrospectionDialog extends JDialog
39 {
40
41 private static final long serialVersionUID = 20140831L;
42
43
44 private JTable table;
45
46
47 private Window parent;
48
49
50
51
52
53 public IntrospectionDialog(final Object introspected)
54 {
55 this(null, introspected);
56 }
57
58
59
60
61
62
63 public IntrospectionDialog(final Window parent, final Object introspected)
64 {
65 this(parent, DelegateIntrospection.checkDelegation(introspected).toString(),
66 new ObjectJTable(new ObjectTableModel(introspected)));
67 }
68
69
70
71
72
73
74 public IntrospectionDialog(final Object introspected, final String title)
75 {
76 this(null, title, new ObjectJTable(new ObjectTableModel(introspected)));
77 }
78
79
80
81
82
83
84 public IntrospectionDialog(final String title, final IntrospectingTableModelInterface content)
85 {
86 this(null, title, content);
87 }
88
89
90
91
92
93
94
95 public IntrospectionDialog(final Window parent, final String title, final IntrospectingTableModelInterface content)
96 {
97 this(parent, title, new ObjectJTable(content));
98 }
99
100
101
102
103
104
105
106 public IntrospectionDialog(final Frame parent, final Object introspected, final String title)
107 {
108 this(parent, title, new ObjectJTable(new ObjectTableModel(introspected)));
109 }
110
111
112
113
114
115
116 public IntrospectionDialog(final String title, final JTable content)
117 {
118 this(null, title, content);
119 }
120
121
122
123
124
125
126
127 public IntrospectionDialog(final Window parent, final String title, final JTable content)
128 {
129 super();
130 this.parent = parent;
131 this.init(title, content);
132 }
133
134
135
136
137
138
139 private void init(final String title, final JTable newTable)
140 {
141 this.table = newTable;
142 this.setModal(false);
143 this.setTitle(title);
144 this.setAlwaysOnTop(true);
145 this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
146 this.getContentPane().setLayout(new BorderLayout());
147 JScrollPane pane = new JScrollPane(newTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
148 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
149 this.getContentPane().add(newTable.getTableHeader(), BorderLayout.NORTH);
150 this.getContentPane().add(pane, BorderLayout.CENTER);
151 if (newTable instanceof ObjectJTableInterface)
152 {
153 if (((ObjectJTableInterface) newTable).getIntrospectingTableModel() instanceof DynamicTableModel)
154 {
155 DynamicTableModel model = (DynamicTableModel) ((ObjectJTableInterface) newTable).getIntrospectingTableModel();
156 this.getContentPane().add(new ButtonPanel(model, newTable), BorderLayout.SOUTH);
157 }
158 }
159 this.pack();
160 setRelativeLocation();
161 this.setVisible(true);
162 }
163
164
165
166
167 public void formatDialog()
168 {
169
170
171 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
172 if (this.table.getPreferredSize().height >= 0.5 * d.height
173 || this.table.getPreferredSize().height + getLocation().y >= 0.9 * d.height)
174 {
175 return;
176 }
177 this.table.setPreferredScrollableViewportSize(this.table.getPreferredSize());
178 pack();
179 }
180
181
182
183
184 public void setRelativeLocation()
185 {
186 setLocationRelativeTo(this.parent);
187 }
188
189
190
191
192 class ButtonPanel extends JPanel
193 {
194
195 private static final long serialVersionUID = 20140831L;
196
197
198 private DynamicTableModel model;
199
200
201 private JTable viewer;
202
203
204
205
206
207
208 ButtonPanel(final DynamicTableModel model, final JTable viewer)
209 {
210 this.model = model;
211 this.viewer = viewer;
212 this.setLayout(new BorderLayout());
213 JPanel buttons = new JPanel();
214 FlowLayout manager = new FlowLayout();
215 manager.setHgap(0);
216 manager.setVgap(0);
217 buttons.setLayout(manager);
218 add(buttons, BorderLayout.CENTER);
219 JButton addButton = new JButton("Add row");
220 JButton delButton = new JButton("Delete rows");
221 if (!model.isRowEditable())
222 {
223 addButton.setEnabled(false);
224 delButton.setEnabled(false);
225 }
226 buttons.add(addButton);
227 buttons.add(delButton);
228 addButton.addActionListener(new ActionListener()
229 {
230 @Override
231 public void actionPerformed(final ActionEvent e)
232 {
233 ButtonPanel.this.addRow();
234 formatDialog();
235 }
236 });
237 delButton.addActionListener(new ActionListener()
238 {
239 @Override
240 public void actionPerformed(final ActionEvent e)
241 {
242 ButtonPanel.this.delRows();
243 formatDialog();
244 }
245 });
246 }
247
248
249
250
251 protected void addRow()
252 {
253 this.model.createRow();
254 }
255
256
257
258
259 protected void delRows()
260 {
261 this.model.deleteRows(this.viewer.getSelectedRows());
262 }
263 }
264 }