View Javadoc

    1   /*
    2    * @(#) OpenFileAction.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.menu.actions;
    11
    12  import java.awt.event.ActionEvent;
    13  import java.awt.event.InputEvent;
    14  import java.rmi.RemoteException;
    15
    16  import javax.swing.AbstractAction;
    17  import javax.swing.Action;
    18  import javax.swing.ImageIcon;
    19  import javax.swing.KeyStroke;
    20
    21  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
    22  import nl.tudelft.simulation.dsol.gui.windows.FileChooserFrame;
    23  import nl.tudelft.simulation.event.Event;
    24  import nl.tudelft.simulation.event.EventInterface;
    25  import nl.tudelft.simulation.event.EventListenerInterface;
    26  import nl.tudelft.simulation.logger.Logger;
    27
    28  /***
    29   * The OpenFile action <br>
    30   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    31   * University of Technology </a>, the Netherlands. <br>
    32   * See for project information <a
    33   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    34   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    35   * General Public License (LGPL) </a>, no warranty.
    36   *
    37   * @version $Revision$ $Date$
    38   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
    39   */
    40
    41  public class OpenFileAction extends AbstractAction implements
    42  		EventListenerInterface
    43  {
    44  	/*** the parent */
    45  	private DSOLApplicationInterface application = null;
    46
    47  	/***
    48  	 * constructs a new OpenFileAction
    49  	 *
    50  	 * @param application the parent application
    51  	 */
    52  	public OpenFileAction(final DSOLApplicationInterface application)
    53  	{
    54  		super("Open File");
    55  		this.application = application;
    56  		this.putValue(Action.MNEMONIC_KEY, new Integer('O'));
    57  		this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('O',
    58  				InputEvent.CTRL_DOWN_MASK));
    59  		this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass()
    60  				.getResource("/toolbarButtonGraphics/general/Open16.gif")));
    61  		this.setEnabled(false);
    62  		try
    63  		{
    64  			this.application.addListener(this,
    65  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    66  			this.notify(new Event(
    67  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
    68  					this.application, this.application.getExperiment()));
    69  		} catch (RemoteException exception)
    70  		{
    71  			Logger.warning(this, "OpenFileAction", exception);
    72  		}
    73  	}
    74
    75  	/***
    76  	 * @see java.awt.event.ActionListener
    77  	 *      #actionPerformed(java.awt.event.ActionEvent)
    78  	 */
    79  	public void actionPerformed(final ActionEvent event)
    80  	{
    81  		new FileChooserFrame(this.application, "/");
    82  	}
    83
    84  	/***
    85  	 * @see nl.tudelft.simulation.event.EventListenerInterface
    86  	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    87  	 */
    88  	public void notify(final EventInterface event)
    89  	{
    90  		if (event.getSource().equals(this.application)
    91  				&& event.getType().equals(
    92  						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
    93  		{
    94  			if (event.getContent() == null)
    95  			{
    96  				this.setEnabled(true);
    97  			} else
    98  			{
    99  				this.setEnabled(false);
    100 			}
    101 		}
    102 	}
    103
    104 	/***
    105 	 * @see java.lang.Object#finalize()
    106 	 */
    107 	protected void finalize() throws Throwable
    108 	{
    109 		this.application.removeListener(this,
    110 				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    111 		super.finalize();
    112 	}
    113 }