[31] | 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.event.ActionEvent; |
---|
| 9 | import java.awt.event.ActionListener; |
---|
| 10 | import java.awt.event.WindowEvent; |
---|
| 11 | import java.awt.event.WindowListener; |
---|
| 12 | import java.io.File; |
---|
| 13 | import java.io.FileInputStream; |
---|
| 14 | import java.io.FileOutputStream; |
---|
| 15 | import java.io.IOException; |
---|
| 16 | import java.io.ObjectInputStream; |
---|
| 17 | import java.io.ObjectOutputStream; |
---|
| 18 | import java.io.PrintStream; |
---|
| 19 | import java.util.ArrayList; |
---|
| 20 | import java.util.logging.Level; |
---|
| 21 | import java.util.logging.Logger; |
---|
| 22 | |
---|
| 23 | import javax.swing.BorderFactory; |
---|
| 24 | import javax.swing.JButton; |
---|
| 25 | import javax.swing.JComboBox; |
---|
| 26 | import javax.swing.JFrame; |
---|
| 27 | import javax.swing.JLabel; |
---|
| 28 | import javax.swing.JOptionPane; |
---|
| 29 | import javax.swing.JPanel; |
---|
| 30 | import javax.swing.JScrollPane; |
---|
| 31 | import javax.swing.JTextField; |
---|
| 32 | import javax.swing.Spring; |
---|
| 33 | import javax.swing.SpringLayout; |
---|
| 34 | import javax.swing.event.ListSelectionEvent; |
---|
| 35 | import javax.swing.event.ListSelectionListener; |
---|
| 36 | |
---|
| 37 | import vnsim.gui.MapLoader; |
---|
| 38 | import vnsim.gui.Utils; |
---|
| 39 | import vnsim.map.object.Map; |
---|
| 40 | import vnsim.vehicular.scenarios.MainSMFCreator; |
---|
| 41 | import vnsim.vehicular.scenarios.Route; |
---|
| 42 | import vnsim.vehicular.scenarios.Scenario; |
---|
| 43 | import vnsim.vehicular.scenarios.ScenarioMap; |
---|
| 44 | import vnsim.vehicular.simulator.Location; |
---|
| 45 | import vnsim.vehicular.simulator.RouteSegment; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * |
---|
| 49 | * @author Simion Liviu Mihai |
---|
| 50 | * |
---|
| 51 | */ |
---|
| 52 | public class RouteConfiguration extends JFrame { |
---|
| 53 | |
---|
| 54 | /** Logger used by this class */ |
---|
| 55 | private static final transient Logger logger = Logger.getLogger("vnsim.gui.selector.RouteConfiguration"); |
---|
| 56 | |
---|
| 57 | public static String mapsDir = "/maps/map/"; |
---|
| 58 | |
---|
| 59 | public static String routeDir = "/maps/routefiles/"; |
---|
| 60 | |
---|
| 61 | private static final long serialVersionUID = -2314871286334244756L; |
---|
| 62 | |
---|
| 63 | int selectedEntryIdx = 0; |
---|
| 64 | |
---|
| 65 | JPanel entryPane; |
---|
| 66 | |
---|
| 67 | JScrollPane entryScroll; |
---|
| 68 | |
---|
| 69 | ScenarioMap inScenario; |
---|
| 70 | |
---|
| 71 | Scenario outScenario; |
---|
| 72 | |
---|
| 73 | JButton deleteRoute, saveScenario, addParam, cancel, AddNewRoute, AddNewEntry, AddNewExit, cancelRoute; |
---|
| 74 | |
---|
| 75 | Map tempMap; |
---|
| 76 | |
---|
| 77 | EntryExitConfig entryConfig; |
---|
| 78 | |
---|
| 79 | JComboBox entry; |
---|
| 80 | |
---|
| 81 | JComboBox exit; |
---|
| 82 | |
---|
| 83 | JComboBox rutemarcate; |
---|
| 84 | |
---|
| 85 | JTextField numeSave, currentroad, currentpoint, selectedroad, selectedpoint; |
---|
| 86 | |
---|
| 87 | JLabel saveAs, entrys, exits, params, currentRoad, currentPoint, selectedRoad, selectedPoint, ruteMarcate; |
---|
| 88 | |
---|
| 89 | RouteConfiguration backRef; |
---|
| 90 | |
---|
| 91 | LocalActionListener lal; |
---|
| 92 | |
---|
| 93 | RouteDraw routeDraw; |
---|
| 94 | |
---|
| 95 | ArrayList<ArrayList<Location>> allRoutes = new ArrayList<ArrayList<Location>>(); |
---|
| 96 | |
---|
| 97 | private static RouteConfiguration _instance = null; |
---|
| 98 | |
---|
| 99 | private RouteDesigner rd = null; |
---|
| 100 | |
---|
| 101 | private void showRoute() { |
---|
| 102 | routeDraw.isRouteShowing = true; |
---|
| 103 | routeDraw.start = allRoutes.get(rutemarcate.getSelectedIndex()).get(0); |
---|
| 104 | routeDraw.stop = allRoutes.get(rutemarcate.getSelectedIndex()).get( |
---|
| 105 | allRoutes.get(rutemarcate.getSelectedIndex()).size() - 1); |
---|
| 106 | routeDraw.routePoints = allRoutes.get(rutemarcate.getSelectedIndex()); |
---|
| 107 | routeDraw.currentRoute = new Route(); |
---|
| 108 | routeDraw.currentRoute.entry = routeDraw.start; |
---|
| 109 | routeDraw.currentRoute.exit = routeDraw.start; |
---|
| 110 | routeDraw.currentRoute.route = new ArrayList<RouteSegment>(); |
---|
| 111 | for (int i = 0; i < allRoutes.get(rutemarcate.getSelectedIndex()).size() - 1; i += 1) { |
---|
| 112 | Location l1 = allRoutes.get(rutemarcate.getSelectedIndex()).get(i); |
---|
| 113 | System.err.println(i + " : " + l1.roadIdx + " " + l1.ptIdx); |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | for (int i = 0; i < allRoutes.get(rutemarcate.getSelectedIndex()).size() - 1; i += 1) { |
---|
| 117 | Location l1 = allRoutes.get(rutemarcate.getSelectedIndex()).get(i); |
---|
| 118 | Location l2 = allRoutes.get(rutemarcate.getSelectedIndex()).get(i + 1); |
---|
| 119 | System.err.println(i + " : " + l1.roadIdx + " " + l1.ptIdx + " : " + l2.roadIdx + " " + l2.ptIdx); |
---|
| 120 | if (l1.roadIdx == l2.roadIdx) { |
---|
| 121 | routeDraw.currentRoute.route.add(new RouteSegment((short) l1.roadIdx, (short) l1.ptIdx, |
---|
| 122 | (short) l2.ptIdx)); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * |
---|
| 129 | * @author Simion Liviu Mihai |
---|
| 130 | * |
---|
| 131 | */ |
---|
| 132 | public class LocalActionListener implements ActionListener { |
---|
| 133 | public void actionPerformed(ActionEvent e) { |
---|
| 134 | if (e.getSource().equals(saveScenario)) { |
---|
| 135 | String s = null; |
---|
| 136 | s = numeSave.getText(); |
---|
| 137 | if (s != null) { |
---|
| 138 | outScenario.name = s; |
---|
| 139 | } |
---|
| 140 | saveScenario(); |
---|
| 141 | routeDraw.isRouteShowing = false; |
---|
| 142 | routeDraw.currentRoute = null; |
---|
| 143 | rd.afterExit(); |
---|
| 144 | backRef.setVisible(false); |
---|
| 145 | MapLoader.show(true); |
---|
| 146 | return; |
---|
| 147 | } |
---|
| 148 | if (e.getSource().equals(cancel)) { |
---|
| 149 | backRef.setVisible(false); |
---|
| 150 | MapLoader.show(true); |
---|
| 151 | return; |
---|
| 152 | } |
---|
| 153 | if (e.getSource().equals(AddNewEntry)) { |
---|
| 154 | if (routeDraw.currentLocation != null) { |
---|
| 155 | routeDraw.entrys.add(routeDraw.currentLocation); |
---|
| 156 | entry.addItem(String.valueOf(routeDraw.entrys.size())); |
---|
| 157 | routeDraw.isRouteEditing = false; |
---|
| 158 | entry.setSelectedIndex(entry.getItemCount() - 1); |
---|
| 159 | routeDraw.setStart(routeDraw.entrys.get(routeDraw.entrys.size() - 1)); |
---|
| 160 | } |
---|
| 161 | routeDraw.isRouteShowing = false; |
---|
| 162 | routeDraw.currentRoute = null; |
---|
| 163 | deleteRoute.setEnabled(false); |
---|
| 164 | return; |
---|
| 165 | } |
---|
| 166 | if (e.getSource().equals(AddNewExit)) { |
---|
| 167 | if (routeDraw.currentLocation != null) { |
---|
| 168 | routeDraw.exits.add(routeDraw.currentLocation); |
---|
| 169 | exit.addItem(String.valueOf(routeDraw.exits.size())); |
---|
| 170 | routeDraw.isRouteEditing = false; |
---|
| 171 | routeDraw.currentRoute = null; |
---|
| 172 | exit.setSelectedIndex(exit.getItemCount() - 1); |
---|
| 173 | routeDraw.setStop(routeDraw.exits.get(routeDraw.exits.size() - 1)); |
---|
| 174 | } |
---|
| 175 | routeDraw.isRouteShowing = false; |
---|
| 176 | deleteRoute.setEnabled(false); |
---|
| 177 | return; |
---|
| 178 | } |
---|
| 179 | if (e.getSource().equals(rutemarcate)) { |
---|
| 180 | System.err.println(rutemarcate.getSelectedIndex() + " : " + allRoutes.size()); |
---|
| 181 | if (rutemarcate.getSelectedIndex() != -1) { |
---|
| 182 | deleteRoute.setEnabled(true); |
---|
| 183 | System.err.println(allRoutes.get(rutemarcate.getSelectedIndex()).size()); |
---|
| 184 | showRoute(); |
---|
| 185 | } |
---|
| 186 | return; |
---|
| 187 | } |
---|
| 188 | if (e.getSource().equals(entry)) { |
---|
| 189 | if ((routeDraw.entrys != null) && (entry.getSelectedIndex() != -1)) { |
---|
| 190 | routeDraw.setStart(routeDraw.entrys.get(Integer.parseInt((String) entry.getSelectedItem()) - 1)); |
---|
| 191 | // routeDraw.start = ; |
---|
| 192 | routeDraw.isRouteEditing = false; |
---|
| 193 | routeDraw.isRouteShowing = false; |
---|
| 194 | routeDraw.currentRoute = null; |
---|
| 195 | deleteRoute.setEnabled(false); |
---|
| 196 | } |
---|
| 197 | return; |
---|
| 198 | } |
---|
| 199 | if (e.getSource().equals(exit)) { |
---|
| 200 | if ((routeDraw.exits != null) && (exit.getSelectedIndex() != -1)) { |
---|
| 201 | routeDraw.stop = routeDraw.exits.get(Integer.parseInt((String) exit.getSelectedItem()) - 1); |
---|
| 202 | routeDraw.isRouteEditing = false; |
---|
| 203 | routeDraw.isRouteShowing = false; |
---|
| 204 | deleteRoute.setEnabled(false); |
---|
| 205 | routeDraw.currentRoute = null; |
---|
| 206 | } |
---|
| 207 | return; |
---|
| 208 | } |
---|
| 209 | if (e.getSource().equals(cancelRoute)) { |
---|
| 210 | AddNewRoute.setText("Make route"); |
---|
| 211 | routeDraw.isRouteEditing = false; |
---|
| 212 | routeDraw.routePoints = new ArrayList<Location>(); |
---|
| 213 | routeDraw.isRouteShowing = false; |
---|
| 214 | routeDraw.currentRoute = null; |
---|
| 215 | deleteRoute.setEnabled(false); |
---|
| 216 | return; |
---|
| 217 | } |
---|
| 218 | if (e.getSource().equals(deleteRoute)) { |
---|
| 219 | if (allRoutes.size() == 0) { |
---|
| 220 | return; |
---|
| 221 | } |
---|
| 222 | allRoutes.remove(rutemarcate.getSelectedIndex()); |
---|
| 223 | rutemarcate.removeItemAt(rutemarcate.getSelectedIndex()); |
---|
| 224 | if (allRoutes.size() > 0) { |
---|
| 225 | rutemarcate.setSelectedIndex(0); |
---|
| 226 | routeDraw.start = allRoutes.get(0).get(0); |
---|
| 227 | routeDraw.stop = allRoutes.get(0).get(allRoutes.get(0).size() - 1); |
---|
| 228 | routeDraw.routePoints = allRoutes.get(0); |
---|
| 229 | showRoute(); |
---|
| 230 | } else { |
---|
| 231 | deleteRoute.setEnabled(false); |
---|
| 232 | routeDraw.isRouteShowing = false; |
---|
| 233 | routeDraw.isRouteEditing = false; |
---|
| 234 | routeDraw.currentRoute = null; |
---|
| 235 | } |
---|
| 236 | rutemarcate.validate(); |
---|
| 237 | rutemarcate.revalidate(); |
---|
| 238 | return; |
---|
| 239 | } |
---|
| 240 | if (e.getSource().equals(AddNewRoute)) { |
---|
| 241 | if ((entry.getSelectedIndex() == -1) || (exit.getSelectedIndex() == -1)) { |
---|
| 242 | return; |
---|
| 243 | } |
---|
| 244 | if (AddNewRoute.getText().charAt(0) == 'M') { |
---|
| 245 | cancelRoute.setEnabled(true); |
---|
| 246 | AddNewRoute.setText("Add route"); |
---|
| 247 | routeDraw.isRouteEditing = true; |
---|
| 248 | routeDraw.routePoints = new ArrayList<Location>(); |
---|
| 249 | routeDraw.routePoints.add(routeDraw.start); |
---|
| 250 | routeDraw.currentRoute = new Route(); |
---|
| 251 | routeDraw.currentRoute.entry = routeDraw.start; |
---|
| 252 | routeDraw.currentRoute.entry = routeDraw.stop; |
---|
| 253 | routeDraw.currentRoute.route = new ArrayList<RouteSegment>(); |
---|
| 254 | } else if (AddNewRoute.getText().contains("Add")) { |
---|
| 255 | routeDraw.currentRoute = null; |
---|
| 256 | routeDraw.routePoints.add(routeDraw.stop); |
---|
| 257 | AddNewRoute.setText("Make route"); |
---|
| 258 | if (routeDraw.routePoints.get(routeDraw.routePoints.size()-2).roadIdx!=routeDraw.routePoints.get(routeDraw.routePoints.size()-1).roadIdx){ |
---|
| 259 | int ret=JOptionPane.showConfirmDialog(null, "The route is incomplete! Would you like to finish it?"); |
---|
| 260 | if (ret==JOptionPane.NO_OPTION){ |
---|
| 261 | AddNewRoute.setText("Make route"); |
---|
| 262 | routeDraw.isRouteEditing = false; |
---|
| 263 | routeDraw.routePoints = new ArrayList<Location>(); |
---|
| 264 | routeDraw.isRouteShowing = false; |
---|
| 265 | routeDraw.currentRoute = null; |
---|
| 266 | deleteRoute.setEnabled(false); |
---|
| 267 | return; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | allRoutes.add(routeDraw.routePoints); |
---|
| 271 | routeDraw.isRouteEditing = false; |
---|
| 272 | routeDraw.isRouteShowing = true; |
---|
| 273 | routeDraw.routePoints = new ArrayList<Location>(); |
---|
| 274 | rutemarcate.addItem(((String) entry.getSelectedItem()) + "->" + ((String) exit.getSelectedItem())); |
---|
| 275 | cancelRoute.setEnabled(false); |
---|
| 276 | showRoute(); |
---|
| 277 | } |
---|
| 278 | routeDraw.isRouteShowing = false; |
---|
| 279 | deleteRoute.setEnabled(false); |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | * |
---|
| 285 | */ |
---|
| 286 | private void saveScenario() { |
---|
| 287 | // TODO Auto-generated method stub |
---|
| 288 | // Creeaza fisierul TXT cu rutele |
---|
| 289 | System.err.println("Am inceput de scris " + allRoutes.size()); |
---|
| 290 | try { |
---|
| 291 | PrintStream ps = new PrintStream(new File("." + File.separatorChar |
---|
| 292 | + MainSMFCreator.routeDir.replace('/', File.separatorChar) + numeSave.getText() + ".txt")); |
---|
| 293 | ps.println(routeDraw.exits.size() + " exits"); |
---|
| 294 | for (int i = 0; i < routeDraw.exits.size(); i++) { |
---|
| 295 | ps.println("\t" + routeDraw.exits.get(i).roadIdx + " " + routeDraw.exits.get(i).ptIdx + " "); |
---|
| 296 | } |
---|
| 297 | ps.println(routeDraw.exits.size() + " entries "); |
---|
| 298 | System.err.println(allRoutes.get(0).get(0).roadIdx + " : " + allRoutes.get(0).get(0).ptIdx); |
---|
| 299 | for (int i = 0; i < routeDraw.entrys.size(); i++) { |
---|
| 300 | ps.println(routeDraw.entrys.get(i).roadIdx + " " + routeDraw.entrys.get(i).ptIdx); |
---|
| 301 | int routesnr = 0; |
---|
| 302 | for (int j = 0; j < allRoutes.size(); j++) { |
---|
| 303 | if (allRoutes.get(j).get(0).equals(routeDraw.entrys.get(i))) { |
---|
| 304 | routesnr++; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | ps.println("\t" + routesnr + " routes"); |
---|
| 308 | for (int j = 0; j < allRoutes.size(); j++) { |
---|
| 309 | if (allRoutes.get(j).get(0).equals(routeDraw.entrys.get(i))) { |
---|
| 310 | ps.println("\tTO " + allRoutes.get(j).get(allRoutes.get(j).size() - 1).roadIdx + " " |
---|
| 311 | + allRoutes.get(j).get(allRoutes.get(j).size() - 1).ptIdx); |
---|
| 312 | int segments = 0; |
---|
| 313 | for (int k = 1; k < allRoutes.get(j).size(); k++) { |
---|
| 314 | if (allRoutes.get(j).get(k).roadIdx == allRoutes.get(j).get(k - 1).roadIdx) { |
---|
| 315 | segments++; |
---|
| 316 | } |
---|
| 317 | } |
---|
| 318 | ps.println("\t\t" + segments + " segments"); |
---|
| 319 | for (int k = 1; k < allRoutes.get(j).size(); k++) { |
---|
| 320 | if (allRoutes.get(j).get(k).roadIdx == allRoutes.get(j).get(k - 1).roadIdx) { |
---|
| 321 | ps.println("\t\t" + allRoutes.get(j).get(k).roadIdx + " " |
---|
| 322 | + allRoutes.get(j).get(k - 1).ptIdx + " " + allRoutes.get(j).get(k).ptIdx); |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | ps.close(); |
---|
| 329 | try { |
---|
| 330 | ArrayList<String> driverTypes = new ArrayList<String>(); |
---|
| 331 | driverTypes.add("very calm"); |
---|
| 332 | driverTypes.add("regular"); |
---|
| 333 | driverTypes.add("aggresive"); |
---|
| 334 | String name = numeSave.getText(); |
---|
| 335 | String routeFile = name + ".txt"; |
---|
| 336 | ScenarioMap scen = new ScenarioMap(); |
---|
| 337 | scen.name = name; |
---|
| 338 | scen.imageFileName = "/images/none.png"; |
---|
| 339 | scen.driverTypes = driverTypes; |
---|
| 340 | scen.mapFileName = inScenario.mapFileName; |
---|
| 341 | System.err.println("-> " + scen.mapFileName); |
---|
| 342 | try { |
---|
| 343 | MainSMFCreator.loadScenarioData(routeDir + routeFile, scen); |
---|
| 344 | } catch (Exception ex) { |
---|
| 345 | logger.severe("Got exception loading scenario data " + ex.getLocalizedMessage()); |
---|
| 346 | } |
---|
| 347 | final String dir = System.getProperty("user.home") + File.separatorChar + "maps" |
---|
| 348 | + File.separatorChar + "smf"; |
---|
| 349 | (new File(dir)).mkdirs(); |
---|
| 350 | ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(dir + File.separatorChar |
---|
| 351 | + name + ".smf")); |
---|
| 352 | oos.writeObject(scen); |
---|
| 353 | oos.close(); |
---|
| 354 | MapLoader.refreshSMF(); |
---|
| 355 | } catch (Exception ex) { |
---|
| 356 | logger.log(Level.SEVERE, "Could not create SMF file", ex); |
---|
| 357 | } |
---|
| 358 | } catch (IOException e) { |
---|
| 359 | // TODO: handle exception |
---|
| 360 | e.printStackTrace(); |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | /** |
---|
| 367 | * |
---|
| 368 | * @param l |
---|
| 369 | */ |
---|
| 370 | public void addEntry(Location l) { |
---|
| 371 | inScenario.entries.add(l); |
---|
| 372 | inScenario.exits.add(l); |
---|
| 373 | outScenario.entries.add(l); |
---|
| 374 | outScenario.exits.add(l); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | public static RouteConfiguration getInstance(RouteDesigner rd, ScenarioMap s, Scenario outScen) { |
---|
| 378 | if (_instance != null) { |
---|
| 379 | _instance.dispose(); |
---|
| 380 | _instance = null; |
---|
| 381 | } |
---|
| 382 | _instance = new RouteConfiguration(rd, s, outScen); |
---|
| 383 | return _instance; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | /** |
---|
| 387 | * @param s |
---|
| 388 | * @param m |
---|
| 389 | * @param outScen |
---|
| 390 | */ |
---|
| 391 | public RouteConfiguration(RouteDesigner rd, ScenarioMap s, Scenario outScen) { |
---|
| 392 | super("Configure simulation!"); |
---|
| 393 | backRef = this; |
---|
| 394 | this.rd = rd; |
---|
| 395 | this.inScenario = s; |
---|
| 396 | this.outScenario = outScen; |
---|
| 397 | if (outScen == null) { |
---|
| 398 | this.outScenario = new Scenario(s.mapFileName, s.entries, s.exits, s.driverTypes, s.allRoutes, s.name); |
---|
| 399 | } else { |
---|
| 400 | this.outScenario = outScen; |
---|
| 401 | } |
---|
| 402 | allRoutes = new ArrayList<ArrayList<Location>>(); |
---|
| 403 | // System.err.println("!@!@!@ " + s.entries.size()); |
---|
| 404 | SpringLayout layout1 = new SpringLayout(); |
---|
| 405 | this.getContentPane().setLayout(layout1); |
---|
| 406 | SpringLayout.Constraints constrain; |
---|
| 407 | entryPane = new JPanel(); |
---|
| 408 | entryPane.setLayout(layout1); |
---|
| 409 | lal = new LocalActionListener(); |
---|
| 410 | entry = new JComboBox(); |
---|
| 411 | System.out.println("Harta este: " + inScenario.mapFileName); |
---|
| 412 | Map tempMap = null; |
---|
| 413 | try { |
---|
| 414 | ObjectInputStream ois = null; |
---|
| 415 | if (inScenario.mapFileName.startsWith(".")) |
---|
| 416 | inScenario.mapFileName = inScenario.mapFileName.substring(1); |
---|
| 417 | String path = System.getProperty("user.home") + File.separatorChar |
---|
| 418 | + inScenario.mapFileName.replace('\\', File.separatorChar).replace('/', File.separatorChar); |
---|
| 419 | File f = new File(path); |
---|
| 420 | if (f.exists() && f.isFile() && f.canRead()) |
---|
| 421 | ois = new ObjectInputStream(new FileInputStream(path)); |
---|
| 422 | else { |
---|
| 423 | ois = new ObjectInputStream(Utils.getInstance().openStream(inScenario.mapFileName.replace('\\', '/'))); |
---|
| 424 | } |
---|
| 425 | tempMap = (Map) ois.readObject(); |
---|
| 426 | ois.close(); |
---|
| 427 | } catch (Exception ex) { |
---|
| 428 | // logger.log(Level.SEVERE, |
---|
| 429 | // "Got exception for "+inScenario.mapFileName, ex); |
---|
| 430 | } |
---|
| 431 | routeDraw = new RouteDraw(this, 1000 - 170, 700 - 55, tempMap); |
---|
| 432 | entry.addActionListener(lal); |
---|
| 433 | exit = new JComboBox(); |
---|
| 434 | exit.addActionListener(lal); |
---|
| 435 | numeSave = new JTextField(); |
---|
| 436 | currentpoint = new JTextField(); |
---|
| 437 | currentroad = new JTextField(); |
---|
| 438 | currentpoint.setEditable(false); |
---|
| 439 | currentroad.setEditable(false); |
---|
| 440 | rutemarcate = new JComboBox(); |
---|
| 441 | |
---|
| 442 | selectedpoint = new JTextField(); |
---|
| 443 | selectedroad = new JTextField(); |
---|
| 444 | selectedpoint.setEditable(false); |
---|
| 445 | selectedroad.setEditable(false); |
---|
| 446 | |
---|
| 447 | numeSave.setText(s.name); |
---|
| 448 | saveAs = new JLabel("Save scenario as: "); |
---|
| 449 | params = new JLabel("Current route: "); |
---|
| 450 | entrys = new JLabel("Entry : "); |
---|
| 451 | exits = new JLabel("Exit : "); |
---|
| 452 | ruteMarcate = new JLabel("Existing routes: "); |
---|
| 453 | currentRoad = new JLabel("Current road: "); |
---|
| 454 | currentPoint = new JLabel("Current point: "); |
---|
| 455 | selectedRoad = new JLabel("Selected road: "); |
---|
| 456 | selectedPoint = new JLabel("Selected point: "); |
---|
| 457 | saveScenario = new JButton("Save"); |
---|
| 458 | cancel = new JButton("Cancel"); |
---|
| 459 | AddNewRoute = new JButton("Make route"); |
---|
| 460 | AddNewEntry = new JButton("Add entry"); |
---|
| 461 | AddNewExit = new JButton("Add exit"); |
---|
| 462 | cancelRoute = new JButton("Cancel route"); |
---|
| 463 | deleteRoute = new JButton("Delete route"); |
---|
| 464 | deleteRoute.setEnabled(false); |
---|
| 465 | cancelRoute.setEnabled(false); |
---|
| 466 | for (int i = 0; i < s.entries.size(); i++) { |
---|
| 467 | routeDraw.entrys.add(s.entries.get(i)); |
---|
| 468 | entry.addItem(String.valueOf(i + 1)); |
---|
| 469 | } |
---|
| 470 | for (int i = 0; i < s.exits.size(); i++) { |
---|
| 471 | routeDraw.exits.add(s.exits.get(i)); |
---|
| 472 | exit.addItem(String.valueOf(i + 1)); |
---|
| 473 | } |
---|
| 474 | for (int i = 0; i < s.allRoutes.size(); i++) { |
---|
| 475 | int cent = 0; |
---|
| 476 | int cex = 0; |
---|
| 477 | for (int j = 0; j < s.entries.size(); j++) { |
---|
| 478 | if (s.entries.get(j).equals(s.allRoutes.get(i).entry)) { |
---|
| 479 | cent = i + 1; |
---|
| 480 | break; |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | for (int j = 0; j < s.exits.size(); j++) { |
---|
| 484 | if (s.exits.get(j).equals(s.allRoutes.get(i).exit)) { |
---|
| 485 | cex = i + 1; |
---|
| 486 | break; |
---|
| 487 | } |
---|
| 488 | } |
---|
| 489 | rutemarcate.addItem((String) (String.valueOf(cent) + "->" + String.valueOf(cex))); |
---|
| 490 | allRoutes.add(new ArrayList<Location>()); |
---|
| 491 | for (int j = 0; j < s.allRoutes.get(i).route.size(); j++) { |
---|
| 492 | allRoutes.get(allRoutes.size() - 1).add( |
---|
| 493 | new Location(s.allRoutes.get(i).route.get(j).roadIndex, s.allRoutes.get(i).route.get(j).pt1)); |
---|
| 494 | allRoutes.get(allRoutes.size() - 1).add( |
---|
| 495 | new Location(s.allRoutes.get(i).route.get(j).roadIndex, s.allRoutes.get(i).route.get(j).pt2)); |
---|
| 496 | } |
---|
| 497 | System.err.println("Am gasit " + allRoutes.size() + " intrari"); |
---|
| 498 | } |
---|
| 499 | SpringLayout.Constraints entryConstraint = layout1.getConstraints(entry); |
---|
| 500 | SpringLayout.Constraints exitConstraint = layout1.getConstraints(exit); |
---|
| 501 | SpringLayout.Constraints addentryConstraint = layout1.getConstraints(AddNewEntry); |
---|
| 502 | SpringLayout.Constraints addexitConstraint = layout1.getConstraints(AddNewExit); |
---|
| 503 | SpringLayout.Constraints addrouteConstraint = layout1.getConstraints(AddNewRoute); |
---|
| 504 | SpringLayout.Constraints cancelConstraint = layout1.getConstraints(cancel); |
---|
| 505 | SpringLayout.Constraints numeSaveConstraint = layout1.getConstraints(numeSave); |
---|
| 506 | SpringLayout.Constraints saveScenarioConstraint = layout1.getConstraints(saveScenario); |
---|
| 507 | SpringLayout.Constraints saveasConstraint = layout1.getConstraints(saveAs); |
---|
| 508 | SpringLayout.Constraints entrysConstraint = layout1.getConstraints(entrys); |
---|
| 509 | SpringLayout.Constraints exitsConstraint = layout1.getConstraints(exits); |
---|
| 510 | SpringLayout.Constraints paramsConstraint = layout1.getConstraints(params); |
---|
| 511 | SpringLayout.Constraints currentpointConstraint = layout1.getConstraints(currentpoint); |
---|
| 512 | SpringLayout.Constraints currentPointConstraint = layout1.getConstraints(currentPoint); |
---|
| 513 | SpringLayout.Constraints currentroadConstraint = layout1.getConstraints(currentroad); |
---|
| 514 | SpringLayout.Constraints currentRoadConstraint = layout1.getConstraints(currentRoad); |
---|
| 515 | SpringLayout.Constraints selectedpointConstraint = layout1.getConstraints(selectedpoint); |
---|
| 516 | SpringLayout.Constraints selectedPointConstraint = layout1.getConstraints(selectedPoint); |
---|
| 517 | SpringLayout.Constraints selectedroadConstraint = layout1.getConstraints(selectedroad); |
---|
| 518 | SpringLayout.Constraints selectedRoadConstraint = layout1.getConstraints(selectedRoad); |
---|
| 519 | SpringLayout.Constraints cancelRouteConstraint = layout1.getConstraints(cancelRoute); |
---|
| 520 | SpringLayout.Constraints rutemarcateConstraints = layout1.getConstraints(rutemarcate); |
---|
| 521 | SpringLayout.Constraints ruteMarcateConstraints = layout1.getConstraints(ruteMarcate); |
---|
| 522 | SpringLayout.Constraints deleteRouteConstraint = layout1.getConstraints(deleteRoute); |
---|
| 523 | |
---|
| 524 | currentRoadConstraint.setX(Spring.constant(10)); |
---|
| 525 | currentRoadConstraint.setY(Spring.constant(10)); |
---|
| 526 | currentRoadConstraint.setWidth(Spring.constant(110)); |
---|
| 527 | currentRoadConstraint.setHeight(Spring.constant(15)); |
---|
| 528 | |
---|
| 529 | currentroadConstraint.setX(Spring.constant(10)); |
---|
| 530 | currentroadConstraint.setY(Spring.constant(35)); |
---|
| 531 | currentroadConstraint.setWidth(Spring.constant(110)); |
---|
| 532 | currentroadConstraint.setHeight(Spring.constant(20)); |
---|
| 533 | |
---|
| 534 | currentPointConstraint.setX(Spring.constant(10)); |
---|
| 535 | currentPointConstraint.setY(Spring.constant(65)); |
---|
| 536 | currentPointConstraint.setWidth(Spring.constant(110)); |
---|
| 537 | currentPointConstraint.setHeight(Spring.constant(15)); |
---|
| 538 | |
---|
| 539 | currentpointConstraint.setX(Spring.constant(10)); |
---|
| 540 | currentpointConstraint.setY(Spring.constant(90)); |
---|
| 541 | currentpointConstraint.setWidth(Spring.constant(110)); |
---|
| 542 | currentpointConstraint.setHeight(Spring.constant(20)); |
---|
| 543 | |
---|
| 544 | selectedRoadConstraint.setX(Spring.constant(10)); |
---|
| 545 | selectedRoadConstraint.setY(Spring.constant(115)); |
---|
| 546 | selectedRoadConstraint.setWidth(Spring.constant(110)); |
---|
| 547 | selectedRoadConstraint.setHeight(Spring.constant(15)); |
---|
| 548 | |
---|
| 549 | selectedroadConstraint.setX(Spring.constant(10)); |
---|
| 550 | selectedroadConstraint.setY(Spring.constant(140)); |
---|
| 551 | selectedroadConstraint.setWidth(Spring.constant(110)); |
---|
| 552 | selectedroadConstraint.setHeight(Spring.constant(20)); |
---|
| 553 | |
---|
| 554 | selectedPointConstraint.setX(Spring.constant(10)); |
---|
| 555 | selectedPointConstraint.setY(Spring.constant(170)); |
---|
| 556 | selectedPointConstraint.setWidth(Spring.constant(110)); |
---|
| 557 | selectedPointConstraint.setHeight(Spring.constant(15)); |
---|
| 558 | |
---|
| 559 | selectedpointConstraint.setX(Spring.constant(10)); |
---|
| 560 | selectedpointConstraint.setY(Spring.constant(195)); |
---|
| 561 | selectedpointConstraint.setWidth(Spring.constant(110)); |
---|
| 562 | selectedpointConstraint.setHeight(Spring.constant(20)); |
---|
| 563 | |
---|
| 564 | addentryConstraint.setX(Spring.constant(10)); |
---|
| 565 | addentryConstraint.setY(Spring.constant(220)); |
---|
| 566 | addentryConstraint.setWidth(Spring.constant(110)); |
---|
| 567 | addentryConstraint.setHeight(Spring.constant(20)); |
---|
| 568 | |
---|
| 569 | addexitConstraint.setX(Spring.constant(10)); |
---|
| 570 | addexitConstraint.setY(Spring.constant(242)); |
---|
| 571 | addexitConstraint.setWidth(Spring.constant(110)); |
---|
| 572 | addexitConstraint.setHeight(Spring.constant(20)); |
---|
| 573 | |
---|
| 574 | ruteMarcateConstraints.setX(Spring.constant(10)); |
---|
| 575 | ruteMarcateConstraints.setY(Spring.constant(270)); |
---|
| 576 | ruteMarcateConstraints.setWidth(Spring.constant(110)); |
---|
| 577 | ruteMarcateConstraints.setHeight(Spring.constant(20)); |
---|
| 578 | |
---|
| 579 | rutemarcateConstraints.setX(Spring.constant(10)); |
---|
| 580 | rutemarcateConstraints.setY(Spring.constant(292)); |
---|
| 581 | rutemarcateConstraints.setWidth(Spring.constant(110)); |
---|
| 582 | rutemarcateConstraints.setHeight(Spring.constant(20)); |
---|
| 583 | |
---|
| 584 | paramsConstraint.setX(Spring.constant(10)); |
---|
| 585 | paramsConstraint.setY(Spring.constant(320)); |
---|
| 586 | paramsConstraint.setWidth(Spring.constant(200)); |
---|
| 587 | paramsConstraint.setHeight(Spring.constant(10)); |
---|
| 588 | |
---|
| 589 | entrysConstraint.setX(Spring.constant(10)); |
---|
| 590 | entrysConstraint.setY(Spring.constant(336)); |
---|
| 591 | entrysConstraint.setWidth(Spring.constant(50)); |
---|
| 592 | entrysConstraint.setHeight(Spring.constant(15)); |
---|
| 593 | |
---|
| 594 | exitsConstraint.setX(Spring.constant(10)); |
---|
| 595 | exitsConstraint.setY(Spring.constant(361)); |
---|
| 596 | exitsConstraint.setWidth(Spring.constant(50)); |
---|
| 597 | exitsConstraint.setHeight(Spring.constant(10)); |
---|
| 598 | |
---|
| 599 | entryConstraint.setX(Spring.constant(50)); |
---|
| 600 | entryConstraint.setY(Spring.constant(335)); |
---|
| 601 | entryConstraint.setWidth(Spring.constant(70)); |
---|
| 602 | entryConstraint.setHeight(Spring.constant(20)); |
---|
| 603 | |
---|
| 604 | exitConstraint.setX(Spring.constant(50)); |
---|
| 605 | exitConstraint.setY(Spring.constant(358)); |
---|
| 606 | exitConstraint.setWidth(Spring.constant(70)); |
---|
| 607 | exitConstraint.setHeight(Spring.constant(20)); |
---|
| 608 | |
---|
| 609 | addrouteConstraint.setX(Spring.constant(10)); |
---|
| 610 | addrouteConstraint.setY(Spring.constant(380)); |
---|
| 611 | addrouteConstraint.setWidth(Spring.constant(110)); |
---|
| 612 | addrouteConstraint.setHeight(Spring.constant(20)); |
---|
| 613 | |
---|
| 614 | cancelRouteConstraint.setX(Spring.constant(10)); |
---|
| 615 | cancelRouteConstraint.setY(Spring.constant(402)); |
---|
| 616 | cancelRouteConstraint.setWidth(Spring.constant(110)); |
---|
| 617 | cancelRouteConstraint.setHeight(Spring.constant(20)); |
---|
| 618 | |
---|
| 619 | deleteRouteConstraint.setX(Spring.constant(10)); |
---|
| 620 | deleteRouteConstraint.setY(Spring.constant(424)); |
---|
| 621 | deleteRouteConstraint.setWidth(Spring.constant(110)); |
---|
| 622 | deleteRouteConstraint.setHeight(Spring.constant(20)); |
---|
| 623 | |
---|
| 624 | saveasConstraint.setX(Spring.constant(10)); |
---|
| 625 | saveasConstraint.setY(Spring.constant(443)); |
---|
| 626 | saveasConstraint.setWidth(Spring.constant(110)); |
---|
| 627 | saveasConstraint.setHeight(Spring.constant(20)); |
---|
| 628 | |
---|
| 629 | numeSaveConstraint.setX(Spring.constant(10)); |
---|
| 630 | numeSaveConstraint.setY(Spring.constant(475)); |
---|
| 631 | numeSaveConstraint.setWidth(Spring.constant(110)); |
---|
| 632 | numeSaveConstraint.setHeight(Spring.constant(20)); |
---|
| 633 | |
---|
| 634 | saveScenarioConstraint.setX(Spring.constant(10)); |
---|
| 635 | saveScenarioConstraint.setY(Spring.constant(493)); |
---|
| 636 | saveScenarioConstraint.setWidth(Spring.constant(110)); |
---|
| 637 | saveScenarioConstraint.setHeight(Spring.constant(20)); |
---|
| 638 | |
---|
| 639 | cancelConstraint.setX(Spring.constant(10)); |
---|
| 640 | cancelConstraint.setY(Spring.constant(520)); |
---|
| 641 | cancelConstraint.setWidth(Spring.constant(110)); |
---|
| 642 | cancelConstraint.setHeight(Spring.constant(20)); |
---|
| 643 | |
---|
| 644 | saveScenario.addActionListener(lal); |
---|
| 645 | cancel.addActionListener(lal); |
---|
| 646 | AddNewRoute.addActionListener(lal); |
---|
| 647 | AddNewEntry.addActionListener(lal); |
---|
| 648 | AddNewExit.addActionListener(lal); |
---|
| 649 | rutemarcate.addActionListener(lal); |
---|
| 650 | deleteRoute.addActionListener(lal); |
---|
| 651 | cancelRoute.addActionListener(lal); |
---|
| 652 | |
---|
| 653 | entryPane.add(params); |
---|
| 654 | entryPane.add(entrys); |
---|
| 655 | entryPane.add(exits); |
---|
| 656 | entryPane.add(entry); |
---|
| 657 | entryPane.add(exit); |
---|
| 658 | entryPane.add(saveAs); |
---|
| 659 | entryPane.add(saveScenario); |
---|
| 660 | entryPane.add(cancel); |
---|
| 661 | entryPane.add(AddNewEntry); |
---|
| 662 | entryPane.add(AddNewExit); |
---|
| 663 | entryPane.add(AddNewRoute); |
---|
| 664 | entryPane.add(numeSave); |
---|
| 665 | entryPane.add(currentPoint); |
---|
| 666 | entryPane.add(currentpoint); |
---|
| 667 | entryPane.add(currentroad); |
---|
| 668 | entryPane.add(currentRoad); |
---|
| 669 | entryPane.add(selectedPoint); |
---|
| 670 | entryPane.add(selectedpoint); |
---|
| 671 | entryPane.add(selectedroad); |
---|
| 672 | entryPane.add(selectedRoad); |
---|
| 673 | entryPane.add(cancelRoute); |
---|
| 674 | entryPane.add(ruteMarcate); |
---|
| 675 | entryPane.add(rutemarcate); |
---|
| 676 | entryPane.add(deleteRoute); |
---|
| 677 | |
---|
| 678 | this.getContentPane().add(entryPane); |
---|
| 679 | |
---|
| 680 | constrain = layout1.getConstraints(entryPane); |
---|
| 681 | constrain.setX(Spring.constant(2)); |
---|
| 682 | constrain.setY(Spring.constant(2)); |
---|
| 683 | constrain.setWidth(Spring.constant(130)); |
---|
| 684 | constrain.setHeight(Spring.constant(580)); |
---|
| 685 | |
---|
| 686 | this.getContentPane().add(this.routeDraw); |
---|
| 687 | routeDraw.setBorder(BorderFactory.createEtchedBorder()); |
---|
| 688 | |
---|
| 689 | constrain = layout1.getConstraints(this.routeDraw); |
---|
| 690 | constrain.setX(Spring.constant(135)); |
---|
| 691 | constrain.setY(Spring.constant(2)); |
---|
| 692 | constrain.setWidth(Spring.constant(1000 - 150)); |
---|
| 693 | constrain.setHeight(Spring.constant(700 - 40)); |
---|
| 694 | |
---|
| 695 | routeDraw.setVisible(true); |
---|
| 696 | routeDraw.repaint(); |
---|
| 697 | this.setSize(1000, 700); |
---|
| 698 | this.setLocation(10, 10); |
---|
| 699 | this.setResizable(false); |
---|
| 700 | this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); |
---|
| 701 | this.setVisible(true); |
---|
| 702 | this.getContentPane().repaint(); |
---|
| 703 | } |
---|
| 704 | |
---|
| 705 | /** |
---|
| 706 | * |
---|
| 707 | * @return |
---|
| 708 | */ |
---|
| 709 | public Scenario getScenario() { |
---|
| 710 | return this.outScenario; |
---|
| 711 | } |
---|
| 712 | |
---|
| 713 | /** |
---|
| 714 | * |
---|
| 715 | * @param outScenario |
---|
| 716 | */ |
---|
| 717 | public void configurationResult(Scenario outScenario) { |
---|
| 718 | this.outScenario = outScenario; |
---|
| 719 | this.setVisible(true); |
---|
| 720 | repaint(); |
---|
| 721 | this.entryConfig = null; |
---|
| 722 | } |
---|
| 723 | } |
---|