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 /***
31 * The address text field.
32 *
33 * @uml.property name="addressField"
34 */
35 private JTextField addressField;
36
37 /***
38 * the DSOL application
39 *
40 * @uml.property name="application"
41 */
42 private DSOLApplicationInterface application = null;
43
44
45 /***
46 * Creates a new empty HTML panel.
47 *
48 * @param application application
49 */
50 public URLChooserFrame(final DSOLApplicationInterface application)
51 {
52 super("Enter the URL of an experiment");
53 this.setLocation(100, 150);
54 this.application = application;
55 this.initialize();
56 }
57
58 /***
59 * initializes the URLChooserFrame
60 */
61 private void initialize()
62 {
63 JPanel contentPane = new JPanel(new BorderLayout());
64 JPanel addressPanel = new JPanel(new BorderLayout());
65 contentPane.add(addressPanel, BorderLayout.CENTER);
66 JPanel addressBar = new JPanel(new BorderLayout());
67 addressBar.setBorder(new CompoundBorder(new BevelBorder(
68 BevelBorder.RAISED), new EmptyBorder(2, 2, 2, 2)));
69 addressPanel.add(addressBar, BorderLayout.CENTER);
70 addressBar.add(new JLabel("Address: "), BorderLayout.WEST);
71 this.addressField = new JTextField();
72 this.addressField.addActionListener(new AddressEntered(this));
73 addressBar.add(this.addressField, BorderLayout.CENTER);
74 addressPanel.setPreferredSize(new Dimension(600, (int) addressBar
75 .getPreferredSize().getHeight()));
76 this.setContentPane(contentPane);
77 this.pack();
78 this.setVisible(true);
79 }
80
81 /***
82 * gets addressField
83 *
84 * @return Returns the addressField.
85 *
86 * @uml.property name="addressField"
87 */
88 protected JTextField getAddressField()
89 {
90 return this.addressField;
91 }
92
93 /***
94 * gets application
95 *
96 * @return Returns the application.
97 *
98 * @uml.property name="application"
99 */
100 protected DSOLApplicationInterface getApplication()
101 {
102 return this.application;
103 }
104
105 /***
106 * Responds to an address change.
107 */
108 private class AddressEntered extends AbstractAction
109 {
110
111 /*** the URLChooserFrame */
112 private URLChooserFrame frame = null;
113
114 /***
115 * constructs a new AddressEntered
116 *
117 * @param frame the frame
118 */
119 public AddressEntered(final URLChooserFrame frame)
120 {
121 this.frame = frame;
122 }
123
124 /***
125 * @see java.awt.event.ActionListener
126 * #actionPerformed(java.awt.event.ActionEvent)
127 */
128 public void actionPerformed(final ActionEvent event)
129 {
130 URLChooserFrame.this.dispose();
131 String address = this.frame.getAddressField().getText();
132 try
133 {
134 if (address.indexOf("://") == -1)
135 {
136 address = "http://" + address;
137 }
138 URL url = new URL(address);
139 new GUIExperimentParsingThread(this.frame, this.frame, url)
140 .start();
141 } catch (Exception exception)
142 {
143 JOptionPane.showMessageDialog(URLChooserFrame.this,
144 exception.getClass().getName()
145 .substring(
146 exception.getClass().getName()
147 .lastIndexOf(".") + 1)
148 + " occurred while connecting to "
149 + address
150 + ".\n", "Error", JOptionPane.ERROR_MESSAGE);
151 return;
152 }
153 }
154 }
155
156 /***
157 * @see nl.tudelft.simulation.event.EventListenerInterface
158 * #notify(nl.tudelft.simulation.event.EventInterface)
159 */
160 public void notify(final EventInterface event)
161 {
162 this.application.setExperiment((Experiment) event.getContent());
163 }
164 }