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.Font;
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16 import java.rmi.RemoteException;
17 import java.text.NumberFormat;
18
19 import javax.swing.BorderFactory;
20 import javax.swing.BoxLayout;
21 import javax.swing.JPanel;
22 import javax.swing.JTextField;
23
24 import nl.tudelft.simulation.dsol.experiment.Experiment;
25 import nl.tudelft.simulation.dsol.experiment.TimeUnitInterface;
26 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
27 import nl.tudelft.simulation.dsol.gui.actions.AnimationDelaySlider;
28 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
29 import nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface;
30 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
31 import nl.tudelft.simulation.event.Event;
32 import nl.tudelft.simulation.event.EventInterface;
33 import nl.tudelft.simulation.event.EventListenerInterface;
34 import nl.tudelft.simulation.logger.Logger;
35
36 /***
37 * A ControlPanel <br>
38 * (c) copyright 2002-2005 <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/lesser.html">Lesser
43 * General Public License (LGPL) </a>, no warranty.
44 *
45 * @version $Revision: 1.4 $ $Date: 2005/01/13 16:28:55 $
46 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
47 */
48 public class ControlPanel extends JPanel implements EventListenerInterface
49 {
50 /*** the timeStepSlider */
51
52 /*** the animationDelaySlider */
53 private AnimationDelaySlider animationDelaySlider;
54
55 /*** the animationDelayTextField */
56 private JTextField animationDelayTextField = null;
57
58 /*** the timeSteptextField */
59 protected JTextField timeStepTextField = null;
60
61 /*** timeStepPanel */
62 private JPanel timeStepPanel = new JPanel();
63
64 /*** the animationDelayFormatter */
65 private NumberFormat animationDelayFormatter = NumberFormat.getInstance();
66
67 /*** the DSOLApplication */
68 protected DSOLApplicationInterface application = null;
69
70 /***
71 * constructs a new ControlPanel
72 *
73 * @param application the application
74 */
75 public ControlPanel(final DSOLApplicationInterface application)
76 {
77 super();
78 this.application = application;
79 this.animationDelayFormatter.setMinimumFractionDigits(0);
80 this.animationDelayFormatter.setMaximumFractionDigits(0);
81 this.initialize();
82 try
83 {
84 this.application.addListener(this,
85 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
86 this.notify(new Event(
87 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
88 this.application, this.application.getExperiment()));
89 } catch (RemoteException exception)
90 {
91 Logger.warning(this, "ControlPanel", exception);
92 }
93 }
94
95 /***
96 * initializes the console
97 */
98 private void initialize()
99 {
100 this.setLayout(new BorderLayout());
101 this.setBorder(BorderFactory.createEmptyBorder());
102
103 JPanel content = new JPanel();
104 content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
105 content.setBorder(BorderFactory.createEmptyBorder());
106
107 this.timeStepPanel.setLayout(new BorderLayout());
108
109 String title = TimeUnitInterface.UNIT.toString();
110 this.timeStepPanel.setBorder(BorderFactory
111 .createTitledBorder("time step ( " + title
112 + " on simulation clock )"));
113
114 JTextField timeSteplabel = new JTextField("time step: ");
115 this.timeStepPanel.add(timeSteplabel, BorderLayout.WEST);
116
117 this.timeStepTextField = new JTextField();
118 this.timeStepTextField.setEditable(true);
119 Font font = this.timeStepTextField.getFont().deriveFont(Font.BOLD);
120 this.timeStepTextField.setFont(font);
121 this.timeStepTextField.addActionListener(new ActionListener()
122 {
123 public void actionPerformed(final ActionEvent e)
124 {
125 DESSSimulatorInterface simulator = (DESSSimulatorInterface) ControlPanel.this.application
126 .getExperiment().getSimulator();
127 try
128 {
129 double value = new Double(e.getActionCommand())
130 .doubleValue();
131 if (value <= 0)
132 {
133 throw new NumberFormatException("timeStep value <=0");
134 }
135 simulator.setTimeStep(value);
136 } catch (Exception numberFormatException)
137 {
138 Logger.warning(this, "timeStepEditor",
139 numberFormatException);
140
141 double value = DESSSimulatorInterface.DEFAULT_TIME_STEP;
142 try
143 {
144 value = simulator.getTimeStep();
145 } catch (RemoteException e1)
146 {
147 Logger.warning(this, "nonsense", e1);
148 }
149 ControlPanel.this.timeStepTextField.setText(value + "");
150 }
151 }
152 });
153 this.timeStepTextField.setBorder(BorderFactory.createEmptyBorder());
154 this.timeStepPanel.add(this.timeStepTextField, BorderLayout.CENTER);
155
156 content.add(this.timeStepPanel);
157
158 JPanel animationDelayPanel = new JPanel();
159 animationDelayPanel.setLayout(new BorderLayout());
160 animationDelayPanel
161 .setBorder(BorderFactory
162 .createTitledBorder("animation delay ( milliseconds on wall clock ) "));
163
164 this.animationDelaySlider = new AnimationDelaySlider(this.application);
165 this.animationDelaySlider.setName("animationDelaySlider");
166 this.animationDelaySlider.setBorder(BorderFactory.createEmptyBorder(0,
167 10, 0, 10));
168
169 animationDelayPanel.add(this.animationDelaySlider, BorderLayout.CENTER);
170
171 this.animationDelayTextField = new JTextField();
172 this.animationDelayTextField.setEditable(false);
173 this.animationDelayTextField.setBorder(BorderFactory
174 .createEmptyBorder());
175 animationDelayPanel
176 .add(this.animationDelayTextField, BorderLayout.EAST);
177
178 try
179 {
180 this.timeStepTextField
181 .setText(DESSSimulatorInterface.DEFAULT_TIME_STEP + "");
182 this.animationDelayTextField.setText(this.animationDelayFormatter
183 .format(AnimatorInterface.DEFAULT_ANIMATION_DELAY));
184 this.animationDelaySlider.setValue((int) Math.round((Math
185 .log(AnimatorInterface.DEFAULT_ANIMATION_DELAY) / Math
186 .log(10)) * 1000));
187 } catch (Exception exception)
188 {
189 Logger.warning(this, "initialize", exception);
190 }
191 content.add(animationDelayPanel);
192 this.add(content);
193 }
194
195 /***
196 * @see nl.tudelft.simulation.event.EventListenerInterface
197 * #notify(nl.tudelft.simulation.event.EventInterface)
198 */
199 public void notify(final EventInterface event) throws RemoteException
200 {
201 if (event.getType().equals(
202 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
203 && event.getSource() instanceof DSOLApplicationInterface)
204 {
205 if (event.getContent() != null)
206 {
207 Experiment experiment = (Experiment) event.getContent();
208 experiment
209 .addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
210 this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
211 experiment, experiment.getSimulator()));
212
213 }
214 return;
215 }
216 if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
217 && event.getSource() instanceof Experiment)
218 {
219 if (event.getContent() != null)
220 {
221 AnimatorInterface simulator = (AnimatorInterface) event
222 .getContent();
223 simulator.addListener(this,
224 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT);
225 simulator.addListener(this,
226 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT);
227 simulator.addListener(this,
228 SimulatorInterface.START_REPLICATION_EVENT);
229 this.notify(new Event(
230 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT,
231 simulator, new Long(simulator.getAnimationDelay())));
232 this.notify(new Event(
233 SimulatorInterface.START_REPLICATION_EVENT, simulator,
234 null));
235 this.notify(new Event(
236 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT,
237 simulator, new Double(simulator.getTimeStep())));
238 }
239 return;
240 }
241 if (event.getSource() instanceof SimulatorInterface
242 && event.getType().equals(
243 SimulatorInterface.START_REPLICATION_EVENT))
244 {
245 SimulatorInterface simulator = (SimulatorInterface) event
246 .getSource();
247 if (simulator.getReplication() != null)
248 {
249 this.timeStepPanel
250 .setBorder(BorderFactory
251 .createTitledBorder("time step ( "
252 + simulator.getReplication()
253 .getRunControl().getTreatment()
254 .getTimeUnit().toString()
255 + " on simulation clock )"));
256 this.validate();
257 this.repaint();
258 return;
259 }
260 }
261 if (event.getSource() instanceof AnimatorInterface)
262 {
263 if (event.getType().equals(
264 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT))
265 {
266 this.animationDelayTextField
267 .setText(this.animationDelayFormatter
268 .format(((Long) event.getContent()).longValue()));
269 }
270 if (event.getType().equals(
271 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT))
272 {
273 this.timeStepTextField.setText(event.getContent().toString());
274 }
275 this.validate();
276 this.repaint();
277 return;
278 }
279 }
280 }