1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.dsol.gui.actions;
11
12 import java.rmi.RemoteException;
13
14 import javax.swing.JSlider;
15 import javax.swing.event.ChangeEvent;
16 import javax.swing.event.ChangeListener;
17
18 import nl.tudelft.simulation.dsol.experiment.Experiment;
19 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
20 import nl.tudelft.simulation.dsol.simulators.DESSSimulatorInterface;
21 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22 import nl.tudelft.simulation.event.Event;
23 import nl.tudelft.simulation.event.EventInterface;
24 import nl.tudelft.simulation.event.EventListenerInterface;
25 import nl.tudelft.simulation.logger.Logger;
26
27 /***
28 * The TimestepSliderAction <br>
29 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
30 * University of Technology </a>, the Netherlands. <br>
31 * See for project information <a
32 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
33 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
34 * General Public License (LGPL) </a>, no warranty.
35 *
36 * @version $Revision$ $Date$
37 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
38 */
39 public class TimeStepSlider extends JSlider implements EventListenerInterface,
40 ChangeListener
41 {
42 /*** the animator */
43 private DESSSimulatorInterface simulator;
44
45 /*** fired */
46 private boolean fired = false;
47
48 /***
49 * constructs a new ShowGridAction
50 *
51 * @param application the application
52 */
53 public TimeStepSlider(final DSOLApplicationInterface application)
54 {
55 super(0, 700, 100);
56 this.setEnabled(false);
57 this.addChangeListener(this);
58 try
59 {
60 application.addListener(this,
61 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
62 this.notify(new Event(
63 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
64 application, application.getExperiment()));
65 } catch (RemoteException exception)
66 {
67 Logger.warning(this, "TimeStepSlider", exception);
68 }
69 }
70
71 /***
72 * @see javax.swing.event.ChangeListener
73 * #stateChanged(javax.swing.event.ChangeEvent)
74 */
75 public void stateChanged(final ChangeEvent event)
76 {
77 if (this.simulator == null)
78 {
79 return;
80 }
81 if (this.fired)
82 {
83 this.fired = false;
84 return;
85 }
86 this.fired = true;
87 try
88 {
89 boolean shoudPause = this.simulator.isRunning();
90 JSlider slider = (JSlider) event.getSource();
91 double value = this.getTimeStep(slider.getValue());
92 if (shoudPause)
93 {
94 this.simulator.stop();
95 }
96 this.simulator.setTimeStep(0.01 * value);
97 if (shoudPause)
98 {
99 this.simulator.start();
100 }
101 this.validate();
102 this.repaint();
103 } catch (Exception exception)
104 {
105 Logger.warning(this, "stateChanged", exception);
106 }
107 }
108
109 /***
110 * @see nl.tudelft.simulation.event.EventListenerInterface
111 * #notify(nl.tudelft.simulation.event.EventInterface)
112 */
113 public void notify(final EventInterface event) throws RemoteException
114 {
115 if (event.getType().equals(
116 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT)
117 && event.getSource() instanceof SimulatorInterface)
118 {
119 this.setValue(this.getSliderValue(((Double) event.getContent())
120 .doubleValue()));
121 }
122 if (event.getType().equals(
123 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
124 && event.getSource() instanceof DSOLApplicationInterface)
125 {
126 if (event.getContent() != null)
127 {
128 Experiment experiment = (Experiment) event.getContent();
129 experiment
130 .addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
131 this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
132 experiment, experiment.getSimulator()));
133 }
134 return;
135 }
136 if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
137 && event.getSource() instanceof Experiment)
138 {
139 if (event.getContent() != null)
140 {
141 if (event.getContent() instanceof DESSSimulatorInterface)
142 {
143 this.simulator = (DESSSimulatorInterface) event
144 .getContent();
145 this.notify(new Event(
146 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT,
147 this.simulator, new Double(this.simulator
148 .getTimeStep())));
149 this.simulator.addListener(this,
150 DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT);
151 this.setEnabled((true));
152 }
153 }
154 }
155 }
156
157 /***
158 * returns the sliderValue based on the timeStep
159 *
160 * @param timeStep the simulator timeStep
161 * @return the sliderValue
162 */
163 public int getSliderValue(final double timeStep)
164 {
165 int sliderValue = (int) (600 + Math.round(100 * Math.log(timeStep)
166 / Math.log(10)));
167 return sliderValue;
168 }
169
170 /***
171 * gets the TimeStep value based on the slider value
172 *
173 * @param value the slider value
174 * @return the corresponding timeStep value
175 */
176 public double getTimeStep(final int value)
177 {
178 double timeStep = Math.pow(10, (value - 600) / 100.0);
179 return timeStep;
180 }
181 }