View Javadoc

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