View Javadoc

    1   /*
    2    * @(#) FileChooserFrame.java Oct 13, 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.windows;
    11
    12  import java.awt.BorderLayout;
    13  import java.awt.event.ActionEvent;
    14  import java.awt.event.ActionListener;
    15  import java.io.File;
    16  import java.net.MalformedURLException;
    17
    18  import javax.swing.JFileChooser;
    19  import javax.swing.JFrame;
    20  import javax.swing.filechooser.FileFilter;
    21
    22  import nl.tudelft.simulation.dsol.experiment.Experiment;
    23  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
    24  import nl.tudelft.simulation.dsol.gui.panels.GUIExperimentParsingThread;
    25  import nl.tudelft.simulation.event.EventInterface;
    26  import nl.tudelft.simulation.event.EventListenerInterface;
    27  import nl.tudelft.simulation.logger.Logger;
    28
    29  /***
    30   * The FileChooserFrame <br>
    31   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    32   * University of Technology </a>, the Netherlands. <br>
    33   * See for project information <a
    34   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    35   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    36   * General Public License (LGPL) </a>, no warranty.
    37   *
    38   * @version $Revision$ $Date$
    39   * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja">Peter Jacobs </a>
    40   */
    41  public class FileChooserFrame extends JFrame implements ActionListener,
    42  		EventListenerInterface
    43  {
    44
    45  	/*** source owns the fileChooser */
    46  	private DSOLApplicationInterface application = null;
    47
    48  	/***
    49  	 * constructs a new FileChooserFrame
    50  	 *
    51  	 * @param application the application
    52  	 * @param directory the default directory
    53  	 */
    54  	public FileChooserFrame(final DSOLApplicationInterface application,
    55  			final String directory)
    56  	{
    57  		super("open experiment");
    58  		this.setLocation(75, 50);
    59  		this.application = application;
    60  		this.initialize(directory);
    61  		this.pack();
    62  		this.setVisible(true);
    63  	}
    64
    65  	/***
    66  	 * initializes the fileChooser
    67  	 *
    68  	 * @param directory the directory
    69  	 */
    70  	private void initialize(final String directory)
    71  	{
    72  		JFileChooser fileChooser = new JFileChooser(directory);
    73  		fileChooser.addChoosableFileFilter(new FileFilter()
    74  		{
    75
    76  			public boolean accept(final File file)
    77  			{
    78  				if (file.getName().endsWith("xml") || file.isDirectory())
    79  				{
    80  					return true;
    81  				}
    82  				return false;
    83  			}
    84
    85  			public String getDescription()
    86  			{
    87  				return ".xml experiment files";
    88  			}
    89  		});
    90  		fileChooser.addActionListener(this);
    91  		this.getContentPane().add(fileChooser, BorderLayout.CENTER);
    92  	}
    93
    94  	/***
    95  	 * @see java.awt.event.ActionListener
    96  	 *      #actionPerformed(java.awt.event.ActionEvent)
    97  	 */
    98  	public void actionPerformed(final ActionEvent actionEvent)
    99  	{
    100 		if (!actionEvent.getActionCommand().equals("CancelSelection"))
    101 		{
    102 			File selectedFile = ((JFileChooser) actionEvent.getSource())
    103 					.getSelectedFile();
    104 			try
    105 			{
    106 				this.dispose();
    107 				new GUIExperimentParsingThread(this, this, selectedFile.toURL())
    108 						.start();
    109 			} catch (MalformedURLException e)
    110 			{
    111 				Logger.warning(this, "actionPerformed", e);
    112 			}
    113 		} else
    114 		{
    115 			this.dispose();
    116 		}
    117 	}
    118
    119 	/***
    120 	 * @see nl.tudelft.simulation.event.EventListenerInterface
    121 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
    122 	 */
    123 	public void notify(final EventInterface event)
    124 	{
    125 		this.application.setExperiment((Experiment) event.getContent());
    126 	}
    127 }