View Javadoc

1   /*
2    * @(#) ShowGridAction.java Oct 24, 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.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 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 TimeStepSlider extends JSlider implements EventListenerInterface,
41  		ChangeListener
42  {
43  	/*** the animator */
44  	private DESSSimulatorInterface 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 TimeStepSlider(final DSOLApplicationInterface application)
55  	{
56  		super(0, 700, 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, "TimeStepSlider", 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 = this.getTimeStep(slider.getValue());
93  			if (shoudPause)
94  			{
95  				this.simulator.stop();
96  			}
97  			this.simulator.setTimeStep(0.01 * 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 				DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT)
118 				&& event.getSource() instanceof SimulatorInterface)
119 		{
120 			this.setValue(this.getSliderValue(((Double) event.getContent())
121 					.doubleValue()));
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 DESSSimulatorInterface)
143 				{
144 					this.simulator = (DESSSimulatorInterface) event
145 							.getContent();
146 					this.notify(new Event(
147 							DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT,
148 							this.simulator, new Double(this.simulator
149 									.getTimeStep())));
150 					this.simulator.addListener(this,
151 							DESSSimulatorInterface.TIME_STEP_CHANGED_EVENT);
152 					this.setEnabled((true));
153 				}
154 			}
155 		}
156 	}
157 
158 	/***
159 	 * returns the sliderValue based on the timeStep
160 	 * 
161 	 * @param timeStep the simulator timeStep
162 	 * @return the sliderValue
163 	 */
164 	public int getSliderValue(final double timeStep)
165 	{
166 		int sliderValue = (int) (600 + Math.round(100 * Math.log(timeStep)
167 				/ Math.log(10)));
168 		return sliderValue;
169 	}
170 
171 	/***
172 	 * gets the TimeStep value based on the slider value
173 	 * 
174 	 * @param value the slider value
175 	 * @return the corresponding timeStep value
176 	 */
177 	public double getTimeStep(final int value)
178 	{
179 		double timeStep = Math.pow(10, (value - 600) / 100.0);
180 		System.out.println("timeStep" + timeStep);
181 		return timeStep;
182 	}
183 }