View Javadoc

1   /*
2    * @(#) OpenFileAction.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.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 2003 <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/gpl.html">General Public
35   * License (GPL) </a>, no warranty <br>
36   * 
37   * @version 1.0 18.10.2003 <br>
38   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
39   *         Jacobs </a>
40   */
41  
42  public class OpenFileAction extends AbstractAction implements
43  		EventListenerInterface
44  {
45  	/*** the parent */
46  	private DSOLApplicationInterface application = null;
47  
48  	/***
49  	 * constructs a new OpenFileAction
50  	 * 
51  	 * @param application the parent application
52  	 */
53  	public OpenFileAction(final DSOLApplicationInterface application)
54  	{
55  		super("Open File");
56  		this.application = application;
57  		this.putValue(Action.MNEMONIC_KEY, new Integer('O'));
58  		this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('O',
59  				InputEvent.CTRL_DOWN_MASK));
60  		this.putValue(Action.SMALL_ICON, new ImageIcon(this.getClass()
61  				.getResource("/toolbarButtonGraphics/general/Open16.gif")));
62  		this.setEnabled(false);
63  		try
64  		{
65  			this.application.addListener(this,
66  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
67  			this.notify(new Event(
68  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
69  					this.application, this.application.getExperiment()));
70  		} catch (RemoteException exception)
71  		{
72  			Logger.warning(this, "OpenFileAction", exception);
73  		}
74  	}
75  
76  	/***
77  	 * @see java.awt.event.ActionListener
78  	 *      #actionPerformed(java.awt.event.ActionEvent)
79  	 */
80  	public void actionPerformed(final ActionEvent event)
81  	{
82  		new FileChooserFrame(this.application, "/");
83  	}
84  
85  	/***
86  	 * @see nl.tudelft.simulation.event.EventListenerInterface
87  	 *      #notify(nl.tudelft.simulation.event.EventInterface)
88  	 */
89  	public void notify(final EventInterface event)
90  	{
91  		if (event.getSource().equals(this.application)
92  				&& event.getType().equals(
93  						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
94  		{
95  			if (event.getContent() == null)
96  			{
97  				this.setEnabled(true);
98  			} else
99  			{
100 				this.setEnabled(false);
101 			}
102 		}
103 	}
104 
105 	/***
106 	 * @see java.lang.Object#finalize()
107 	 */
108 	protected void finalize() throws Throwable
109 	{
110 		this.application.removeListener(this,
111 				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
112 		super.finalize();
113 	}
114 }