View Javadoc

1   /*
2    * @(#) FileChooserFrame.java Oct 13, 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.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 2003 <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/gpl.html">General Public
36   * License (GPL) </a>, no warranty <br>
37   * 
38   * @version 1.0 18.10.2003 <br>
39   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
40   *         Jacobs </a>
41   */
42  public class FileChooserFrame extends JFrame implements ActionListener,
43  		EventListenerInterface
44  {
45  
46  	/*** source owns the fileChooser */
47  	private DSOLApplicationInterface application = null;
48  
49  	/***
50  	 * constructs a new FileChooserFrame
51  	 * 
52  	 * @param application the application
53  	 * @param directory the default directory
54  	 */
55  	public FileChooserFrame(final DSOLApplicationInterface application,
56  			final String directory)
57  	{
58  		super("open experiment");
59  		this.setLocation(75, 50);
60  		this.application = application;
61  		this.initialize(directory);
62  		this.pack();
63  		this.setVisible(true);
64  	}
65  
66  	/***
67  	 * initializes the fileChooser
68  	 * 
69  	 * @param directory the directory
70  	 */
71  	private void initialize(final String directory)
72  	{
73  		JFileChooser fileChooser = new JFileChooser(directory);
74  		fileChooser.addChoosableFileFilter(new FileFilter()
75  		{
76  
77  			public boolean accept(final File file)
78  			{
79  				if (file.getName().endsWith("xml") || file.isDirectory())
80  				{
81  					return true;
82  				}
83  				return false;
84  			}
85  
86  			public String getDescription()
87  			{
88  				return ".xml experiment files";
89  			}
90  		});
91  		fileChooser.addActionListener(this);
92  		this.getContentPane().add(fileChooser, BorderLayout.CENTER);
93  	}
94  
95  	/***
96  	 * @see java.awt.event.ActionListener
97  	 *      #actionPerformed(java.awt.event.ActionEvent)
98  	 */
99  	public void actionPerformed(final ActionEvent actionEvent)
100 	{
101 		if (!actionEvent.getActionCommand().equals("CancelSelection"))
102 		{
103 			File selectedFile = ((JFileChooser) actionEvent.getSource())
104 					.getSelectedFile();
105 			try
106 			{
107 				this.dispose();
108 				new GUIExperimentParsingThread(this, this, selectedFile.toURL())
109 						.start();
110 			} catch (MalformedURLException e)
111 			{
112 				Logger.warning(this, "actionPerformed", e);
113 			}
114 		} else
115 		{
116 			this.dispose();
117 		}
118 	}
119 
120 	/***
121 	 * @see nl.tudelft.simulation.event.EventListenerInterface
122 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
123 	 */
124 	public void notify(final EventInterface event)
125 	{
126 		this.application.setExperiment((Experiment) event.getContent());
127 	}
128 }