View Javadoc

1   package nl.tudelft.simulation.dsol.gui.windows;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Dimension;
5   import java.awt.event.ActionEvent;
6   import java.net.URL;
7   
8   import javax.swing.AbstractAction;
9   import javax.swing.JFrame;
10  import javax.swing.JLabel;
11  import javax.swing.JOptionPane;
12  import javax.swing.JPanel;
13  import javax.swing.JTextField;
14  import javax.swing.border.BevelBorder;
15  import javax.swing.border.CompoundBorder;
16  import javax.swing.border.EmptyBorder;
17  
18  import nl.tudelft.simulation.dsol.experiment.Experiment;
19  import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
20  import nl.tudelft.simulation.dsol.gui.panels.GUIExperimentParsingThread;
21  import nl.tudelft.simulation.event.EventInterface;
22  import nl.tudelft.simulation.event.EventListenerInterface;
23  
24  /***
25   * The URLChooserFrame provides an addess bar for the selection of an URL
26   */
27  public class URLChooserFrame extends JFrame implements EventListenerInterface
28  {
29  
30  	/*** The address text field. */
31  	private JTextField addressField;
32  
33  	/*** the DSOL application */
34  	private DSOLApplicationInterface application = null;
35  
36  
37  	/***
38  	 * Creates a new empty HTML panel.
39  	 * 
40  	 * @param application application
41  	 */
42  	public URLChooserFrame(final DSOLApplicationInterface application)
43  	{
44  		super("Enter the URL of an experiment");
45  		this.setLocation(100, 150);
46  		this.application = application;
47  		this.initialize();
48  	}
49  
50  	/***
51  	 * initializes the URLChooserFrame
52  	 */
53  	private void initialize()
54  	{
55  		JPanel contentPane = new JPanel(new BorderLayout());
56  		JPanel addressPanel = new JPanel(new BorderLayout());
57  		contentPane.add(addressPanel, BorderLayout.CENTER);
58  		JPanel addressBar = new JPanel(new BorderLayout());
59  		addressBar.setBorder(new CompoundBorder(new BevelBorder(
60  				BevelBorder.RAISED), new EmptyBorder(2, 2, 2, 2)));
61  		addressPanel.add(addressBar, BorderLayout.CENTER);
62  		addressBar.add(new JLabel("Address: "), BorderLayout.WEST);
63  		this.addressField = new JTextField();
64  		this.addressField.addActionListener(new AddressEntered(this));
65  		addressBar.add(this.addressField, BorderLayout.CENTER);
66  		addressPanel.setPreferredSize(new Dimension(600, (int) addressBar
67  				.getPreferredSize().getHeight()));
68  		this.setContentPane(contentPane);
69  		this.pack();
70  		this.setVisible(true);
71  	}
72  
73  	/***
74  	 * gets addressField
75  	 * 
76  	 * @return Returns the addressField.
77  	 */
78  	protected JTextField getAddressField()
79  	{
80  		return this.addressField;
81  	}
82  
83  	/***
84  	 * gets application
85  	 * 
86  	 * @return Returns the application.
87  	 */
88  	protected DSOLApplicationInterface getApplication()
89  	{
90  		return this.application;
91  	}
92  
93  	/***
94  	 * Responds to an address change.
95  	 */
96  	private class AddressEntered extends AbstractAction
97  	{
98  
99  		/*** the URLChooserFrame */
100 		private URLChooserFrame frame = null;
101 
102 		/***
103 		 * constructs a new AddressEntered
104 		 * 
105 		 * @param frame the frame
106 		 */
107 		public AddressEntered(final URLChooserFrame frame)
108 		{
109 			this.frame = frame;
110 		}
111 
112 		/***
113 		 * @see java.awt.event.ActionListener
114 		 *      #actionPerformed(java.awt.event.ActionEvent)
115 		 */
116 		public void actionPerformed(final ActionEvent event)
117 		{
118 			URLChooserFrame.this.dispose();
119 			String address = this.frame.getAddressField().getText();
120 			try
121 			{
122 				if (address.indexOf("://") == -1)
123 				{
124 					address = "http://" + address;
125 				}
126 				URL url = new URL(address);
127 				new GUIExperimentParsingThread(this.frame, this.frame, url)
128 						.start();
129 			} catch (Exception exception)
130 			{
131 				JOptionPane.showMessageDialog(URLChooserFrame.this,
132 						exception.getClass().getName()
133 								.substring(
134 										exception.getClass().getName()
135 												.lastIndexOf(".") + 1)
136 								+ " occurred while connecting to "
137 								+ address
138 								+ ".\n", "Error", JOptionPane.ERROR_MESSAGE);
139 				return;
140 			}
141 		}
142 	}
143 
144 	/***
145 	 * @see nl.tudelft.simulation.event.EventListenerInterface
146 	 *      #notify(nl.tudelft.simulation.event.EventInterface)
147 	 */
148 	public void notify(final EventInterface event)
149 	{
150 		this.application.setExperiment((Experiment) event.getContent());
151 	}
152 }