1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.gui.panels;
11
12 import java.awt.BorderLayout;
13 import java.awt.Color;
14 import java.awt.Dimension;
15 import java.rmi.RemoteException;
16 import java.text.NumberFormat;
17
18 import javax.swing.BorderFactory;
19 import javax.swing.BoxLayout;
20 import javax.swing.JButton;
21 import javax.swing.JPanel;
22 import javax.swing.JTextField;
23 import javax.swing.UIManager;
24
25 import nl.tudelft.simulation.dsol.experiment.Experiment;
26 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
27 import nl.tudelft.simulation.dsol.gui.actions.FastForwardAction;
28 import nl.tudelft.simulation.dsol.gui.actions.ResetAction;
29 import nl.tudelft.simulation.dsol.gui.actions.StartAction;
30 import nl.tudelft.simulation.dsol.gui.actions.StepAction;
31 import nl.tudelft.simulation.dsol.gui.actions.StopAction;
32 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
33 import nl.tudelft.simulation.event.Event;
34 import nl.tudelft.simulation.event.EventInterface;
35 import nl.tudelft.simulation.event.EventListenerInterface;
36 import nl.tudelft.simulation.logger.Logger;
37
38 /***
39 * The StatusBar <br>
40 * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
41 * University of Technology </a>, the Netherlands. <br>
42 * See for project information <a
43 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
44 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
45 * License (GPL) </a>, no warranty <br>
46 *
47 * @version 1.0 18.10.2003 <br>
48 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
49 * Jacobs </a>
50 */
51 public class Statusbar extends JPanel implements EventListenerInterface
52 {
53
54 /***
55 * the timeField of the statusBar
56 *
57 * @uml.property name="timeField"
58 */
59 private JTextField timeField = new JTextField();
60
61
62 /*** the replicationField of the statusBar */
63 private JTextField replicationField = new JTextField();
64
65 /*** the replicationField of the statusBar */
66 private JTextField treatmentField = new JTextField();
67
68 /*** the parent DSOL Panel */
69 private DSOLApplicationInterface application = null;
70
71 /***
72 * the units
73 *
74 * @uml.property name="units"
75 */
76 private String units = null;
77
78 /***
79 * the simulatorTime
80 *
81 * @uml.property name="simulatorTime"
82 */
83 private double simulatorTime = Double.NaN;
84
85
86 /***
87 * constructs a new Statusbar
88 *
89 * @param application the application
90 */
91 public Statusbar(final DSOLApplicationInterface application)
92 {
93 super();
94 this.application = application;
95 this.initialize();
96 try
97 {
98 this.application.addListener(this,
99 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
100 this.notify(new Event(
101 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
102 this.application, this.application.getExperiment()));
103 if (this.application.getExperiment() != null
104 && this.application.getExperiment().getSimulator() != null)
105 {
106 if (this.application.getExperiment().getSimulator()
107 .getReplication() != null)
108 {
109 this.notify(new Event(
110 SimulatorInterface.START_REPLICATION_EVENT,
111 this.application.getExperiment().getSimulator(),
112 this.application.getExperiment().getSimulator()
113 .getReplication()));
114 }
115 }
116 } catch (Exception exception)
117 {
118 Logger.warning(this, "Statusbar", exception);
119 }
120 }
121
122 /***
123 * initializes the statusbar
124 */
125 private void initialize()
126 {
127 Color backgroundColor = UIManager.getColor("MenuBar.background");
128 this.setBackground(backgroundColor);
129 this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
130 JPanel controlPanel = new JPanel();
131 controlPanel.setBackground(backgroundColor);
132 controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.X_AXIS));
133 StepAction stepAction = new StepAction(this.application);
134 JButton stepButton = new JButton(stepAction);
135 stepButton.setText("");
136 stepButton.setBackground(backgroundColor);
137 stepButton.setToolTipText("steps the simulator");
138 stepButton.setBorder(BorderFactory.createEmptyBorder());
139 controlPanel.add(stepButton);
140 StartAction startAction = new StartAction(this.application);
141 JButton startButton = new JButton(startAction);
142 startButton.setText("");
143 startButton.setBackground(backgroundColor);
144 startButton.setToolTipText("starts the simulator");
145 startButton.setBorder(BorderFactory.createEmptyBorder());
146 controlPanel.add(startButton);
147 StopAction stopAction = new StopAction(this.application);
148 JButton stopButton = new JButton(stopAction);
149 stopButton.setText("");
150 stopButton.setBackground(backgroundColor);
151 stopButton.setToolTipText("stops the simulator");
152 stopButton.setBorder(BorderFactory.createEmptyBorder());
153 controlPanel.add(stopButton);
154 FastForwardAction fastForwardAction = new FastForwardAction(
155 this.application);
156 JButton fastForwardButton = new JButton(fastForwardAction);
157 fastForwardButton.setText("");
158 fastForwardButton.setBackground(backgroundColor);
159 fastForwardButton.setToolTipText("fast forwards the simulator");
160 fastForwardButton.setBorder(BorderFactory.createEmptyBorder());
161 controlPanel.add(fastForwardButton);
162 ResetAction resetAction = new ResetAction(this.application);
163 JButton resetButton = new JButton(resetAction);
164 resetButton.setText("");
165 resetButton.setBackground(backgroundColor);
166 resetButton.setToolTipText("resets the experiment");
167 resetButton.setBorder(BorderFactory.createEmptyBorder());
168 controlPanel.add(resetButton);
169 JPanel statusPanel = new JPanel();
170 statusPanel.setBackground(backgroundColor);
171 statusPanel.setBorder(BorderFactory.createEmptyBorder());
172 statusPanel.setLayout(new BorderLayout());
173 this.timeField.setBackground(Color.WHITE);
174 this.timeField.setPreferredSize(new Dimension(200, 12));
175 this.timeField.setEditable(false);
176 this.timeField.setText("Time:");
177 this.timeField.setToolTipText("The simulator time");
178 statusPanel.add(this.timeField, BorderLayout.WEST);
179 JPanel eastSide = new JPanel();
180 eastSide.setBackground(backgroundColor);
181 eastSide.setBorder(BorderFactory.createEmptyBorder());
182 eastSide.setLayout(new BorderLayout());
183 this.replicationField.setBackground(Color.WHITE);
184 this.replicationField.setEditable(false);
185 this.replicationField.setText("Rep(-/-)");
186 this.replicationField
187 .setToolTipText("The currently running replication");
188 this.treatmentField.setBackground(Color.WHITE);
189 this.treatmentField.setEditable(false);
190 this.treatmentField.setText("Treat(-/-)");
191 this.treatmentField.setToolTipText("The currently running treatment");
192 eastSide.add(this.replicationField, BorderLayout.WEST);
193 eastSide.add(this.treatmentField, BorderLayout.EAST);
194 statusPanel.add(eastSide, BorderLayout.EAST);
195 this.add(controlPanel);
196 this.add(statusPanel);
197 new TimeUpdater(this).start();
198 }
199
200 /***
201 * gets simulatorTime
202 *
203 * @return Returns the simulatorTime.
204 *
205 * @uml.property name="simulatorTime"
206 */
207 protected double getSimulatorTime()
208 {
209 return this.simulatorTime;
210 }
211
212 /***
213 * gets timeField
214 *
215 * @return Returns the timeField.
216 *
217 * @uml.property name="timeField"
218 */
219 protected JTextField getTimeField()
220 {
221 return this.timeField;
222 }
223
224 /***
225 * gets units
226 *
227 * @return Returns the units.
228 *
229 * @uml.property name="units"
230 */
231 protected String getUnits()
232 {
233 return this.units;
234 }
235
236 /***
237 * @see nl.tudelft.simulation.event.EventListenerInterface
238 * #notify(nl.tudelft.simulation.event.EventInterface)
239 */
240 public void notify(final EventInterface event) throws RemoteException
241 {
242 if (event.getSource() instanceof DSOLApplicationInterface)
243 {
244 if (event.getType().equals(
245 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
246 {
247 if (event.getContent() != null)
248 {
249 Experiment experiment = (Experiment) event.getContent();
250 experiment.addListener(this,
251 Experiment.SIMULATOR_CHANGED_EVENT);
252 this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
253 experiment, experiment.getSimulator()));
254 }
255 return;
256 }
257 } else if (event.getSource() instanceof Experiment)
258 {
259 if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT))
260 {
261 this.simulatorTime = Double.NaN;
262 if (event.getContent() != null)
263 {
264 SimulatorInterface simulator = (SimulatorInterface) event
265 .getContent();
266 simulator.addListener(this,
267 SimulatorInterface.START_REPLICATION_EVENT);
268 simulator.addListener(this,
269 SimulatorInterface.TIME_CHANGED_EVENT);
270 this.treatmentField.setText("Treat(-/-)");
271 this.replicationField.setText("Rep(-/-)");
272 this.timeField.setText("Time:");
273 if (simulator.getReplication() != null)
274 {
275 this.notify(new Event(
276 SimulatorInterface.START_REPLICATION_EVENT,
277 simulator, simulator.getReplication()));
278 }
279 this.validate();
280 this.repaint();
281 }
282 return;
283 }
284 }
285 if (event.getSource() instanceof SimulatorInterface)
286 {
287 if (event.getType().equals(
288 SimulatorInterface.START_REPLICATION_EVENT))
289 {
290 this.units = this.application.getExperiment().getSimulator()
291 .getReplication().getRunControl().getTreatment()
292 .getTimeUnit().toString();
293 int rep = ((SimulatorInterface) event.getSource())
294 .getReplication().getNumber() + 1;
295 int nrrep = ((SimulatorInterface) event.getSource())
296 .getReplication().getRunControl().getReplications().length;
297 int treat = ((SimulatorInterface) event.getSource())
298 .getReplication().getRunControl().getTreatment()
299 .getNumber() + 1;
300 int nrtreat = ((SimulatorInterface) event.getSource())
301 .getReplication().getRunControl().getTreatment()
302 .getExperiment().getTreatments().length;
303 this.replicationField.setText("Rep(" + rep + "/" + nrrep + ")");
304 this.treatmentField.setText("Treat(" + treat + "/" + nrtreat
305 + ")");
306 this.validate();
307 this.repaint();
308 return;
309 }
310 if (event.getType().equals(SimulatorInterface.TIME_CHANGED_EVENT))
311 {
312 this.simulatorTime = ((Double) event.getContent())
313 .doubleValue();
314 }
315 }
316 }
317
318 /***
319 * The TimeUpdater
320 */
321 private class TimeUpdater extends Thread
322 {
323
324 /*** formatter */
325 private NumberFormat formatter = NumberFormat.getInstance();
326
327 /*** the last length of the string */
328 private int maxLength = 0;
329
330 /*** the source */
331 private Statusbar source = null;
332
333 /***
334 * constructs a new
335 *
336 * @param source the statusbar
337 */
338 public TimeUpdater(final Statusbar source)
339 {
340 super();
341 this.source = source;
342 }
343
344 /***
345 * @see java.lang.Runnable#run()
346 */
347 public void run()
348 {
349 while (true)
350 {
351 try
352 {
353 String formattedTime = "Time:"
354 + this.formatter.format(this.source
355 .getSimulatorTime()) + " "
356 + this.source.getUnits();
357 this.source.getTimeField().setText(formattedTime);
358 if (formattedTime.length() > this.maxLength)
359 {
360 this.maxLength = formattedTime.length();
361 this.source.validate();
362 }
363 Thread.sleep(250);
364 } catch (Throwable throwable)
365 {
366 throwable = null;
367 }
368 }
369 }
370 }
371 }