[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; |
---|
| 7 | |
---|
| 8 | import javax.swing.*; |
---|
| 9 | |
---|
| 10 | import vnsim.applications.trafficview.SimulatedCarInfo; |
---|
| 11 | import vnsim.map.object.*; |
---|
| 12 | import vnsim.network.dsrc.*; |
---|
| 13 | |
---|
| 14 | import java.awt.*; |
---|
| 15 | import java.awt.event.*; |
---|
| 16 | |
---|
| 17 | public class Control extends JPanel { |
---|
| 18 | |
---|
| 19 | private JButton run, nextStep, pause, restart, carSelect, driverView, trace, route; |
---|
| 20 | |
---|
| 21 | // Petroaca - the close program button |
---|
| 22 | private JButton close; |
---|
| 23 | |
---|
| 24 | private JTextField carId; |
---|
| 25 | |
---|
| 26 | private JButton zoomIn, zoomOut; |
---|
| 27 | |
---|
| 28 | private JButton pauseGUI, disableCOMM, traceTheCar; |
---|
| 29 | |
---|
| 30 | private int state; |
---|
| 31 | |
---|
| 32 | // private Increment runner; |
---|
| 33 | |
---|
| 34 | private myActionListener2 theListener; |
---|
| 35 | |
---|
| 36 | public boolean visibleMap = true; |
---|
| 37 | |
---|
| 38 | public boolean driverViewStatus = false; |
---|
| 39 | |
---|
| 40 | static final long serialVersionUID = 1; |
---|
| 41 | |
---|
| 42 | class Increment extends Thread { |
---|
| 43 | public long interval; |
---|
| 44 | |
---|
| 45 | public Increment(long i) { |
---|
| 46 | this.interval = i; |
---|
| 47 | this.start(); |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | public void run() { |
---|
| 51 | long t1, t2; |
---|
| 52 | while (state == 1) { |
---|
| 53 | t1 = System.currentTimeMillis(); |
---|
| 54 | Globals.engine.step(); |
---|
| 55 | t2 = System.currentTimeMillis(); |
---|
| 56 | t2 = t2 - t1; |
---|
| 57 | try { |
---|
| 58 | Thread.currentThread().sleep((long) (this.interval - t2)); |
---|
| 59 | } catch (Exception ee) { |
---|
| 60 | |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | public void setInterval(long l) { |
---|
| 67 | this.interval = l; |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | class myActionListener2 implements ActionListener { |
---|
| 72 | |
---|
| 73 | public void actionPerformed(ActionEvent e) { |
---|
| 74 | |
---|
| 75 | JButton tp; |
---|
| 76 | tp = (JButton) e.getSource(); |
---|
| 77 | |
---|
| 78 | // depending on the button being pressed, a method is invoked |
---|
| 79 | if (tp == run) { |
---|
| 80 | if (state != 1) { |
---|
| 81 | state = 1; |
---|
| 82 | |
---|
| 83 | Thread t = new Thread(new Runnable() { |
---|
| 84 | public void run() { |
---|
| 85 | Globals.engine.play(); |
---|
| 86 | } |
---|
| 87 | }); |
---|
| 88 | t.start(); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | if (tp == nextStep) { |
---|
| 92 | if (state == 0) { |
---|
| 93 | // Globals.di.nextData(); |
---|
| 94 | // Globals.engine.step(); |
---|
| 95 | Globals.engine.play(1); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | if (tp == pause) { |
---|
| 99 | if (state == 1) { |
---|
| 100 | state = 0; |
---|
| 101 | Globals.engine.pause(); |
---|
| 102 | /* |
---|
| 103 | * try{ runner.stop(); }catch(Exception ee){} |
---|
| 104 | */ |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | if (tp == restart) { |
---|
| 108 | // QUERY |
---|
| 109 | // try { |
---|
| 110 | // int id=Globals.demo.mv.currentCar.getVehicleId(); |
---|
| 111 | // PredefinedQueries.queries.put(Globals.engine.crtTime+10, new |
---|
| 112 | // Query(PredefinedQueries.req, id)); |
---|
| 113 | // } catch (Exception ex) { |
---|
| 114 | // System.out.println("THERE WAS AN EXCEPTION CREATING THE |
---|
| 115 | // QUERY:"+ex.toString()); |
---|
| 116 | // } |
---|
| 117 | } |
---|
| 118 | if (tp == zoomIn) { |
---|
| 119 | Globals.demo.zoomIn(); |
---|
| 120 | } |
---|
| 121 | if (tp == zoomOut) { |
---|
| 122 | Globals.demo.zoomOut(); |
---|
| 123 | } |
---|
| 124 | if (tp == pauseGUI) { |
---|
| 125 | if (visibleMap) { |
---|
| 126 | visibleMap = false; |
---|
| 127 | pauseGUI.setText("GUI ON"); |
---|
| 128 | Globals.demo.mv.anim.stop(); |
---|
| 129 | synchronized (Globals.mutex) { |
---|
| 130 | Globals.engine.withGUI = false; |
---|
| 131 | } |
---|
| 132 | } else { |
---|
| 133 | visibleMap = true; |
---|
| 134 | pauseGUI.setText("GUI OFF"); |
---|
| 135 | Globals.demo.mv.anim.start(); |
---|
| 136 | synchronized (Globals.mutex) { |
---|
| 137 | Globals.engine.withGUI = true; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | if (tp == disableCOMM) { |
---|
| 142 | if (Globals.engine.disableComm) { |
---|
| 143 | Globals.engine.restartCommunication(); |
---|
| 144 | disableCOMM.setText("COM OFF"); |
---|
| 145 | } else { |
---|
| 146 | Globals.engine.disableComm = true; |
---|
| 147 | disableCOMM.setText("COM ON"); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | if (tp == traceTheCar) { |
---|
| 151 | Globals.comView = !Globals.comView; |
---|
| 152 | } |
---|
| 153 | if (tp == carSelect) { |
---|
| 154 | // |
---|
| 155 | int v = 0; |
---|
| 156 | try { |
---|
| 157 | v = (new Integer(carId.getText())).intValue(); |
---|
| 158 | } catch (Exception ee) { |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | SimulatedCarInfo sm = Globals.engine.getCarIdx(v); |
---|
| 162 | Globals.demo.setCurrentCar(sm); |
---|
| 163 | if (sm != null) { |
---|
| 164 | Application app = ((CarRunningDSRC) sm).getAppLayer(); |
---|
| 165 | Globals.demo.st.addInfo("CurrentCar: " + sm + "\n FV:" + app.getFV() + " NFV:" |
---|
| 166 | + app.getNFV() + " LAV:" + app.getLAV() + " RAV:" + app.getRAV()); |
---|
| 167 | } else { |
---|
| 168 | Globals.demo.st.addInfo("CurrentCar is NULL "); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | if (tp == driverView) { |
---|
| 172 | if (driverViewStatus) { |
---|
| 173 | driverViewStatus = false; |
---|
| 174 | driverView.setText("Driver Mode"); |
---|
| 175 | Globals.demo.unsetDriverView(); |
---|
| 176 | } else { |
---|
| 177 | driverView.setText("Map Mode"); |
---|
| 178 | driverViewStatus = true; |
---|
| 179 | Globals.demo.setDriverView(); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | if (tp == trace) { |
---|
| 183 | SimulatedCarInfo car = Globals.demo.mv.currentCar; |
---|
| 184 | if (car != null && Globals.monitoredCars.indexOf(car) == -1) { |
---|
| 185 | Globals.monitoredCars.add(car); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | if (tp == route) { |
---|
| 189 | Globals.demo.mv.printRoute = !Globals.demo.mv.printRoute; |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // Petroaca - close action |
---|
| 194 | if (tp == close) { |
---|
| 195 | Component comp = getParent(); |
---|
| 196 | ((JFrame) comp.getParent().getParent().getParent()).dispose(); |
---|
| 197 | Globals.engine.reset(); |
---|
| 198 | |
---|
| 199 | DSRCStatistics.plotReceivedPackets(); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | public Control(int w, int h) { |
---|
| 206 | super(); |
---|
| 207 | state = 0; |
---|
| 208 | this.setBackground(Color.white); // sets the aspect of the container |
---|
| 209 | // and the size |
---|
| 210 | this.setLayout(new GridLayout(3, 5)); |
---|
| 211 | // this.setLayout(null); |
---|
| 212 | this.setSize(w, h); |
---|
| 213 | |
---|
| 214 | theListener = new myActionListener2(); // creates an actionListener |
---|
| 215 | |
---|
| 216 | run = new JButton("Run"); // creates the buttons |
---|
| 217 | pause = new JButton("Pause"); |
---|
| 218 | nextStep = new JButton("Step"); |
---|
| 219 | restart = new JButton("Query"); |
---|
| 220 | zoomIn = new JButton("zoomIn"); |
---|
| 221 | zoomOut = new JButton("zoomOut"); |
---|
| 222 | carSelect = new JButton("currentCar ID"); |
---|
| 223 | traceTheCar = new JButton("Switch View"); |
---|
| 224 | pauseGUI = new JButton("GUI OFF"); |
---|
| 225 | driverView = new JButton("Driver Mode"); |
---|
| 226 | carId = new JTextField(); |
---|
| 227 | disableCOMM = new JButton("COM OFF"); |
---|
| 228 | trace = new JButton("Trace Car"); |
---|
| 229 | route = new JButton("Show Route"); |
---|
| 230 | // Petroaca |
---|
| 231 | close = new JButton("Close"); |
---|
| 232 | |
---|
| 233 | run.addActionListener(theListener); // adds the listner to the buttons |
---|
| 234 | pause.addActionListener(theListener); |
---|
| 235 | nextStep.addActionListener(theListener); |
---|
| 236 | restart.addActionListener(theListener); |
---|
| 237 | zoomIn.addActionListener(theListener); |
---|
| 238 | zoomOut.addActionListener(theListener); |
---|
| 239 | carSelect.addActionListener(theListener); |
---|
| 240 | pauseGUI.addActionListener(theListener); |
---|
| 241 | disableCOMM.addActionListener(theListener); |
---|
| 242 | driverView.addActionListener(theListener); |
---|
| 243 | trace.addActionListener(theListener); |
---|
| 244 | route.addActionListener(theListener); |
---|
| 245 | traceTheCar.addActionListener(theListener); |
---|
| 246 | // Petroaca |
---|
| 247 | close.addActionListener(theListener); |
---|
| 248 | |
---|
| 249 | this.add(run); // adds the buttons to the container |
---|
| 250 | this.add(zoomIn); |
---|
| 251 | this.add(pauseGUI); |
---|
| 252 | this.add(carId); |
---|
| 253 | this.add(trace); |
---|
| 254 | |
---|
| 255 | this.add(pause); |
---|
| 256 | this.add(zoomOut); |
---|
| 257 | this.add(disableCOMM); |
---|
| 258 | this.add(carSelect); |
---|
| 259 | this.add(route); |
---|
| 260 | |
---|
| 261 | this.add(nextStep); |
---|
| 262 | this.add(restart); |
---|
| 263 | this.add(driverView); |
---|
| 264 | this.add(traceTheCar); |
---|
| 265 | // Petroaca |
---|
| 266 | this.add(close); |
---|
| 267 | // this.add(tmp2); |
---|
| 268 | |
---|
| 269 | this.setVisible(true); |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | } |
---|