/************************************************************************************ * 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; import javax.swing.*; import vnsim.applications.trafficview.SimulatedCarInfo; import vnsim.map.object.*; import vnsim.network.dsrc.*; import java.awt.*; import java.awt.event.*; public class Control extends JPanel { private JButton run, nextStep, pause, restart, carSelect, driverView, trace, route; // Petroaca - the close program button private JButton close; private JTextField carId; private JButton zoomIn, zoomOut; private JButton pauseGUI, disableCOMM, traceTheCar; private int state; // private Increment runner; private myActionListener2 theListener; public boolean visibleMap = true; public boolean driverViewStatus = false; static final long serialVersionUID = 1; class Increment extends Thread { public long interval; public Increment(long i) { this.interval = i; this.start(); } public void run() { long t1, t2; while (state == 1) { t1 = System.currentTimeMillis(); Globals.engine.step(); t2 = System.currentTimeMillis(); t2 = t2 - t1; try { Thread.currentThread().sleep((long) (this.interval - t2)); } catch (Exception ee) { } } } public void setInterval(long l) { this.interval = l; } } class myActionListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { JButton tp; tp = (JButton) e.getSource(); // depending on the button being pressed, a method is invoked if (tp == run) { if (state != 1) { state = 1; Thread t = new Thread(new Runnable() { public void run() { Globals.engine.play(); } }); t.start(); } } if (tp == nextStep) { if (state == 0) { // Globals.di.nextData(); // Globals.engine.step(); Globals.engine.play(1); } } if (tp == pause) { if (state == 1) { state = 0; Globals.engine.pause(); /* * try{ runner.stop(); }catch(Exception ee){} */ } } if (tp == restart) { // QUERY // try { // int id=Globals.demo.mv.currentCar.getVehicleId(); // PredefinedQueries.queries.put(Globals.engine.crtTime+10, new // Query(PredefinedQueries.req, id)); // } catch (Exception ex) { // System.out.println("THERE WAS AN EXCEPTION CREATING THE // QUERY:"+ex.toString()); // } } if (tp == zoomIn) { Globals.demo.zoomIn(); } if (tp == zoomOut) { Globals.demo.zoomOut(); } if (tp == pauseGUI) { if (visibleMap) { visibleMap = false; pauseGUI.setText("GUI ON"); Globals.demo.mv.anim.stop(); synchronized (Globals.mutex) { Globals.engine.withGUI = false; } } else { visibleMap = true; pauseGUI.setText("GUI OFF"); Globals.demo.mv.anim.start(); synchronized (Globals.mutex) { Globals.engine.withGUI = true; } } } if (tp == disableCOMM) { if (Globals.engine.disableComm) { Globals.engine.restartCommunication(); disableCOMM.setText("COM OFF"); } else { Globals.engine.disableComm = true; disableCOMM.setText("COM ON"); } } if (tp == traceTheCar) { Globals.comView = !Globals.comView; } if (tp == carSelect) { // int v = 0; try { v = (new Integer(carId.getText())).intValue(); } catch (Exception ee) { return; } SimulatedCarInfo sm = Globals.engine.getCarIdx(v); Globals.demo.setCurrentCar(sm); if (sm != null) { Application app = ((CarRunningDSRC) sm).getAppLayer(); Globals.demo.st.addInfo("CurrentCar: " + sm + "\n FV:" + app.getFV() + " NFV:" + app.getNFV() + " LAV:" + app.getLAV() + " RAV:" + app.getRAV()); } else { Globals.demo.st.addInfo("CurrentCar is NULL "); } } if (tp == driverView) { if (driverViewStatus) { driverViewStatus = false; driverView.setText("Driver Mode"); Globals.demo.unsetDriverView(); } else { driverView.setText("Map Mode"); driverViewStatus = true; Globals.demo.setDriverView(); } } if (tp == trace) { SimulatedCarInfo car = Globals.demo.mv.currentCar; if (car != null && Globals.monitoredCars.indexOf(car) == -1) { Globals.monitoredCars.add(car); } } if (tp == route) { Globals.demo.mv.printRoute = !Globals.demo.mv.printRoute; } // Petroaca - close action if (tp == close) { Component comp = getParent(); ((JFrame) comp.getParent().getParent().getParent()).dispose(); Globals.engine.reset(); DSRCStatistics.plotReceivedPackets(); } } } public Control(int w, int h) { super(); state = 0; this.setBackground(Color.white); // sets the aspect of the container // and the size this.setLayout(new GridLayout(3, 5)); // this.setLayout(null); this.setSize(w, h); theListener = new myActionListener2(); // creates an actionListener run = new JButton("Run"); // creates the buttons pause = new JButton("Pause"); nextStep = new JButton("Step"); restart = new JButton("Query"); zoomIn = new JButton("zoomIn"); zoomOut = new JButton("zoomOut"); carSelect = new JButton("currentCar ID"); traceTheCar = new JButton("Switch View"); pauseGUI = new JButton("GUI OFF"); driverView = new JButton("Driver Mode"); carId = new JTextField(); disableCOMM = new JButton("COM OFF"); trace = new JButton("Trace Car"); route = new JButton("Show Route"); // Petroaca close = new JButton("Close"); run.addActionListener(theListener); // adds the listner to the buttons pause.addActionListener(theListener); nextStep.addActionListener(theListener); restart.addActionListener(theListener); zoomIn.addActionListener(theListener); zoomOut.addActionListener(theListener); carSelect.addActionListener(theListener); pauseGUI.addActionListener(theListener); disableCOMM.addActionListener(theListener); driverView.addActionListener(theListener); trace.addActionListener(theListener); route.addActionListener(theListener); traceTheCar.addActionListener(theListener); // Petroaca close.addActionListener(theListener); this.add(run); // adds the buttons to the container this.add(zoomIn); this.add(pauseGUI); this.add(carId); this.add(trace); this.add(pause); this.add(zoomOut); this.add(disableCOMM); this.add(carSelect); this.add(route); this.add(nextStep); this.add(restart); this.add(driverView); this.add(traceTheCar); // Petroaca this.add(close); // this.add(tmp2); this.setVisible(true); } }