/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.opentrafficsimulation.gui; import com.opentrafficsimulation.connector.TraciConnector; import com.opentrafficsimulation.editor.light.LightEditor; import com.opentrafficsimulation.editor.light.ReadTrafficLightState; import com.opentrafficsimulation.gui.utility.AssetUtility; import com.opentrafficsimulation.screen.simulation.SimulationScreen; import javax.swing.DefaultListModel; import javax.swing.JOptionPane; /** * * @author macromania */ public class EditTrafficLights extends javax.swing.JFrame { /** * Creates new form EditTrafficLights */ public EditTrafficLights() { initComponents(); DefaultListModel model = new DefaultListModel(); for (int i = 0; i < LightEditor.getInstance().tlModel.size(); i++) { model.addElement(LightEditor.getInstance().tlModel.get(i)); } jListLights.setModel(model); jPanelEdit.setVisible(false); } public void openEditor(String id) { try { String state = TraciConnector.RTL.readTLState(id); jLabelState.setText(state); jPanelEdit.setVisible(true); jButtonEdit.setEnabled(false); jListLights.setEnabled(false); } catch (Exception e) { e.printStackTrace(); } } /** * 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() { jLabelTitle = new javax.swing.JLabel(); jLabelInfo = new javax.swing.JLabel(); jSeparator1 = new javax.swing.JSeparator(); jSeparator2 = new javax.swing.JSeparator(); jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jListLights = new javax.swing.JList(); jPanel2 = new javax.swing.JPanel(); jButtonShow = new javax.swing.JButton(); jLabelStatus = new javax.swing.JLabel(); jButtonEdit = new javax.swing.JButton(); jPanelEdit = new javax.swing.JPanel(); jLabel2 = new javax.swing.JLabel(); jLabelState = new javax.swing.JLabel(); jSeparator3 = new javax.swing.JSeparator(); jButtonCloseEditor = new javax.swing.JButton(); jButtonRed = new javax.swing.JButton(); jButtonGreen = new javax.swing.JButton(); jLabelSelectedID = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Traffic Lights - Open Traffic Simulation"); setAlwaysOnTop(true); setAutoRequestFocus(false); setIconImage(new AssetUtility().getLogo()); setPreferredSize(new java.awt.Dimension(410, 600)); setResizable(false); jLabelTitle.setBackground(new java.awt.Color(227, 225, 225)); jLabelTitle.setFont(new java.awt.Font("Arial", 0, 30)); // NOI18N jLabelTitle.setForeground(new java.awt.Color(30, 15, 42)); jLabelTitle.setText("Traffic Light Control Panel"); jLabelInfo.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabelInfo.setText("<html>\n<body>\n<p style=\"width:350px;\">\nThis panel lists available traffic lights in the running simulation. You can locate requested traffic light on map change its state instantly during the simulation.\n</p>\n</body>\n</html>"); // NOI18N jLabel1.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabel1.setText("Select Traffic Lights"); jLabel1.setToolTipText(""); jListLights.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jScrollPane1.setViewportView(jListLights); 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(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addGap(0, 20, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(4, 4, 4) .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jButtonShow.setText("Show On Map"); jButtonShow.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonShowActionPerformed(evt); } }); jButtonEdit.setText("Edit State"); jButtonEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonEditActionPerformed(evt); } }); jPanelEdit.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jLabel2.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabel2.setText("Status:"); jLabelState.setForeground(new java.awt.Color(51, 51, 255)); jLabelState.setText("jLabel3"); jButtonCloseEditor.setText("Close"); jButtonCloseEditor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonCloseEditorActionPerformed(evt); } }); jButtonRed.setBackground(new java.awt.Color(204, 0, 0)); jButtonRed.setForeground(new java.awt.Color(255, 0, 0)); jButtonRed.setText("Set RED"); jButtonRed.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonRedActionPerformed(evt); } }); jButtonGreen.setBackground(new java.awt.Color(0, 153, 0)); jButtonGreen.setForeground(new java.awt.Color(0, 153, 0)); jButtonGreen.setText("Set GREEN"); jButtonGreen.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonGreenActionPerformed(evt); } }); jLabelSelectedID.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N jLabelSelectedID.setText("jLabel3"); javax.swing.GroupLayout jPanelEditLayout = new javax.swing.GroupLayout(jPanelEdit); jPanelEdit.setLayout(jPanelEditLayout); jPanelEditLayout.setHorizontalGroup( jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelEditLayout.createSequentialGroup() .addContainerGap() .addGroup(jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelEditLayout.createSequentialGroup() .addComponent(jSeparator3) .addGap(20, 20, 20)) .addGroup(jPanelEditLayout.createSequentialGroup() .addGroup(jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelEditLayout.createSequentialGroup() .addComponent(jButtonRed) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonGreen)) .addGroup(jPanelEditLayout.createSequentialGroup() .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabelState))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanelEditLayout.createSequentialGroup() .addComponent(jLabelSelectedID) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButtonCloseEditor) .addContainerGap()))) ); jPanelEditLayout.setVerticalGroup( jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelEditLayout.createSequentialGroup() .addContainerGap() .addGroup(jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonCloseEditor) .addComponent(jLabelSelectedID)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelState) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanelEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButtonRed) .addComponent(jButtonGreen)) .addGap(0, 11, Short.MAX_VALUE)) ); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(jLabelStatus, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButtonShow, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE) .addComponent(jButtonEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jPanelEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 55, Short.MAX_VALUE)) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jButtonShow, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButtonEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabelStatus) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanelEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(40, 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() .addGap(20, 20, 20) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jLabelTitle) .addComponent(jSeparator1) .addComponent(jLabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jSeparator2)) .addContainerGap(80, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jLabelTitle) .addGap(5, 5, 5) .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(3, 3, 3) .addComponent(jLabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addGap(43, 43, 43) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(275, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jButtonShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonShowActionPerformed if (jListLights.getSelectedIndex() >= 0) { String id = String.valueOf(jListLights.getSelectedValue()); System.out.println("Selected Light: " + id); try { TraciConnector.GOTO_JUNCTION = id; } catch (Exception exception) { JOptionPane.showMessageDialog(null, "Could not connect to Traci simulation"); exception.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "Please select a traffic list from the list!"); } }//GEN-LAST:event_jButtonShowActionPerformed private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed if (jListLights.getSelectedIndex() >= 0) { String id = String.valueOf(jListLights.getSelectedValue()); System.out.println("Selected Light: " + id); try { jLabelSelectedID.setText("Editing " + id); TraciConnector.TLID = id; /*String state = TraciConnector.RTL.readTLState(id); jLabelState.setText(state);*/ } catch (Exception exception) { JOptionPane.showMessageDialog(null, "Could not connect to Traci simulation"); exception.printStackTrace(); } } else { JOptionPane.showMessageDialog(null, "Please select a traffic list from the list!"); } }//GEN-LAST:event_jButtonEditActionPerformed private void jButtonCloseEditorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCloseEditorActionPerformed // TODO add your handling code here: jPanelEdit.setVisible(false); jButtonEdit.setEnabled(true); jListLights.setEnabled(true); }//GEN-LAST:event_jButtonCloseEditorActionPerformed private void jButtonRedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRedActionPerformed String st = jLabelState.getText(); int l = st.length(); String result = ""; for (int i = 0; i < l; i++) { result += "r"; } TraciConnector.CHGSTATE = result; TraciConnector.CHGID = String.valueOf(jListLights.getSelectedValue()); TraciConnector.TLID = String.valueOf(jListLights.getSelectedValue()); }//GEN-LAST:event_jButtonRedActionPerformed private void jButtonGreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGreenActionPerformed String st = jLabelState.getText(); int l = st.length(); String result = ""; for (int i = 0; i < l; i++) { result += "G"; } TraciConnector.CHGSTATE = result; TraciConnector.CHGID = String.valueOf(jListLights.getSelectedValue()); TraciConnector.TLID = String.valueOf(jListLights.getSelectedValue()); }//GEN-LAST:event_jButtonGreenActionPerformed /** * @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(EditTrafficLights.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(EditTrafficLights.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(EditTrafficLights.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(EditTrafficLights.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 EditTrafficLights().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonCloseEditor; private javax.swing.JButton jButtonEdit; private javax.swing.JButton jButtonGreen; private javax.swing.JButton jButtonRed; private javax.swing.JButton jButtonShow; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabelInfo; private javax.swing.JLabel jLabelSelectedID; private javax.swing.JLabel jLabelState; private javax.swing.JLabel jLabelStatus; private javax.swing.JLabel jLabelTitle; private javax.swing.JList jListLights; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanelEdit; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; // End of variables declaration//GEN-END:variables }