View Javadoc

    1   /*
    2    * @(#) DEVSSimulatorAction.java Oct 23, 2003
    3    *
    4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
    5    * Delft, the Netherlands. All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the Lesser 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 2002-2005 <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/lesser.html">Lesser
    31   * General Public License (LGPL) </a>, no warranty.
    32   *
    33   * @version $Revision$ $Date$
    34   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
    35   */
    36  public abstract class SimulatorAction extends AbstractAction implements
    37  		EventListenerInterface
    38  {
    39
    40  	/***
    41  	 * the simulator
    42  	 */
    43  	protected SimulatorInterface simulator = null;
    44
    45
    46  	/*** the application */
    47  	protected DSOLApplicationInterface application = null;
    48
    49  	/***
    50  	 * constructs a new DEVSSimulatorAction
    51  	 *
    52  	 * @param name the name of the action
    53  	 * @param application the application
    54  	 */
    55  	public SimulatorAction(final String name,
    56  			final DSOLApplicationInterface application)
    57  	{
    58  		super(name);
    59  		this.application = application;
    60  		this.setEnabled(false);
    61  		try
    62  		{
    63  			this.application.addListener(this,
    64  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    65  			this.notify(new Event(
    66  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
    67  					this.application, this.application.getExperiment()));
    68  		} catch (RemoteException exception)
    69  		{
    70  			Logger.warning(this, "SimulatorAction", exception);
    71  		}
    72  	}
    73
    74  	/***
    75  	 * @see java.lang.Object#finalize()
    76  	 */
    77  	protected void finalize() throws Throwable
    78  	{
    79  		this.application.removeListener(this,
    80  				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    81  		if (this.simulator != null)
    82  		{
    83  			this.simulator.removeListener(this, SimulatorInterface.START_EVENT);
    84  			this.simulator.removeListener(this, SimulatorInterface.STOP_EVENT);
    85  		}
    86  		super.finalize();
    87  	}
    88
    89  	/***
    90  	 * sets the simulator
    91  	 *
    92  	 * @param simulator the simulator
    93  	 */
    94  	protected void setSimulator(final SimulatorInterface simulator)
    95  	{
    96  		try
    97  		{
    98  			if (this.simulator != null)
    99  			{
    100 				simulator.removeListener(this, SimulatorInterface.START_EVENT);
    101 				simulator.removeListener(this, SimulatorInterface.STOP_EVENT);
    102 			}
    103 			this.simulator = simulator;
    104 			if (simulator == null)
    105 			{
    106 				this.setEnabled(false);
    107 			} else
    108 			{
    109 				this.setEnabled(true);
    110 				this.simulator
    111 						.addListener(this, SimulatorInterface.START_EVENT);
    112 				this.simulator.addListener(this, SimulatorInterface.STOP_EVENT);
    113 			}
    114 		} catch (Exception exception)
    115 		{
    116 			Logger.warning(this, "actionPerformed", exception);
    117 		}
    118 	}
    119
    120 	/***
    121 	 * @see nl.tudelft.simulation.event.EventListenerInterface
    122 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    123 	 */
    124 	public void notify(final EventInterface event)
    125 	{
    126 		if (event.getType().equals(
    127 				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT)
    128 				&& event.getSource() instanceof DSOLApplicationInterface)
    129 		{
    130 			if (event.getContent() != null)
    131 			{
    132 				Experiment experiment = (Experiment) event.getContent();
    133 				experiment
    134 						.addListener(this, Experiment.SIMULATOR_CHANGED_EVENT);
    135 				experiment
    136 						.addListener(this, Experiment.END_OF_EXPERIMENT_EVENT);
    137 				this.notify(new Event(Experiment.SIMULATOR_CHANGED_EVENT,
    138 						experiment, experiment.getSimulator()));
    139 			} else
    140 			{
    141 				this.setEnabled(false);
    142 			}
    143 			return;
    144 		}
    145 		if (event.getType().equals(Experiment.END_OF_EXPERIMENT_EVENT)
    146 				&& event.getSource() instanceof Experiment)
    147 		{
    148 			this.setEnabled(false);
    149 		}
    150 		if (event.getType().equals(Experiment.SIMULATOR_CHANGED_EVENT)
    151 				&& event.getSource() instanceof Experiment)
    152 		{
    153 			this.setSimulator((SimulatorInterface) event.getContent());
    154 		}
    155 	}
    156 }