1
2
3
4 package nl.tudelft.simulation.dsol.gui.actions;
5
6 import java.awt.event.ActionEvent;
7 import java.awt.event.InputEvent;
8 import java.rmi.RemoteException;
9
10 import javax.swing.Action;
11 import javax.swing.ImageIcon;
12 import javax.swing.JOptionPane;
13 import javax.swing.KeyStroke;
14
15 import nl.tudelft.simulation.dsol.formalisms.devs.SimEvent;
16 import nl.tudelft.simulation.dsol.formalisms.devs.SimEventInterface;
17 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
18 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
19 import nl.tudelft.simulation.logger.Logger;
20
21 /***
22 * The SmoothWorker <br>
23 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
24 * University of Technology </a>, the Netherlands. <br>
25 * See for project information <a
26 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
27 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
28 * General Public License (LGPL) </a>, no warranty.
29 *
30 * @version $Revision$ $Date$
31 * @author <a href="mailto:nlang@fbk.eur.nl">Niels Lang </a>, <a
32 * href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
33 */
34 public class PauseAction extends SimulatorAction
35 {
36 /***
37 * The pauseAction schedules a pause
38 *
39 * @param application the application
40 */
41 public PauseAction(final DSOLApplicationInterface application)
42 {
43 super("Pause Simulator At...", application);
44 this.putValue(Action.MNEMONIC_KEY, new Integer('s'));
45 this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('P',
46 InputEvent.ALT_DOWN_MASK));
47 this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass()
48 .getResource("/toolbarButtonGraphics/media/Pause16.gif")));
49 }
50
51 /***
52 * notifyPaused
53 */
54 protected void notifyStopped()
55 {
56 try
57 {
58 this.simulator.stop();
59 JOptionPane
60 .showMessageDialog(null, "Pausing simulation @ "
61 + this.simulator.getSimulatorTime()
62 + " on user's request.");
63 } catch (Exception exception)
64 {
65 Logger.warning(this, "notifyStopped", exception);
66 }
67 }
68
69 /***
70 * @see java.awt.event.ActionListener
71 * #actionPerformed(java.awt.event.ActionEvent)
72 */
73 public void actionPerformed(final ActionEvent evt)
74 {
75 String result = JOptionPane.showInputDialog("Enter desired pause-time");
76 if (result != null)
77 {
78 try
79 {
80 double value = Double.parseDouble(result);
81 if (value > this.simulator.getSimulatorTime()
82 || (new Double(this.simulator.getSimulatorTime())
83 .isNaN() && value >= 0.0))
84 {
85 ((DEVSSimulatorInterface) this.simulator)
86 .scheduleEvent(new SimEvent(value,
87 SimEventInterface.MIN_PRIORITY, this, this,
88 "notifyStopped", null));
89 JOptionPane.showMessageDialog(null,
90 "Succesfully scheduled pause @ " + value);
91 } else
92 {
93 throw new Exception();
94 }
95 } catch (Exception exception)
96 {
97 try
98 {
99 JOptionPane.showMessageDialog(null, "Invalid input: "
100 + result
101 + ". Value should be numerical and larger than "
102 + "simulator time:"
103 + this.simulator.getSimulatorTime());
104 } catch (RemoteException remoteException)
105 {
106 Logger.warning(this, "actionPerformed", remoteException);
107 }
108 }
109 }
110 }
111 }