View Javadoc

1   /*
2    * @(#) FileMenu.java Nov 5, 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;
11  
12  import java.rmi.RemoteException;
13  
14  import javax.swing.JMenu;
15  import javax.swing.JMenuItem;
16  import javax.swing.JSeparator;
17  
18  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
19  import nl.tudelft.simulation.dsol.gui.menu.actions.CloseExperimentAction;
20  import nl.tudelft.simulation.dsol.gui.menu.actions.ExitAction;
21  import nl.tudelft.simulation.dsol.gui.menu.actions.OpenFileAction;
22  import nl.tudelft.simulation.dsol.gui.menu.actions.OpenRecentAction;
23  import nl.tudelft.simulation.dsol.gui.menu.actions.OpenURLAction;
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 File Menu <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  public class FileMenu extends JMenu implements EventListenerInterface
42  {
43  	/*** application the application */
44  	private DSOLApplicationInterface application = null;
45  
46  	/***
47  	 * constructs a new FileMenu
48  	 * 
49  	 * @param application the application
50  	 */
51  	public FileMenu(final DSOLApplicationInterface application)
52  	{
53  		super("File");
54  		this.application = application;
55  		this.setMnemonic('F');
56  		try
57  		{
58  			this.application.addListener(this,
59  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
60  		} catch (RemoteException exception)
61  		{
62  			Logger.warning(this, "FileMenu", exception);
63  		}
64  
65  		this.initialize();
66  	}
67  
68  	/***
69  	 * initializes the menu
70  	 */
71  	private void initialize()
72  	{
73  		JMenuItem openFileItem = new JMenuItem(new OpenFileAction(
74  				this.application));
75  		this.add(openFileItem);
76  
77  		JMenuItem openURLItem = new JMenuItem(new OpenURLAction(
78  				this.application));
79  		this.add(openURLItem);
80  
81  		JMenuItem closeURLItem = new JMenuItem(new CloseExperimentAction(
82  				this.application));
83  		this.add(closeURLItem);
84  
85  		this.add(new JSeparator());
86  		this.add(this.createRecent());
87  		this.add(new JSeparator());
88  
89  		JMenuItem exitItem = new JMenuItem(new ExitAction(this.application));
90  		this.add(exitItem);
91  	}
92  
93  	/***
94  	 * creates a recent menu
95  	 * 
96  	 * @return JMenu
97  	 */
98  	private JMenu createRecent()
99  	{
100 		JMenu recent = new JMenu("Open Recent");
101 		for (int i = 0; i < 3; i++)
102 		{
103 			String label = this.application.getProperties().getProperty(
104 					"recent[" + i + "]");
105 			if (label != null && label.length() > 0)
106 			{
107 				JMenuItem recentItem = new JMenuItem(new OpenRecentAction(
108 						this.application, label.split("!#!")[0]));
109 				recent.add(recentItem);
110 			}
111 		}
112 		return recent;
113 	}
114 
115 	/***
116 	 * @see nl.tudelft.simulation.event.EventListenerInterface
117 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
118 	 */
119 	public void notify(final EventInterface event)
120 	{
121 		if (event.getSource().equals(this.application)
122 				&& event.getType().equals(
123 						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
124 		{
125 			this.remove(4);
126 			this.add(this.createRecent(), 4);
127 		}
128 	}
129 }