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 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 AnimationDelaySlider extends JSlider implements
40 EventListenerInterface, ChangeListener
41 {
42 /*** the animator */
43 private AnimatorInterface 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 AnimationDelaySlider(final DSOLApplicationInterface application)
54 {
55 super(0, 3000, 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, "AnimationDelaySlider", 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 = Math.pow(10, (double) slider.getValue() / 1000) - 1;
92 if (shoudPause)
93 {
94 this.simulator.stop();
95 }
96 this.simulator.setAnimationDelay(Math.round(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 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT)
117 && event.getSource() instanceof SimulatorInterface)
118 {
119 this.setValue((int) Math.round((Math
120 .log(((Long) event.getContent()).longValue()) / Math
121 .log(10)) * 1000));
122 }
123 if (event.getType().equals(
124 DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
125 && event.getSource() instanceof DSOLApplicationInterface)
126 {
127 if (event.getContent() != null)
128 {
129 Experiment experiment = (Experiment) event.getContent();
130 experiment
131 .addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
132 this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
133 experiment, experiment.getSimulator()));
134 }
135 return;
136 }
137 if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
138 && event.getSource() instanceof Experiment)
139 {
140 if (event.getContent() != null)
141 {
142 if (event.getContent() instanceof AnimatorInterface)
143 {
144 this.simulator = (AnimatorInterface) event.getContent();
145 this.notify(new Event(
146 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT,
147 this.simulator, new Long(this.simulator
148 .getAnimationDelay())));
149 this.simulator.addListener(this,
150 AnimatorInterface.ANIMATION_DELAY_CHANGED_EVENT);
151 this.setEnabled((true));
152 }
153 }
154 }
155 }
156 }