View Javadoc

    1   /*
    2    * @(#) OpenURLAction.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.net.URL;
    14  import java.rmi.RemoteException;
    15
    16  import javax.swing.AbstractAction;
    17  import javax.swing.JOptionPane;
    18
    19  import nl.tudelft.simulation.dsol.experiment.Experiment;
    20  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
    21  import nl.tudelft.simulation.dsol.gui.panels.GUIExperimentParsingThread;
    22  import nl.tudelft.simulation.event.Event;
    23  import nl.tudelft.simulation.event.EventInterface;
    24  import nl.tudelft.simulation.event.EventListenerInterface;
    25  import nl.tudelft.simulation.logger.Logger;
    26  import nl.tudelft.simulation.xml.dsol.ExperimentParsingThread;
    27
    28  /***
    29   * @author peter
    30   */
    31  public class OpenRecentAction extends AbstractAction implements
    32  		EventListenerInterface
    33  {
    34
    35  	/*** the parent */
    36  	private DSOLApplicationInterface application = null;
    37
    38  	/*** the url */
    39  	private String url = null;
    40
    41  	/***
    42  	 * constructs a new OpenURLAction
    43  	 *
    44  	 * @param application the parent application
    45  	 * @param url the recent url
    46  	 */
    47  	public OpenRecentAction(final DSOLApplicationInterface application,
    48  			final String url)
    49  	{
    50  		super(url);
    51  		this.url = url;
    52  		this.application = application;
    53  		this.setEnabled(false);
    54  		try
    55  		{
    56  			this.application.addListener(this,
    57  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    58  			this.notify(new Event(
    59  					DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT,
    60  					this.application, this.application.getExperiment()));
    61  		} catch (RemoteException exception)
    62  		{
    63  			Logger.warning(this, "OpenRecentAction", exception);
    64  		}
    65  	}
    66
    67  	/***
    68  	 * @see nl.tudelft.simulation.event.EventListenerInterface
    69  	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    70  	 */
    71  	public void notify(final EventInterface event)
    72  	{
    73  		if (event.getSource().equals(this.application)
    74  				&& event.getType().equals(
    75  						DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT))
    76  		{
    77  			if (event.getContent() == null)
    78  			{
    79  				this.setEnabled(true);
    80  			} else
    81  			{
    82  				this.setEnabled(false);
    83  			}
    84  		}
    85  		if (event.getType().equals(
    86  				ExperimentParsingThread.EXPERIMENT_PARSED_EVENT))
    87  		{
    88  			this.application.setExperiment((Experiment) event.getContent());
    89  		}
    90  	}
    91
    92  	/***
    93  	 * @see java.lang.Object#finalize()
    94  	 */
    95  	public void finalize() throws Throwable
    96  	{
    97  		this.application.removeListener(this,
    98  				DSOLApplicationInterface.EXPERIMENT_CHANGED_EVENT);
    99  		super.finalize();
    100 	}
    101
    102 	/***
    103 	 * @see java.awt.event.ActionListener
    104 	 *      #actionPerformed(java.awt.event.ActionEvent)
    105 	 */
    106 	public void actionPerformed(final ActionEvent event)
    107 	{
    108 		try
    109 		{
    110 			new GUIExperimentParsingThread(this, null, new URL(this.url))
    111 					.start();
    112 		} catch (Exception exception)
    113 		{
    114 			JOptionPane.showMessageDialog(null,
    115 					exception.getClass().getName()
    116 							.substring(
    117 									exception.getClass().getName().lastIndexOf(
    118 											".") + 1)
    119 							+ " occurred while connecting " + this.url + ".\n",
    120 					"Error", JOptionPane.ERROR_MESSAGE);
    121 		}
    122 	}
    123 }