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.AnimatorInterface;
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 AnimationDelaySlider <br>
29 * (c) copyright 2003 <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/gpl.html">General Public
34 * License (GPL) </a>, no warranty <br>
35 *
36 * @version 1.0 18.10.2003 <br>
37 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
38 * Jacobs </a>
39 */
40 public class AnimationDelaySlider extends JSlider implements
41 EventListenerInterface, ChangeListener
42 {
43 /*** the animator */
44 private AnimatorInterface simulator;
45
46 /*** fired */
47 private boolean fired = false;
48
49 /***
50 * constructs a new ShowGridAction
51 *
52 * @param application the application
53 */
54 public AnimationDelaySlider(final DSOLApplicationInterface application)
55 {
56 super(0, 3000, 100);
57 this.setEnabled(false);
58 this.addChangeListener(this);
59 try
60 {
61 application.addListener(this,
62 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
63 this.notify(new Event(
64 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
65 application, application.getExperiment()));
66 } catch (RemoteException exception)
67 {
68 Logger.warning(this, "AnimationDelaySlider", exception);
69 }
70 }
71
72 /***
73 * @see javax.swing.event.ChangeListener
74 * #stateChanged(javax.swing.event.ChangeEvent)
75 */
76 public void stateChanged(final ChangeEvent event)
77 {
78 if (this.simulator == null)
79 {
80 return;
81 }
82 if (this.fired)
83 {
84 this.fired = false;
85 return;
86 }
87 this.fired = true;
88 try
89 {
90 boolean shoudPause = this.simulator.isRunning();
91 JSlider slider = (JSlider) event.getSource();
92 double value = Math.pow(10, (double) slider.getValue() / 1000) - 1;
93 if (shoudPause)
94 {
95 this.simulator.stop();
96 }
97 this.simulator.setAnimationDelay(Math.round(value));
98 if (shoudPause)
99 {
100 this.simulator.start();
101 }
102 this.validate();
103 this.repaint();
104 } catch (Exception exception)
105 {
106 Logger.warning(this, "stateChanged", exception);
107 }
108 }
109
110 /***
111 * @see nl.tudelft.simulation.event.EventListenerInterface
112 * #notify(nl.tudelft.simulation.event.EventInterface)
113 */
114 public void notify(final EventInterface event) throws RemoteException
115 {
116 if (event.getType().equals(
117 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT)
118 && event.getSource() instanceof SimulatorInterface)
119 {
120 this.setValue((int) Math.round((Math
121 .log(((Long) event.getContent()).longValue()) / Math
122 .log(10)) * 1000));
123 }
124 if (event.getType().equals(
125 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
126 && event.getSource() instanceof DSOLApplicationInterface)
127 {
128 if (event.getContent() != null)
129 {
130 Experiment experiment = (Experiment) event.getContent();
131 experiment
132 .addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
133 this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
134 experiment, experiment.getSimulator()));
135 }
136 return;
137 }
138 if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
139 && event.getSource() instanceof Experiment)
140 {
141 if (event.getContent() != null)
142 {
143 if (event.getContent() instanceof AnimatorInterface)
144 {
145 this.simulator = (AnimatorInterface) event.getContent();
146 this.notify(new Event(
147 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT,
148 this.simulator, new Long(this.simulator
149 .getAnimationDelay())));
150 this.simulator.addListener(this,
151 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT);
152 this.setEnabled((true));
153 }
154 }
155 }
156 }
157 }