/************************************************************************************ * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University * All rights reserved. * Refer to LICENSE for terms and conditions of use. ***********************************************************************************/ package vnsim.gui.selector; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.StringTokenizer; import javax.swing.BorderFactory; import javax.swing.DefaultListModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.Spring; import javax.swing.SpringLayout; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import vnsim.gui.MapLoader; import vnsim.vehicular.scenarios.*; import vnsim.vehicular.simulator.Location; public class ScenarioConfiguration extends JFrame { private static final long serialVersionUID = -2314871286334244756L; int selectedEntryIdx = 0; JPanel entryPane, mapPanel; ScenarioMap inScenario; Scenario outScenario; JButton startSim, addParam, cancel, AddNewRoute, CancelAdd; static MapPanel mapRepresentation; EntryExitConfig entryConfig; JComboBox entry; JTextField numeSave; JLabel saveAs, entryLabel, exitLabel; static ScenarioConfiguration backRef = null; private ScenarioDesigner sd = null; LocalActionListener lal; public class LocalActionListener implements ActionListener, ListSelectionListener { public void actionPerformed(ActionEvent e) { if (e.getSource().equals(entry)) { entryConfig.setNewEntry(inScenario.entries.get(entry.getSelectedIndex())); } if (e.getSource().equals(cancel)) { backRef.setVisible(false); MapLoader.show(true); } if (e.getSource().equals(startSim)) { setVisible(false); String s = null; s = numeSave.getText(); if (s != null) { outScenario.name = s; } entryConfig.saveChanges(); sd.afterExit(); } if (e.getSource().equals(AddNewRoute)) { if (AddNewRoute.getText().equals("Add a new route")) { AddNewRoute.setText("Add the route"); CancelAdd.setEnabled(true); entryConfig.disableIt(); startSim.setEnabled(false); entry.setEnabled(false); mapRepresentation.routePoints = null; } else { AddNewRoute.setText("Add a new route"); CancelAdd.setEnabled(false); entryConfig.enableIt(); startSim.setEnabled(true); entry.setEnabled(true); Location ex = null, en = null; if (mapRepresentation.currentRoute != null) { String s = entry.getSelectedItem().toString(); Route rt = mapRepresentation.currentRoute; StringTokenizer st = new StringTokenizer(s); st.nextToken(); int ent = Integer.valueOf((st.nextToken())); s = entryConfig.exitSelect.getSelectedItem().toString(); st = new StringTokenizer(s); st.nextToken(); int ext = Integer.valueOf((st.nextToken())); en = new Location(inScenario.entries.get(ent).roadIdx, inScenario.entries.get(ent).ptIdx); ex = new Location(inScenario.exits.get(ext).roadIdx, inScenario.exits.get(ext).ptIdx); rt.entry = en; rt.exit = ex; inScenario.allRoutes.add(rt); outScenario.allRoutes.add(rt); } mapRepresentation.routePoints = null; } } if (e.getSource().equals(CancelAdd)) { AddNewRoute.setText("Add a route"); CancelAdd.setEnabled(false); entryConfig.enableIt(); startSim.setEnabled(true); entry.setEnabled(true); } } public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (entry.getSelectedIndex() != selectedEntryIdx) { selectedEntryIdx = entry.getSelectedIndex(); Location newEntry = inScenario.entries.get(entry.getSelectedIndex()); entryConfig.setNewEntry(newEntry); } } } } public void addEntry(Location l) { inScenario.entries.add(l); inScenario.exits.add(l); outScenario.entries.add(l); outScenario.exits.add(l); } public static final ScenarioConfiguration getInstance(ScenarioDesigner sd, ScenarioMap s, Scenario outScen) { if (backRef != null) { backRef.dispose(); backRef = null; } try { backRef = new ScenarioConfiguration(sd, s, outScen); } catch (Exception e) { return null; } return backRef; } private ScenarioConfiguration(ScenarioDesigner sd, ScenarioMap s, Scenario outScen) throws Exception { super("Configure simulation!"); if (s.allRoutes.size() == 0) { throw new Exception(); } this.inScenario = s; this.sd = sd; System.err.println("##" + inScenario.exits.size()); if (outScen == null) { this.outScenario = new Scenario(s.mapFileName, s.entries, s.exits, s.driverTypes, s.allRoutes, s.name); } else { this.outScenario = outScen; } int hight = 500 + (50 * s.driverTypes.size()); SpringLayout layout1 = new SpringLayout(); SpringLayout layout2 = new SpringLayout(); SpringLayout layout3 = new SpringLayout(); this.setLayout(layout1); entryPane = new JPanel(); entryPane.setLayout(layout2); entryPane.setBorder(BorderFactory.createEtchedBorder()); mapPanel = new JPanel(); mapPanel.setLayout(layout3); mapPanel.setBorder(BorderFactory.createEtchedBorder()); this.entryConfig = new EntryExitConfig(this, inScenario, inScenario.entries.get(0), outScenario); SpringLayout.Constraints constrain; lal = new LocalActionListener(); entry = new JComboBox(); for (int i = 0; i < s.entries.size(); i++) { entry.addItem("Entry " + (int) (i)); } entry.setSelectedIndex(0); entry.addActionListener(lal); entryLabel = new JLabel("Select an entry:"); numeSave = new JTextField(); numeSave.setText(s.name); saveAs = new JLabel("Save scenario as:"); startSim = new JButton("Save"); cancel = new JButton("Cancel"); AddNewRoute = new JButton("Add a new route"); CancelAdd = new JButton("Cancel current route"); CancelAdd.setEnabled(false); SpringLayout.Constraints mapReprezentationConstraint = layout3.getConstraints(mapRepresentation); mapReprezentationConstraint.setX(Spring.constant(0)); mapReprezentationConstraint.setY(Spring.constant(0)); mapReprezentationConstraint.setWidth(Spring.constant(hight-10)); mapReprezentationConstraint.setHeight(Spring.constant(hight-10)); SpringLayout.Constraints entryLabelConstraint = layout2.getConstraints(entryLabel); SpringLayout.Constraints entryConstraint = layout2.getConstraints(entry); SpringLayout.Constraints entryConfigConstraint = layout2.getConstraints(entryConfig); SpringLayout.Constraints startSimConstraint = layout2.getConstraints(startSim); SpringLayout.Constraints cancelConstraint = layout2.getConstraints(cancel); SpringLayout.Constraints numeSaveConstraint = layout2.getConstraints(numeSave); entryLabelConstraint.setX(Spring.constant(1)); entryLabelConstraint.setY(Spring.constant(1)); entryLabelConstraint.setWidth(Spring.constant(193)); entryLabelConstraint.setHeight(Spring.constant(20)); entryConstraint.setX(Spring.constant(1)); entryConstraint.setY(Spring.constant(25)); entryConstraint.setWidth(Spring.constant(193)); entryConstraint.setHeight(Spring.constant(20)); entryConfigConstraint.setX(Spring.constant(1)); entryConfigConstraint.setY(Spring.constant(50)); entryConfigConstraint.setWidth(Spring.constant(200)); entryConfigConstraint.setHeight(Spring.constant(hight-450)); constrain = layout2.getConstraints(AddNewRoute); constrain.setX(Spring.constant(1)); constrain.setY(Spring.constant((hight - 200))); constrain.setWidth(Spring.constant(193)); constrain.setHeight(Spring.constant(20)); constrain = layout2.getConstraints(CancelAdd); constrain.setX(Spring.constant(1)); constrain.setY(Spring.constant((hight - 175))); constrain.setWidth(Spring.constant(193)); constrain.setHeight(Spring.constant(20)); numeSaveConstraint.setX(Spring.constant(1)); numeSaveConstraint.setY(Spring.constant((hight - 125))); numeSaveConstraint.setWidth(Spring.constant(193)); numeSaveConstraint.setHeight(Spring.constant(20)); constrain = layout2.getConstraints(saveAs); constrain.setX(Spring.constant(1)); constrain.setY(Spring.constant(hight - 150)); constrain.setWidth(Spring.constant(193)); constrain.setHeight(Spring.constant(20)); startSimConstraint.setX(Spring.constant(1)); startSimConstraint.setY(Spring.constant((hight - 100))); startSimConstraint.setWidth(Spring.constant(93)); startSimConstraint.setHeight(Spring.constant(20)); cancelConstraint.setX(Spring.constant(100)); cancelConstraint.setY(Spring.constant((hight - 100))); cancelConstraint.setWidth(Spring.constant(93)); cancelConstraint.setHeight(Spring.constant(20)); startSim.addActionListener(lal); cancel.addActionListener(lal); AddNewRoute.addActionListener(lal); CancelAdd.addActionListener(lal); entryPane.add(entryLabel); entryPane.add(entry); entryPane.add(numeSave); entryPane.add(saveAs); entryPane.add(startSim); entryPane.add(cancel); entryPane.add(AddNewRoute); entryPane.add(CancelAdd); entryPane.add(entryConfig); mapPanel.add(mapRepresentation); constrain = layout1.getConstraints(entryPane); constrain.setX(Spring.constant(1)); constrain.setY(Spring.constant(1)); constrain.setWidth(Spring.constant(205)); constrain.setHeight(Spring.constant(hight)); constrain = layout1.getConstraints(mapPanel); constrain.setX(Spring.constant(210)); constrain.setY(Spring.constant(1)); constrain.setWidth(Spring.constant(hight - 5)); constrain.setHeight(Spring.constant(hight)); this.add(entryPane); this.add(mapPanel); this.setSize(hight+212, hight + 37); this.setLocation(10, 10); this.setResizable(false); this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setVisible(true); this.getContentPane().repaint(); entryConfig.setNewEntry(inScenario.entries.get(0)); } public Scenario getScenario() { return this.outScenario; } public void configurationResult(Scenario outScenario) { this.outScenario = outScenario; this.setVisible(true); this.entryConfig = null; } }