/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.opentrafficsimulation.gui; import com.opentrafficsimulation.connector.Connector; import com.opentrafficsimulation.connector.utility.ConnectorType; import com.opentrafficsimulation.editor.light.LightEditor; import com.opentrafficsimulation.gui.utility.AssetUtility; import com.opentrafficsimulation.gui.utility.InputFilterUtility; import com.opentrafficsimulation.screen.simulation.SimulationScreen; import com.opentrafficsimulation.utility.converter.Routes; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultListModel; import javax.swing.JOptionPane; import javax.swing.ListSelectionModel; import javax.swing.SwingWorker; import javax.swing.text.PlainDocument; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; /** * * @author macromania */ public class CreateSimulationFrame extends javax.swing.JFrame { private static String maxSpeed = "100"; private static List<Routes.Vehicle> privateList = new ArrayList<Routes.Vehicle>(); private static List<Routes.Vehicle> busList = new ArrayList<Routes.Vehicle>(); private static List<Routes.Vehicle> taxiList = new ArrayList<Routes.Vehicle>(); private static List<Routes.Vehicle> lorryList = new ArrayList<Routes.Vehicle>(); DefaultListModel model = new DefaultListModel(); private boolean isTrafficGenerated = false; private static CreateSimulationFrame createSimulationFrame = new CreateSimulationFrame(); /** * Creates new form CreateSimulationFrame */ private CreateSimulationFrame() { initComponents(); PlainDocument doc = (PlainDocument) jTextFieldBegin.getDocument(); doc.setDocumentFilter(new InputFilterUtility()); doc = (PlainDocument) jTextFieldEnd.getDocument(); doc.setDocumentFilter(new InputFilterUtility()); doc = (PlainDocument) jTextFieldStepSize.getDocument(); doc.setDocumentFilter(new InputFilterUtility()); doc = (PlainDocument) jTextFieldNoOfVehicle.getDocument(); doc.setDocumentFilter(new InputFilterUtility()); } public static CreateSimulationFrame getInstance() { return createSimulationFrame; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jSeparator3 = new javax.swing.JSeparator(); jPanel1 = new javax.swing.JPanel(); jLabelEditVehicles = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jComboBoxVehicleType = new javax.swing.JComboBox(); jScrollPane1 = new javax.swing.JScrollPane(); jListVehicles = new javax.swing.JList(); jTextFieldNoOfVehicle = new javax.swing.JTextField(); buttonAdd = new javax.swing.JButton(); buttonDelete = new javax.swing.JButton(); buttonReset = new javax.swing.JButton(); buttonCreate = new javax.swing.JButton(); jSeparator4 = new javax.swing.JSeparator(); jSeparator1 = new javax.swing.JSeparator(); jPanel3 = new javax.swing.JPanel(); jLabelEditSimulation = new javax.swing.JLabel(); jLabelBeginTime = new javax.swing.JLabel(); jTextFieldBegin = new javax.swing.JTextField(); jLabelEndTime = new javax.swing.JLabel(); jTextFieldEnd = new javax.swing.JTextField(); jLabelStepSize = new javax.swing.JLabel(); jTextFieldStepSize = new javax.swing.JTextField(); jSeparator2 = new javax.swing.JSeparator(); jButtonEditMap = new javax.swing.JButton(); jButtonPreview = new javax.swing.JButton(); jButtonRunSimulation = new javax.swing.JButton(); jLabelEditTrafficLights = new javax.swing.JLabel(); jPanel5 = new javax.swing.JPanel(); jLabelTrafficLights = new javax.swing.JLabel(); jLabelJunctions = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); jListTrafficLights = new javax.swing.JList(); jScrollPane3 = new javax.swing.JScrollPane(); jListJunctions = new javax.swing.JList(); jButtonConvertToTrafficLight = new javax.swing.JButton(); jButtonRemoveTrafficLight = new javax.swing.JButton(); jButtonReset = new javax.swing.JButton(); jPanel4 = new javax.swing.JPanel(); jLabelLightCount = new javax.swing.JLabel(); jButtonGenerateRandom = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Create Simulation - Open Traffic Simulation"); setIconImage(new AssetUtility().getLogo()); setPreferredSize(new java.awt.Dimension(680, 560)); setResizable(false); jLabelEditVehicles.setBackground(new java.awt.Color(227, 225, 225)); jLabelEditVehicles.setFont(new java.awt.Font("Arial", 0, 30)); // NOI18N jLabelEditVehicles.setForeground(new java.awt.Color(30, 15, 42)); jLabelEditVehicles.setText("Edit Traffic Intensity"); jLabel1.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabel1.setText("Select Vehicle Type"); jLabel2.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabel2.setText("Added Vehicles"); jComboBoxVehicleType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Private", "Bus", "Lorry", "Taxi", " " })); jListVehicles.setModel(model); jScrollPane1.setViewportView(jListVehicles); buttonAdd.setText("Add Vehicle"); buttonAdd.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonAddActionPerformed(evt); } }); buttonDelete.setText(" Delete Selected"); buttonDelete.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonDeleteActionPerformed(evt); } }); buttonReset.setText("Reset"); buttonReset.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonResetActionPerformed(evt); } }); buttonCreate.setText("Generate Vehicles"); buttonCreate.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonCreateActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jSeparator4) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel1) .addComponent(jComboBoxVehicleType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTextFieldNoOfVehicle) .addComponent(buttonAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(buttonDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))) .addComponent(jLabelEditVehicles) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(buttonCreate, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(50, 50, 50))) .addGap(0, 0, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabelEditVehicles) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jLabel2)) .addGap(5, 5, 5) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jComboBoxVehicleType, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jTextFieldNoOfVehicle, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(7, 7, 7) .addComponent(buttonDelete) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(buttonCreate, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) .addComponent(buttonReset, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(0, 0, 0)) ); jLabelEditSimulation.setBackground(new java.awt.Color(227, 225, 225)); jLabelEditSimulation.setFont(new java.awt.Font("Arial", 0, 30)); // NOI18N jLabelEditSimulation.setForeground(new java.awt.Color(30, 15, 42)); jLabelEditSimulation.setText("Simulation Options"); jLabelBeginTime.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jLabelBeginTime.setText("Begin Time"); jTextFieldBegin.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jTextFieldBegin.setText("0"); jLabelEndTime.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jLabelEndTime.setText("End Time"); jLabelEndTime.setToolTipText(""); jTextFieldEnd.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jTextFieldEnd.setText("1000"); jLabelStepSize.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jLabelStepSize.setText("Step Size"); jTextFieldStepSize.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jTextFieldStepSize.setText("1000"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelEditSimulation) .addGroup(jPanel3Layout.createSequentialGroup() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelBeginTime) .addComponent(jTextFieldBegin, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelEndTime) .addComponent(jTextFieldEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextFieldStepSize, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabelStepSize)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel3Layout.setVerticalGroup( jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabelEditSimulation) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelBeginTime) .addComponent(jLabelEndTime) .addComponent(jLabelStepSize)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextFieldBegin, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextFieldStepSize, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 28, Short.MAX_VALUE)) ); jButtonEditMap.setText("Edit Map"); jButtonEditMap.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonEditMapActionPerformed(evt); } }); jButtonPreview.setText("Preview"); jButtonPreview.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonPreviewActionPerformed(evt); } }); jButtonRunSimulation.setText("Run The Simulation"); jButtonRunSimulation.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonRunSimulationActionPerformed(evt); } }); jLabelEditTrafficLights.setBackground(new java.awt.Color(227, 225, 225)); jLabelEditTrafficLights.setFont(new java.awt.Font("Arial", 0, 30)); // NOI18N jLabelEditTrafficLights.setForeground(new java.awt.Color(30, 15, 42)); jLabelEditTrafficLights.setText("Edit Traffic Lights"); jLabelTrafficLights.setText("Traffic Lights"); jLabelJunctions.setText("Junctions"); jListTrafficLights.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane2.setViewportView(jListTrafficLights); jListJunctions.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane3.setViewportView(jListJunctions); jButtonConvertToTrafficLight.setText("Add Traffic Light"); jButtonConvertToTrafficLight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonConvertToTrafficLightActionPerformed(evt); } }); jButtonRemoveTrafficLight.setText("Remove"); jButtonRemoveTrafficLight.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonRemoveTrafficLightActionPerformed(evt); } }); jButtonReset.setText("Reset"); jButtonReset.setEnabled(false); jButtonReset.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonResetActionPerformed(evt); } }); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelTrafficLights) .addGroup(jPanel5Layout.createSequentialGroup() .addComponent(jButtonRemoveTrafficLight) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonReset)) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButtonConvertToTrafficLight) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabelJunctions)) .addContainerGap()) ); jPanel5Layout.setVerticalGroup( jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup() .addGap(1, 1, 1) .addComponent(jLabelTrafficLights)) .addComponent(jLabelJunctions)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonConvertToTrafficLight) .addComponent(jButtonRemoveTrafficLight) .addComponent(jButtonReset)) .addGap(6, 6, 6)) ); jLabelLightCount.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N jLabelLightCount.setText("Click to generate traffic lights randomly."); jButtonGenerateRandom.setText("Generate Randomly"); jButtonGenerateRandom.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonGenerateRandomActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelLightCount) .addComponent(jButtonGenerateRandom)) .addGap(0, 0, Short.MAX_VALUE)) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup() .addComponent(jLabelLightCount) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButtonGenerateRandom) .addContainerGap(20, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButtonEditMap, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jButtonPreview, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonRunSimulation, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jSeparator2) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(51, 51, 51) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelEditTrafficLights) .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup() .addGap(61, 61, 61) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(0, 15, Short.MAX_VALUE))) .addGap(10, 10, 10)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jSeparator1) .addContainerGap()))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup() .addComponent(jLabelEditTrafficLights) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(3, 3, 3) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jButtonEditMap, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addComponent(jButtonRunSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonPreview, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap(59, Short.MAX_VALUE)) ); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void clearList() { privateList.clear(); busList.clear(); lorryList.clear(); taxiList.clear(); model.removeAllElements(); System.gc(); isTrafficGenerated = false; } private void lockEditVehicles(boolean state) { buttonAdd.setEnabled(state); buttonCreate.setEnabled(state); buttonDelete.setEnabled(state); buttonReset.setEnabled(state); jListVehicles.setEnabled(state); } private void updateLightAndJunctions() { DefaultListModel jm = new DefaultListModel(); DefaultListModel tm = new DefaultListModel(); for (int i = 0; i < LightEditor.getInstance().tlModel.size(); i++) { tm.addElement(LightEditor.getInstance().tlModel.get(i)); } for (int i = 0; i < LightEditor.getInstance().junctionModel.size(); i++) { jm.addElement(LightEditor.getInstance().junctionModel.get(i)); } System.out.println("Lights:" +LightEditor.getInstance().tlModel); System.out.println("Junctions:" +LightEditor.getInstance().junctionModel); if (tm.size() > 0) { jButtonRemoveTrafficLight.setEnabled(true); } else{ jButtonRemoveTrafficLight.setEnabled(false); } if (jm.size() > 0) { jButtonConvertToTrafficLight.setEnabled(true); } else{ jButtonConvertToTrafficLight.setEnabled(false); } jLabelTrafficLights.setText("Traffic Lights (" + tm.size() + ")"); jLabelJunctions.setText("Junctions (" + jm.size() + ")"); jListTrafficLights.setModel(tm); jListJunctions.setModel(jm); } public void createJunctions() { /*jButtonGenerateLights.setEnabled(false); jButtonGenerateLights.setText("Generating...");*/ SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { LightEditor.getInstance().triggerGeneration(); return "running"; } @Override public void done() { /*jLabelLightCount.setText(LightEditor.getInstance().trafficLightIDs.size() + " junctions have traffic lights created!"); System.out.println("generation completed");*/ updateLightAndJunctions(); } }; worker.execute(); } private void jButtonEditMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditMapActionPerformed this.setVisible(false); CreateMapFrame.getInstance().setVisible(true); }//GEN-LAST:event_jButtonEditMapActionPerformed private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed boolean isValid = true; int numOfVehicles = 0; if (jTextFieldNoOfVehicle.getText().length() < 1) { isValid = false; JOptionPane.showMessageDialog(null, "Please enter number of vehicles value!"); } else { numOfVehicles = Integer.parseInt(jTextFieldNoOfVehicle.getText()); if (numOfVehicles <= 0) { isValid = false; JOptionPane.showMessageDialog(null, "Please enter a positive number for number of vehicles value!"); } else { if (numOfVehicles > 1000) { isValid = false; JOptionPane.showMessageDialog(null, "You can only add 1000 vehicle at once! Please update you input."); } } } if (isValid) { SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { buttonAdd.setText("Generating..."); lockEditVehicles(false); String net = CreateMapFrame.getInstance().netgenerate_file + ".net.xml"; Connector connector = new Connector(ConnectorType.CONNECTOR_PYTHON); String Option; { Option = connector.getToolsDir() + "sumolib\\trip\\randomTrips.py -n " + connector.getOutputDir() + net; Option += " -e " + jTextFieldNoOfVehicle.getText() + " -l"; Option += " -o " + connector.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + ".trips.xml"; connector.runCommand(Option); } Connector connectorRouter = new Connector(ConnectorType.CONNECTOR_ROUTER); String Options1; { Options1 = "-n " + connectorRouter.getOutputDir(); Options1 += net; Options1 += " -t " + connectorRouter.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + ".trips.xml"; Options1 += " -o " + connectorRouter.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + "Old.rou.xml"; connectorRouter.runCommand(Options1); } model.removeAllElements(); // create JAXB context and instantiate marshaller JAXBContext context; try { context = JAXBContext.newInstance(Routes.class); javax.xml.bind.Unmarshaller m = context.createUnmarshaller(); Routes bookstore2 = (Routes) m.unmarshal(new FileReader(connectorRouter.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + "Old.rou.xml")); List<Routes.Vehicle> list = bookstore2.getVehicle(); for (Routes.Vehicle vehicle : list) { vehicle.setMaxSpeed(maxSpeed); String vehicleType = ""; if (jComboBoxVehicleType.getSelectedIndex() == 0) { vehicle.setVehicleType("Private"); privateList.add(vehicle); } else if (jComboBoxVehicleType.getSelectedIndex() == 1) { vehicle.setVehicleType("Bus"); busList.add(vehicle); } else if (jComboBoxVehicleType.getSelectedIndex() == 2) { vehicle.setVehicleType("Lorry"); lorryList.add(vehicle); } else if (jComboBoxVehicleType.getSelectedIndex() == 3) { vehicle.setVehicleType("Taxi"); taxiList.add(vehicle); } } if (privateList.size() > 0) { model.addElement("Private = " + privateList.size()); } if (busList.size() > 0) { model.addElement("Bus = " + busList.size()); } if (lorryList.size() > 0) { model.addElement("Lorry = " + lorryList.size()); } if (taxiList.size() > 0) { model.addElement("Taxi = " + taxiList.size()); } jTextFieldNoOfVehicle.setText(""); } catch (JAXBException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } return "running"; } @Override public void done() { buttonAdd.setText("Add Vehicle"); lockEditVehicles(true); } }; worker.execute(); } }//GEN-LAST:event_buttonAddActionPerformed private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed ListSelectionModel selmodel = jListVehicles.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) { String value = (String) model.get(index); if (value.contains("Private")) { privateList.clear(); } if (value.contains("Bus")) { busList.clear(); } if (value.contains("Taxi")) { taxiList.clear(); } if (value.contains("Lorry")) { lorryList.clear(); } model.remove(index); } else { JOptionPane.showMessageDialog(this, "Select a value from the list to delete"); } }//GEN-LAST:event_buttonDeleteActionPerformed private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed clearList(); }//GEN-LAST:event_buttonResetActionPerformed private void buttonCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCreateActionPerformed boolean isValid = true; int index = model.getSize(); if (index <= 0) { isValid = false; } if (isValid) { SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { buttonCreate.setText("Generating..."); lockEditVehicles(false); Connector connector = new Connector(null); List<Routes.Vehicle> commonList = new ArrayList<Routes.Vehicle>(); int i = 0; for (Routes.Vehicle privateVehicle : privateList) { privateVehicle.setId(Integer.toString(i)); privateVehicle.setDepart(Integer.toString(i) + ".00"); privateVehicle.setColor("0,180,255"); privateVehicle.setGuiShape("evehicle"); commonList.add(privateVehicle); i++; } for (Routes.Vehicle bus : busList) { bus.setId(Integer.toString(i)); bus.setDepart(Integer.toString(i) + ".00"); bus.setColor("255,0,0"); bus.setGuiShape("bus"); commonList.add(bus); i++; } for (Routes.Vehicle lorry : lorryList) { lorry.setId(Integer.toString(i)); lorry.setDepart(Integer.toString(i) + ".00"); lorry.setColor("132,255,0"); lorry.setGuiShape("trailer"); commonList.add(lorry); i++; } for (Routes.Vehicle taxi : taxiList) { taxi.setId(Integer.toString(i)); taxi.setDepart(Integer.toString(i) + ".00"); taxi.setColor("138,138,138"); taxi.setGuiShape("bicycle"); commonList.add(taxi); i++; } try { // create JAXB context and instantiate marshaller JAXBContext context = JAXBContext.newInstance(Routes.class); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Routes route = new Routes(); route.getVehicle().addAll(commonList); // Write to System.out m.marshal(route, System.out); // Write to File m.marshal(route, new File(connector.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + ".rou.xml")); //clearList(); JOptionPane.showMessageDialog(null, "Vehicles genereated successfully"); isTrafficGenerated = true; } catch (JAXBException e) { System.out.println("Exception Occured"); JOptionPane.showMessageDialog(null, "An exception has been occured! Please try again later."); } return "running"; } @Override public void done() { buttonCreate.setText("Generate Vehicles"); lockEditVehicles(true); } }; worker.execute(); } else { JOptionPane.showMessageDialog(null, "Please add vehicles!"); } }//GEN-LAST:event_buttonCreateActionPerformed private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed jLabelLightCount.setText("You haven't created any traffic lights"); jButtonReset.setEnabled(false); LightEditor.getInstance().resetLights(); }//GEN-LAST:event_jButtonResetActionPerformed private void jButtonPreviewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPreviewActionPerformed SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { Connector connector = new Connector(ConnectorType.CONNECTOR_SUMO_GUI); String options; options = " --net-file=" + connector.getOutputDir() + CreateMapFrame.getInstance().netgenerate_file + ".net.xml"; connector.runCommand(options); return "running"; } @Override public void done() { } }; worker.execute(); }//GEN-LAST:event_jButtonPreviewActionPerformed private void jButtonRunSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRunSimulationActionPerformed boolean isValid = true; if (jTextFieldBegin.getText().length() < 1 || jTextFieldEnd.getText().length() < 1 || jTextFieldStepSize.getText().length() < 1) { isValid = false; JOptionPane.showConfirmDialog(null, "Please set simulation input values!"); } else { int begin = Integer.parseInt(jTextFieldBegin.getText()); int end = Integer.parseInt(jTextFieldEnd.getText()); int stepSize = Integer.parseInt(jTextFieldStepSize.getText()); if (end <= 0 || stepSize <= 0) { isValid = false; JOptionPane.showMessageDialog(null, "Please enter positive numbers for simulation input values!"); } else { if (stepSize > 1000) { isValid = false; JOptionPane.showMessageDialog(null, "Step size is too big! Please enter less or equal then 1000!"); } if (end > stepSize) { isValid = false; JOptionPane.showMessageDialog(null, "End time couldn't be bigger then step size!"); } if (begin >= end) { isValid = false; JOptionPane.showMessageDialog(null, "Please enter begin time as smaller then end time!"); } if (isValid) { if (isTrafficGenerated) { SimulationScreen.SIMULATION_BEGIN = begin; SimulationScreen.SIMULATION_END = end; SimulationScreen.SIMULATION_TIME = stepSize; SimulationScreen.getInstance().runSimulation(); } else { JOptionPane.showMessageDialog(null, "Please generate traffic by clicking Generate Vehicles button."); } } } } }//GEN-LAST:event_jButtonRunSimulationActionPerformed private void jButtonConvertToTrafficLightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConvertToTrafficLightActionPerformed if (jListJunctions.getSelectedIndex() >= 0) { SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { jButtonConvertToTrafficLight.setEnabled(false); jButtonConvertToTrafficLight.setText("Converting.."); LightEditor.getInstance().convertToTrafficLight(String.valueOf(jListJunctions.getSelectedValue())); return "running"; } @Override public void done() { jButtonConvertToTrafficLight.setEnabled(true); jButtonConvertToTrafficLight.setText("Add Traffic Light"); updateLightAndJunctions(); } }; worker.execute(); } }//GEN-LAST:event_jButtonConvertToTrafficLightActionPerformed private void jButtonRemoveTrafficLightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveTrafficLightActionPerformed if (jListTrafficLights.getSelectedIndex() >= 0) { SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { jButtonRemoveTrafficLight.setEnabled(false); String id = String.valueOf(jListTrafficLights.getSelectedValue()); LightEditor.getInstance().removeTrafficLight(id); return "running"; } @Override public void done() { jButtonRemoveTrafficLight.setEnabled(true); updateLightAndJunctions(); } }; worker.execute(); } }//GEN-LAST:event_jButtonRemoveTrafficLightActionPerformed private void jButtonGenerateRandomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGenerateRandomActionPerformed SwingWorker worker = new SwingWorker<String, Object>() { @Override public String doInBackground() { jButtonGenerateRandom.setEnabled(false); jButtonGenerateRandom.setText("Generating"); LightEditor.getInstance().generateRandomly(); return "running"; } @Override public void done() { jButtonGenerateRandom.setEnabled(true); jButtonGenerateRandom.setText("Generate Randomly"); updateLightAndJunctions(); jButtonReset.setEnabled(true); } }; worker.execute(); }//GEN-LAST:event_jButtonGenerateRandomActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(CreateSimulationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(CreateSimulationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(CreateSimulationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(CreateSimulationFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CreateSimulationFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buttonAdd; private javax.swing.JButton buttonCreate; private javax.swing.JButton buttonDelete; private javax.swing.JButton buttonReset; private javax.swing.JButton jButtonConvertToTrafficLight; private javax.swing.JButton jButtonEditMap; private javax.swing.JButton jButtonGenerateRandom; private javax.swing.JButton jButtonPreview; private javax.swing.JButton jButtonRemoveTrafficLight; private javax.swing.JButton jButtonReset; private javax.swing.JButton jButtonRunSimulation; private javax.swing.JComboBox jComboBoxVehicleType; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabelBeginTime; private javax.swing.JLabel jLabelEditSimulation; private javax.swing.JLabel jLabelEditTrafficLights; private javax.swing.JLabel jLabelEditVehicles; private javax.swing.JLabel jLabelEndTime; private javax.swing.JLabel jLabelJunctions; private javax.swing.JLabel jLabelLightCount; private javax.swing.JLabel jLabelStepSize; private javax.swing.JLabel jLabelTrafficLights; public javax.swing.JList jListJunctions; public javax.swing.JList jListTrafficLights; private javax.swing.JList jListVehicles; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JSeparator jSeparator4; private javax.swing.JTextField jTextFieldBegin; private javax.swing.JTextField jTextFieldEnd; private javax.swing.JTextField jTextFieldNoOfVehicle; private javax.swing.JTextField jTextFieldStepSize; // End of variables declaration//GEN-END:variables }