View Javadoc

1   /*
2    * @(#) DEVSSimulatorAction.java Oct 23, 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.AbstractAction;
15  
16  import nl.tudelft.simulation.dsol.experiment.Experiment;
17  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19  import nl.tudelft.simulation.event.Event;
20  import nl.tudelft.simulation.event.EventInterface;
21  import nl.tudelft.simulation.event.EventListenerInterface;
22  import nl.tudelft.simulation.logger.Logger;
23  
24  /***
25   * The basic simulatorAction <br>
26   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
27   * University of Technology </a>, the Netherlands. <br>
28   * See for project information <a
29   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
30   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
31   * License (GPL) </a>, no warranty <br>
32   * 
33   * @version 1.0 18.10.2003 <br>
34   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
35   *         Jacobs </a>
36   */
37  public abstract class SimulatorAction extends AbstractAction implements
38  		EventListenerInterface
39  {
40  
41  	/***
42  	 * the simulator
43  	 * 
44  	 * @uml.property name="simulator"
45  	 */
46  	protected SimulatorInterface simulator = null;
47  
48  
49  	/*** the application */
50  	protected DSOLApplicationInterface application = null;
51  
52  	/***
53  	 * constructs a new DEVSSimulatorAction
54  	 * 
55  	 * @param name the name of the action
56  	 * @param application the application
57  	 */
58  	public SimulatorAction(final String name,
59  			final DSOLApplicationInterface application)
60  	{
61  		super(name);
62  		this.application = application;
63  		this.setEnabled(false);
64  		try
65  		{
66  			this.application.addListener(this,
67  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
68  			this.notify(new Event(
69  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
70  					this.application, this.application.getExperiment()));
71  		} catch (RemoteException exception)
72  		{
73  			Logger.warning(this, "SimulatorAction", exception);
74  		}
75  	}
76  
77  	/***
78  	 * @see java.lang.Object#finalize()
79  	 */
80  	protected void finalize() throws Throwable
81  	{
82  		this.application.removeListener(this,
83  				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
84  		if (this.simulator != null)
85  		{
86  			this.simulator.removeListener(this, SimulatorInterface.START_EVENT);
87  			this.simulator.removeListener(this, SimulatorInterface.STOP_EVENT);
88  		}
89  		super.finalize();
90  	}
91  
92  	/***
93  	 * sets the simulator
94  	 * 
95  	 * @param simulator the simulator
96  	 * 
97  	 * @uml.property name="simulator"
98  	 */
99  	protected void setSimulator(final SimulatorInterface simulator)
100 	{
101 		try
102 		{
103 			if (this.simulator != null)
104 			{
105 				simulator.removeListener(this, SimulatorInterface.START_EVENT);
106 				simulator.removeListener(this, SimulatorInterface.STOP_EVENT);
107 			}
108 			this.simulator = simulator;
109 			if (simulator == null)
110 			{
111 				this.setEnabled(false);
112 			} else
113 			{
114 				this.setEnabled(true);
115 				this.simulator
116 						.addListener(this, SimulatorInterface.START_EVENT);
117 				this.simulator.addListener(this, SimulatorInterface.STOP_EVENT);
118 			}
119 		} catch (Exception exception)
120 		{
121 			Logger.warning(this, "actionPerformed", exception);
122 		}
123 	}
124 
125 	/***
126 	 * @see nl.tudelft.simulation.event.EventListenerInterface
127 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
128 	 */
129 	public void notify(final EventInterface event)
130 	{
131 		if (event.getType().equals(
132 				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
133 				&& event.getSource() instanceof DSOLApplicationInterface)
134 		{
135 			if (event.getContent() != null)
136 			{
137 				Experiment experiment = (Experiment) event.getContent();
138 				experiment
139 						.addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
140 				experiment
141 						.addListener(this, Experiment.END_OF_EXPERIMENT_EVENT);
142 				this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
143 						experiment, experiment.getSimulator()));
144 			} else
145 			{
146 				this.setEnabled(false);
147 			}
148 			return;
149 		}
150 		if (event.getType().equals(Experiment.END_OF_EXPERIMENT_EVENT)
151 				&& event.getSource() instanceof Experiment)
152 		{
153 			this.setEnabled(false);
154 		}
155 		if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
156 				&& event.getSource() instanceof Experiment)
157 		{
158 			this.setSimulator((SimulatorInterface) event.getContent());
159 		}
160 	}
161 }