1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.gscg.gameleader.animation2D.mouse;
16
17 import info.clearthought.layout.TableLayout;
18 import info.clearthought.layout.TableLayoutConstants;
19
20 import java.awt.Color;
21 import java.awt.Dimension;
22 import java.awt.FlowLayout;
23 import java.awt.Font;
24 import java.awt.event.ActionEvent;
25 import java.text.NumberFormat;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.swing.AbstractAction;
31 import javax.swing.BorderFactory;
32 import javax.swing.JButton;
33 import javax.swing.JComponent;
34 import javax.swing.JDialog;
35 import javax.swing.JLabel;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39
40 import nl.tudelft.simulation.logger.Logger;
41
42 import org.gscg.gameleader.interactionlayer.RemoteInteractionLayerInterface;
43
44
45 /***
46 * The IntrospectedObjectDataAction opens an introspection dialog if the action
47 * is invoked via a popup window on an animation panel.
48 * <p>
49 *
50 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
51 * Delft, the Netherlands. All rights reserved.
52 *
53 * See for project information <a href="http://www.simulation.tudelft.nl/">
54 * www.simulation.tudelft.nl </a>.
55 *
56 * The source code and binary code of this software is proprietary information
57 * of Delft University of Technology.
58 *
59 * @author <a
60 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
61 * van Houten </a>
62 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
63 * @since 1.1.2
64 */
65 public class IntrospectedObjectDataAction extends AbstractAction
66 {
67 /*** the serial version uid */
68 private static final long serialVersionUID = 11L;
69
70 /*** the data to create a dialog for */
71 private IntrospectionData data = null;
72
73 /*** the button */
74 private JButton button = null;
75
76 /*** the array with editable fields */
77 private ArrayList editables = new ArrayList();
78
79 /*** the server side remote interaction layer */
80 private RemoteInteractionLayerInterface remoteInteractionLayerInterface = null;
81
82 /*** the dialog */
83 private JDialog dialog = null;
84
85 /***
86 * constructs a new IntrospectedObjectDataAction
87 *
88 * @param data the data
89 * @param remoteInteractionLayerInterface the remote interaction layer
90 */
91 public IntrospectedObjectDataAction(
92 final IntrospectionData data,
93 final RemoteInteractionLayerInterface remoteInteractionLayerInterface)
94 {
95 super(data.getName());
96 this.button = new JButton("update");
97 this.button.addActionListener(this);
98 this.data = data;
99 this.remoteInteractionLayerInterface = remoteInteractionLayerInterface;
100 }
101
102 /***
103 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
104 */
105 public void actionPerformed(final ActionEvent actionEvent)
106 {
107 if (actionEvent.getSource() != this.button)
108 {
109 this.dialog = new JDialog();
110 this.dialog.setTitle(this.data.getName());
111 this.dialog.getContentPane().setLayout(new FlowLayout());
112 this.dialog.getContentPane().setBackground(Color.WHITE);
113
114 double[][] layout = {{TableLayoutConstants.FILL}, {20}};
115 TableLayout tableLayout = new TableLayout(layout);
116 JPanel dataPanel = new JPanel(tableLayout);
117 dataPanel.setOpaque(false);
118
119 boolean addButton = false;
120
121 for (int i = 0; i < this.data.getData().length; i++)
122 {
123 IntrospectedFieldData rowData = this.data.getData()[i];
124
125 JPanel panel = new JPanel();
126 panel.setOpaque(false);
127 double[][] panelLayout = {{150, 150}, {20}};
128 panel.setLayout(new TableLayout(panelLayout));
129 JLabel label = new JLabel(rowData.getName());
130 label.setPreferredSize(new Dimension(150, 20));
131 label.setOpaque(false);
132 label.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
133 label.setFocusable(false);
134
135
136 if (rowData.getValue() instanceof String)
137 {
138 if (((String) rowData.getValue()).equalsIgnoreCase("##"))
139 {
140
141 label.setOpaque(true);
142 label.setFont(label.getFont().deriveFont(Font.BOLD));
143 label.setBackground(Color.BLUE);
144 label.setForeground(Color.WHITE);
145 }
146 }
147
148 JComponent field = null;
149 if (!rowData.isEditable())
150 {
151 field = new JLabel("" + rowData.getValue());
152 field.setFocusable(false);
153 field.setOpaque(false);
154
155 if (rowData.getValue() instanceof String)
156 {
157 if (((String) rowData.getValue())
158 .equalsIgnoreCase("##"))
159 {
160
161 field.setOpaque(true);
162 field.setBackground(Color.BLUE);
163 field.setForeground(Color.WHITE);
164 ((JLabel) field).setText("");
165 }
166 }
167 } else
168 {
169 addButton = true;
170 field = new CustomJTextField("" + rowData.getValue(),
171 rowData);
172 ((JTextField) field).setEditable(true);
173 ((JTextField) field).setSelectedTextColor(Color.WHITE);
174 ((JTextField) field).setSelectionColor(Color.BLUE);
175 this.editables.add(field);
176 }
177
178 field.setPreferredSize(new Dimension(150, 20));
179 field.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
180 panel.add(label, "0,0,L,C");
181 panel.add(field, "1,0,L,C");
182 tableLayout.insertRow(tableLayout.getNumRow(), 20.0);
183 dataPanel.add(panel, "0," + (tableLayout.getNumRow() - 1)
184 + ",L,C");
185 }
186
187 if (addButton)
188 {
189
190 tableLayout.insertRow(tableLayout.getNumRow(), 20.0);
191 dataPanel.add(this.button, "0," + (tableLayout.getNumRow() - 1)
192 + ",C,C");
193 }
194 this.dialog.getContentPane().add(dataPanel);
195 dataPanel.setPreferredSize(new Dimension(300, tableLayout
196 .getNumRow() * 20));
197 this.dialog.pack();
198 this.dialog.setResizable(false);
199 this.dialog.setLocationRelativeTo(null);
200 this.dialog.show();
201 } else
202 {
203 NumberFormat numberFormat = NumberFormat.getInstance();
204 IntrospectedFieldData fieldData = null;
205 List changes = new ArrayList();
206 try
207 {
208 for (int i = 0; i < this.editables.size(); i++)
209 {
210 fieldData = ((CustomJTextField) this.editables.get(i))
211 .getFieldData();
212 changes.add(fieldData);
213 JTextField text = (CustomJTextField) this.editables.get(i);
214
215 Number number = numberFormat.parse(text.getText().trim());
216 fieldData.setValue(number);
217 }
218
219 this.remoteInteractionLayerInterface
220 .updateIntrospectedObject(new IntrospectionData(
221 this.data.getKey(),
222 (IntrospectedFieldData[]) changes
223 .toArray(new IntrospectedFieldData[changes
224 .size()]), this.data.getName()));
225 this.dialog.dispose();
226 } catch (ParseException parseException)
227 {
228 JOptionPane.showMessageDialog(null,
229 " Exception occurred while parsing text.\n Please adjust your text for field: "
230 + fieldData.getName() + ".", "Error",
231 JOptionPane.ERROR_MESSAGE);
232 } catch (Exception exception)
233 {
234 Logger.severe(this, "actionPerformed", exception);
235 }
236 }
237 }
238 /***
239 * A custom textfield.
240 * <p>
241 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628
242 * BX Delft, the Netherlands. All rights reserved.
243 *
244 * See for project information <a href="http://www.simulation.tudelft.nl/">
245 * www.simulation.tudelft.nl </a>.
246 *
247 * The source code and binary code of this software is proprietary
248 * information of Delft University of Technology.
249 *
250 * @author <a
251 * href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
252 * van Houten </a>
253 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:54 $
254 * @since 1.1.2
255 */
256 private class CustomJTextField extends JTextField
257 {
258 /*** the serial version uid */
259 private static final long serialVersionUID = 11L;
260
261 /*** the field data */
262 private IntrospectedFieldData fieldData = null;
263
264 /***
265 * constructs a new CustomJTextField
266 *
267 * @param text the text
268 * @param fieldData the data
269 */
270 public CustomJTextField(final String text,
271 final IntrospectedFieldData fieldData)
272 {
273 super(text);
274 this.fieldData = fieldData;
275 }
276
277 /***
278 * @return Returns the field data
279 */
280 public IntrospectedFieldData getFieldData()
281 {
282 return this.fieldData;
283 }
284 }
285 }