1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.gui.selector; |
---|
7 | |
---|
8 | import java.awt.Dimension; |
---|
9 | import java.awt.event.ActionEvent; |
---|
10 | import java.awt.event.ActionListener; |
---|
11 | import java.util.StringTokenizer; |
---|
12 | |
---|
13 | import javax.swing.BorderFactory; |
---|
14 | import javax.swing.DefaultListModel; |
---|
15 | import javax.swing.JButton; |
---|
16 | import javax.swing.JComboBox; |
---|
17 | import javax.swing.JFrame; |
---|
18 | import javax.swing.JLabel; |
---|
19 | import javax.swing.JList; |
---|
20 | import javax.swing.JPanel; |
---|
21 | import javax.swing.JScrollPane; |
---|
22 | import javax.swing.JTextField; |
---|
23 | import javax.swing.ListSelectionModel; |
---|
24 | import javax.swing.Spring; |
---|
25 | import javax.swing.SpringLayout; |
---|
26 | import javax.swing.event.ListSelectionEvent; |
---|
27 | import javax.swing.event.ListSelectionListener; |
---|
28 | |
---|
29 | import vnsim.gui.MapLoader; |
---|
30 | import vnsim.vehicular.scenarios.*; |
---|
31 | import vnsim.vehicular.simulator.Location; |
---|
32 | |
---|
33 | public class ScenarioConfiguration extends JFrame { |
---|
34 | |
---|
35 | private static final long serialVersionUID = -2314871286334244756L; |
---|
36 | |
---|
37 | int selectedEntryIdx = 0; |
---|
38 | |
---|
39 | JPanel entryPane, mapPanel; |
---|
40 | |
---|
41 | ScenarioMap inScenario; |
---|
42 | |
---|
43 | Scenario outScenario; |
---|
44 | |
---|
45 | JButton startSim, addParam, cancel, AddNewRoute, CancelAdd; |
---|
46 | |
---|
47 | static MapPanel mapRepresentation; |
---|
48 | |
---|
49 | EntryExitConfig entryConfig; |
---|
50 | |
---|
51 | JComboBox entry; |
---|
52 | |
---|
53 | JTextField numeSave; |
---|
54 | |
---|
55 | JLabel saveAs, entryLabel, exitLabel; |
---|
56 | |
---|
57 | static ScenarioConfiguration backRef = null; |
---|
58 | |
---|
59 | private ScenarioDesigner sd = null; |
---|
60 | |
---|
61 | LocalActionListener lal; |
---|
62 | |
---|
63 | public class LocalActionListener implements ActionListener, ListSelectionListener { |
---|
64 | public void actionPerformed(ActionEvent e) { |
---|
65 | if (e.getSource().equals(entry)) { |
---|
66 | entryConfig.setNewEntry(inScenario.entries.get(entry.getSelectedIndex())); |
---|
67 | } |
---|
68 | if (e.getSource().equals(cancel)) { |
---|
69 | backRef.setVisible(false); |
---|
70 | MapLoader.show(true); |
---|
71 | } |
---|
72 | if (e.getSource().equals(startSim)) { |
---|
73 | setVisible(false); |
---|
74 | String s = null; |
---|
75 | s = numeSave.getText(); |
---|
76 | if (s != null) { |
---|
77 | outScenario.name = s; |
---|
78 | } |
---|
79 | entryConfig.saveChanges(); |
---|
80 | sd.afterExit(); |
---|
81 | } |
---|
82 | if (e.getSource().equals(AddNewRoute)) { |
---|
83 | if (AddNewRoute.getText().equals("Add a new route")) { |
---|
84 | AddNewRoute.setText("Add the route"); |
---|
85 | CancelAdd.setEnabled(true); |
---|
86 | entryConfig.disableIt(); |
---|
87 | startSim.setEnabled(false); |
---|
88 | entry.setEnabled(false); |
---|
89 | mapRepresentation.routePoints = null; |
---|
90 | } else { |
---|
91 | AddNewRoute.setText("Add a new route"); |
---|
92 | CancelAdd.setEnabled(false); |
---|
93 | entryConfig.enableIt(); |
---|
94 | startSim.setEnabled(true); |
---|
95 | entry.setEnabled(true); |
---|
96 | Location ex = null, en = null; |
---|
97 | if (mapRepresentation.currentRoute != null) { |
---|
98 | String s = entry.getSelectedItem().toString(); |
---|
99 | Route rt = mapRepresentation.currentRoute; |
---|
100 | StringTokenizer st = new StringTokenizer(s); |
---|
101 | st.nextToken(); |
---|
102 | int ent = Integer.valueOf((st.nextToken())); |
---|
103 | s = entryConfig.exitSelect.getSelectedItem().toString(); |
---|
104 | st = new StringTokenizer(s); |
---|
105 | st.nextToken(); |
---|
106 | int ext = Integer.valueOf((st.nextToken())); |
---|
107 | en = new Location(inScenario.entries.get(ent).roadIdx, inScenario.entries.get(ent).ptIdx); |
---|
108 | ex = new Location(inScenario.exits.get(ext).roadIdx, inScenario.exits.get(ext).ptIdx); |
---|
109 | rt.entry = en; |
---|
110 | rt.exit = ex; |
---|
111 | inScenario.allRoutes.add(rt); |
---|
112 | outScenario.allRoutes.add(rt); |
---|
113 | } |
---|
114 | mapRepresentation.routePoints = null; |
---|
115 | } |
---|
116 | } |
---|
117 | if (e.getSource().equals(CancelAdd)) { |
---|
118 | AddNewRoute.setText("Add a route"); |
---|
119 | CancelAdd.setEnabled(false); |
---|
120 | entryConfig.enableIt(); |
---|
121 | startSim.setEnabled(true); |
---|
122 | entry.setEnabled(true); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | public void valueChanged(ListSelectionEvent e) { |
---|
127 | if (e.getValueIsAdjusting() == false) { |
---|
128 | if (entry.getSelectedIndex() != selectedEntryIdx) { |
---|
129 | selectedEntryIdx = entry.getSelectedIndex(); |
---|
130 | Location newEntry = inScenario.entries.get(entry.getSelectedIndex()); |
---|
131 | entryConfig.setNewEntry(newEntry); |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | public void addEntry(Location l) { |
---|
138 | inScenario.entries.add(l); |
---|
139 | inScenario.exits.add(l); |
---|
140 | outScenario.entries.add(l); |
---|
141 | outScenario.exits.add(l); |
---|
142 | } |
---|
143 | |
---|
144 | public static final ScenarioConfiguration getInstance(ScenarioDesigner sd, ScenarioMap s, Scenario outScen) { |
---|
145 | if (backRef != null) { |
---|
146 | backRef.dispose(); |
---|
147 | backRef = null; |
---|
148 | } |
---|
149 | try { |
---|
150 | backRef = new ScenarioConfiguration(sd, s, outScen); |
---|
151 | } catch (Exception e) { |
---|
152 | return null; |
---|
153 | } |
---|
154 | return backRef; |
---|
155 | } |
---|
156 | |
---|
157 | private ScenarioConfiguration(ScenarioDesigner sd, ScenarioMap s, Scenario outScen) throws Exception { |
---|
158 | super("Configure simulation!"); |
---|
159 | if (s.allRoutes.size() == 0) { |
---|
160 | throw new Exception(); |
---|
161 | } |
---|
162 | this.inScenario = s; |
---|
163 | this.sd = sd; |
---|
164 | System.err.println("##" + inScenario.exits.size()); |
---|
165 | if (outScen == null) { |
---|
166 | this.outScenario = new Scenario(s.mapFileName, s.entries, s.exits, s.driverTypes, s.allRoutes, s.name); |
---|
167 | } else { |
---|
168 | this.outScenario = outScen; |
---|
169 | } |
---|
170 | int hight = 500 + (50 * s.driverTypes.size()); |
---|
171 | |
---|
172 | SpringLayout layout1 = new SpringLayout(); |
---|
173 | SpringLayout layout2 = new SpringLayout(); |
---|
174 | SpringLayout layout3 = new SpringLayout(); |
---|
175 | |
---|
176 | this.setLayout(layout1); |
---|
177 | entryPane = new JPanel(); |
---|
178 | entryPane.setLayout(layout2); |
---|
179 | entryPane.setBorder(BorderFactory.createEtchedBorder()); |
---|
180 | |
---|
181 | mapPanel = new JPanel(); |
---|
182 | mapPanel.setLayout(layout3); |
---|
183 | mapPanel.setBorder(BorderFactory.createEtchedBorder()); |
---|
184 | |
---|
185 | this.entryConfig = new EntryExitConfig(this, inScenario, inScenario.entries.get(0), outScenario); |
---|
186 | |
---|
187 | SpringLayout.Constraints constrain; |
---|
188 | |
---|
189 | lal = new LocalActionListener(); |
---|
190 | |
---|
191 | entry = new JComboBox(); |
---|
192 | for (int i = 0; i < s.entries.size(); i++) { |
---|
193 | entry.addItem("Entry " + (int) (i)); |
---|
194 | } |
---|
195 | entry.setSelectedIndex(0); |
---|
196 | entry.addActionListener(lal); |
---|
197 | |
---|
198 | entryLabel = new JLabel("Select an entry:"); |
---|
199 | numeSave = new JTextField(); |
---|
200 | numeSave.setText(s.name); |
---|
201 | saveAs = new JLabel("Save scenario as:"); |
---|
202 | startSim = new JButton("Save"); |
---|
203 | cancel = new JButton("Cancel"); |
---|
204 | AddNewRoute = new JButton("Add a new route"); |
---|
205 | CancelAdd = new JButton("Cancel current route"); |
---|
206 | CancelAdd.setEnabled(false); |
---|
207 | |
---|
208 | SpringLayout.Constraints mapReprezentationConstraint = layout3.getConstraints(mapRepresentation); |
---|
209 | mapReprezentationConstraint.setX(Spring.constant(0)); |
---|
210 | mapReprezentationConstraint.setY(Spring.constant(0)); |
---|
211 | mapReprezentationConstraint.setWidth(Spring.constant(hight-10)); |
---|
212 | mapReprezentationConstraint.setHeight(Spring.constant(hight-10)); |
---|
213 | |
---|
214 | SpringLayout.Constraints entryLabelConstraint = layout2.getConstraints(entryLabel); |
---|
215 | SpringLayout.Constraints entryConstraint = layout2.getConstraints(entry); |
---|
216 | SpringLayout.Constraints entryConfigConstraint = layout2.getConstraints(entryConfig); |
---|
217 | SpringLayout.Constraints startSimConstraint = layout2.getConstraints(startSim); |
---|
218 | SpringLayout.Constraints cancelConstraint = layout2.getConstraints(cancel); |
---|
219 | SpringLayout.Constraints numeSaveConstraint = layout2.getConstraints(numeSave); |
---|
220 | |
---|
221 | entryLabelConstraint.setX(Spring.constant(1)); |
---|
222 | entryLabelConstraint.setY(Spring.constant(1)); |
---|
223 | entryLabelConstraint.setWidth(Spring.constant(193)); |
---|
224 | entryLabelConstraint.setHeight(Spring.constant(20)); |
---|
225 | |
---|
226 | entryConstraint.setX(Spring.constant(1)); |
---|
227 | entryConstraint.setY(Spring.constant(25)); |
---|
228 | entryConstraint.setWidth(Spring.constant(193)); |
---|
229 | entryConstraint.setHeight(Spring.constant(20)); |
---|
230 | |
---|
231 | entryConfigConstraint.setX(Spring.constant(1)); |
---|
232 | entryConfigConstraint.setY(Spring.constant(50)); |
---|
233 | entryConfigConstraint.setWidth(Spring.constant(200)); |
---|
234 | entryConfigConstraint.setHeight(Spring.constant(hight-450)); |
---|
235 | |
---|
236 | constrain = layout2.getConstraints(AddNewRoute); |
---|
237 | constrain.setX(Spring.constant(1)); |
---|
238 | constrain.setY(Spring.constant((hight - 200))); |
---|
239 | constrain.setWidth(Spring.constant(193)); |
---|
240 | constrain.setHeight(Spring.constant(20)); |
---|
241 | |
---|
242 | constrain = layout2.getConstraints(CancelAdd); |
---|
243 | constrain.setX(Spring.constant(1)); |
---|
244 | constrain.setY(Spring.constant((hight - 175))); |
---|
245 | constrain.setWidth(Spring.constant(193)); |
---|
246 | constrain.setHeight(Spring.constant(20)); |
---|
247 | |
---|
248 | numeSaveConstraint.setX(Spring.constant(1)); |
---|
249 | numeSaveConstraint.setY(Spring.constant((hight - 125))); |
---|
250 | numeSaveConstraint.setWidth(Spring.constant(193)); |
---|
251 | numeSaveConstraint.setHeight(Spring.constant(20)); |
---|
252 | |
---|
253 | constrain = layout2.getConstraints(saveAs); |
---|
254 | constrain.setX(Spring.constant(1)); |
---|
255 | constrain.setY(Spring.constant(hight - 150)); |
---|
256 | constrain.setWidth(Spring.constant(193)); |
---|
257 | constrain.setHeight(Spring.constant(20)); |
---|
258 | |
---|
259 | startSimConstraint.setX(Spring.constant(1)); |
---|
260 | startSimConstraint.setY(Spring.constant((hight - 100))); |
---|
261 | startSimConstraint.setWidth(Spring.constant(93)); |
---|
262 | startSimConstraint.setHeight(Spring.constant(20)); |
---|
263 | |
---|
264 | cancelConstraint.setX(Spring.constant(100)); |
---|
265 | cancelConstraint.setY(Spring.constant((hight - 100))); |
---|
266 | cancelConstraint.setWidth(Spring.constant(93)); |
---|
267 | cancelConstraint.setHeight(Spring.constant(20)); |
---|
268 | |
---|
269 | startSim.addActionListener(lal); |
---|
270 | cancel.addActionListener(lal); |
---|
271 | AddNewRoute.addActionListener(lal); |
---|
272 | CancelAdd.addActionListener(lal); |
---|
273 | |
---|
274 | entryPane.add(entryLabel); |
---|
275 | entryPane.add(entry); |
---|
276 | entryPane.add(numeSave); |
---|
277 | entryPane.add(saveAs); |
---|
278 | entryPane.add(startSim); |
---|
279 | entryPane.add(cancel); |
---|
280 | entryPane.add(AddNewRoute); |
---|
281 | entryPane.add(CancelAdd); |
---|
282 | entryPane.add(entryConfig); |
---|
283 | |
---|
284 | mapPanel.add(mapRepresentation); |
---|
285 | |
---|
286 | constrain = layout1.getConstraints(entryPane); |
---|
287 | constrain.setX(Spring.constant(1)); |
---|
288 | constrain.setY(Spring.constant(1)); |
---|
289 | constrain.setWidth(Spring.constant(205)); |
---|
290 | constrain.setHeight(Spring.constant(hight)); |
---|
291 | |
---|
292 | constrain = layout1.getConstraints(mapPanel); |
---|
293 | constrain.setX(Spring.constant(210)); |
---|
294 | constrain.setY(Spring.constant(1)); |
---|
295 | constrain.setWidth(Spring.constant(hight - 5)); |
---|
296 | constrain.setHeight(Spring.constant(hight)); |
---|
297 | |
---|
298 | this.add(entryPane); |
---|
299 | this.add(mapPanel); |
---|
300 | |
---|
301 | this.setSize(hight+212, hight + 37); |
---|
302 | this.setLocation(10, 10); |
---|
303 | this.setResizable(false); |
---|
304 | this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
---|
305 | this.setVisible(true); |
---|
306 | this.getContentPane().repaint(); |
---|
307 | |
---|
308 | entryConfig.setNewEntry(inScenario.entries.get(0)); |
---|
309 | } |
---|
310 | |
---|
311 | public Scenario getScenario() { |
---|
312 | return this.outScenario; |
---|
313 | } |
---|
314 | |
---|
315 | public void configurationResult(Scenario outScenario) { |
---|
316 | this.outScenario = outScenario; |
---|
317 | this.setVisible(true); |
---|
318 | this.entryConfig = null; |
---|
319 | } |
---|
320 | } |
---|