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 2003 <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/gpl.html">General Public
28 * License (GPL) </a>, no warranty <br>
29 *
30 * @version 1.0 18.10.2003 <br>
31 * @author <a href="mailto:nlang@fbk.eur.nl">Niels Lang </a>, <a
32 * href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
33 * Jacobs </a>
34 */
35 public class PauseAction extends SimulatorAction
36 {
37 /***
38 * The pauseAction schedules a pause
39 *
40 * @param application the application
41 */
42 public PauseAction(final DSOLApplicationInterface application)
43 {
44 super("Pause Simulator At...", application);
45 this.putValue(Action.MNEMONIC_KEY, new Integer('s'));
46 this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('P',
47 InputEvent.ALT_DOWN_MASK));
48 this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass()
49 .getResource("/toolbarButtonGraphics/media/Pause16.gif")));
50 }
51
52 /***
53 * notifyPaused
54 */
55 protected void notifyStopped()
56 {
57 try
58 {
59 this.simulator.stop();
60 JOptionPane
61 .showMessageDialog(null, "Pausing simulation @ "
62 + this.simulator.getSimulatorTime()
63 + " on user's request.");
64 } catch (Exception exception)
65 {
66 Logger.warning(this, "notifyStopped", exception);
67 }
68 }
69
70 /***
71 * @see java.awt.event.ActionListener
72 * #actionPerformed(java.awt.event.ActionEvent)
73 */
74 public void actionPerformed(final ActionEvent evt)
75 {
76 String result = JOptionPane.showInputDialog("Enter desired pause-time");
77 if (result != null)
78 {
79 try
80 {
81 double value = Double.parseDouble(result);
82 if (value > this.simulator.getSimulatorTime()
83 || (new Double(this.simulator.getSimulatorTime())
84 .isNaN() && value >= 0.0))
85 {
86 ((DEVSSimulatorInterface) this.simulator)
87 .scheduleEvent(new SimEvent(value,
88 SimEventInterface.MIN_PRIORITY, this, this,
89 "notifyStopped", null));
90 JOptionPane.showMessageDialog(null,
91 "Succesfully scheduled pause @ " + value);
92 } else
93 {
94 throw new Exception();
95 }
96 } catch (Exception exception)
97 {
98 try
99 {
100 JOptionPane.showMessageDialog(null, "Invalid input: "
101 + result
102 + ". Value should be numerical and larger than "
103 + "simulator time:"
104 + this.simulator.getSimulatorTime());
105 } catch (RemoteException remoteException)
106 {
107 Logger.warning(this, "actionPerformed", remoteException);
108 }
109 }
110 }
111 }
112 }