View Javadoc

1   /*
2    * @(#) ViewContextAction.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.KeyEvent;
14  
15  import javax.naming.NamingException;
16  import javax.naming.event.EventContext;
17  import javax.swing.AbstractAction;
18  import javax.swing.Action;
19  import javax.swing.KeyStroke;
20  
21  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
22  import nl.tudelft.simulation.dsol.gui.windows.ContextFrame;
23  import nl.tudelft.simulation.logger.Logger;
24  import nl.tudelft.simulation.naming.InitialEventContext;
25  
26  /***
27   * The ViewContext action <br>
28   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
29   * University of Technology </a>, the Netherlands. <br>
30   * See for project information <a
31   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
32   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
33   * General Public License (LGPL) </a>, no warranty.
34   * 
35   * @version $Revision$ $Date$
36   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
37   */
38  public class ViewContextAction extends AbstractAction
39  {
40  	/*** the context */
41  	private final EventContext context;
42  
43  	/*** application the application */
44  	private DSOLApplicationInterface application = null;
45  
46  	/***
47  	 * constructs a new ViewContextAction
48  	 * 
49  	 * @param application the application
50  	 * @throws NamingException on failure
51  	 */
52  	public ViewContextAction(final DSOLApplicationInterface application)
53  			throws NamingException
54  	{
55  		this(new InitialEventContext(), application);
56  	}
57  
58  	/***
59  	 * constructs a new ViewContextAction
60  	 * 
61  	 * @param context the specific context to view
62  	 * @param application the application
63  	 */
64  	public ViewContextAction(final EventContext context,
65  			final DSOLApplicationInterface application)
66  	{
67  		super("Context");
68  		this.application = application;
69  		this.putValue(Action.MNEMONIC_KEY, new Integer('C'));
70  		this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
71  				KeyEvent.VK_F1, 0));
72  		this.context = context;
73  	}
74  
75  	/***
76  	 * @see java.awt.event.ActionListener
77  	 *      #actionPerformed(java.awt.event.ActionEvent)
78  	 */
79  	public void actionPerformed(final ActionEvent e)
80  	{
81  		try
82  		{
83  			boolean running = false;
84  			try
85  			{
86  				if (this.application.getExperiment().getSimulator().isRunning())
87  				{
88  					running = true;
89  					this.application.getExperiment().getSimulator().stop();
90  				}
91  			} catch (NullPointerException nullPointerException)
92  			{
93  				// This was meant to happen
94  				nullPointerException = null;
95  			}
96  			new ContextFrame(this.context);
97  			if (running)
98  			{
99  				this.application.getExperiment().getSimulator().start();
100 			}
101 		} catch (Exception exception)
102 		{
103 			Logger.warning(this, "actionPerformed", exception);
104 		}
105 	}
106 }