| 1 | /* |
|---|
| 2 | * To change this license header, choose License Headers in Project Properties. |
|---|
| 3 | * To change this template file, choose Tools | Templates |
|---|
| 4 | * and open the template in the editor. |
|---|
| 5 | */ |
|---|
| 6 | package atmsdriver.trafficeventseditor; |
|---|
| 7 | |
|---|
| 8 | import atmsdriver.model.Highway; |
|---|
| 9 | import atmsdriver.model.Highways; |
|---|
| 10 | import atmsdriver.model.LoopDetector; |
|---|
| 11 | import atmsdriver.model.LoopDetector.DOTCOLOR; |
|---|
| 12 | import atmsdriver.model.Station; |
|---|
| 13 | import java.util.ArrayList; |
|---|
| 14 | import java.util.List; |
|---|
| 15 | import java.util.Observable; |
|---|
| 16 | import java.util.logging.Level; |
|---|
| 17 | import java.util.logging.Logger; |
|---|
| 18 | import tmcsim.common.SimulationException; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * |
|---|
| 22 | * @author jtorres |
|---|
| 23 | */ |
|---|
| 24 | public class TimeFrames extends Observable |
|---|
| 25 | { |
|---|
| 26 | public List<TimeFrame> frames; |
|---|
| 27 | final public Highways highways; |
|---|
| 28 | public Highway currentHighway; |
|---|
| 29 | public TimeFrame currentTimeFrame; |
|---|
| 30 | public Station currentStation; |
|---|
| 31 | public LoopDetector currentLoopDetector; |
|---|
| 32 | public TrafficLaneEvent currentTrafficLaneEvent; |
|---|
| 33 | |
|---|
| 34 | public TimeFrames() |
|---|
| 35 | { |
|---|
| 36 | frames = new ArrayList<>(); |
|---|
| 37 | highways = new Highways("./config/vds_data/highways_fullmap.txt", |
|---|
| 38 | "192.168.251.46", 8080); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | public void addTimeFrame(String time) |
|---|
| 42 | { |
|---|
| 43 | TimeFrame frame = new TimeFrame(time); |
|---|
| 44 | frames.add(frame); |
|---|
| 45 | |
|---|
| 46 | setCurrentTimeFrame(this.frames.size() - 1); |
|---|
| 47 | |
|---|
| 48 | setChanged(); |
|---|
| 49 | notifyObservers("add frame"); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | public void addEventToTimeFrame(DOTCOLOR dotcolor) |
|---|
| 53 | { |
|---|
| 54 | TrafficLaneEvent event = new TrafficLaneEvent( |
|---|
| 55 | currentHighway.routeNumber, currentStation, |
|---|
| 56 | currentLoopDetector, dotcolor); |
|---|
| 57 | currentTimeFrame.addEvent(event); |
|---|
| 58 | |
|---|
| 59 | setChanged(); |
|---|
| 60 | notifyObservers("add event"); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | public void setCurrentHighway(int index) |
|---|
| 64 | { |
|---|
| 65 | currentHighway = highways.highways.get(index); |
|---|
| 66 | currentStation = null; |
|---|
| 67 | currentLoopDetector = null; |
|---|
| 68 | |
|---|
| 69 | setChanged(); |
|---|
| 70 | notifyObservers("select hwy"); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public void setCurrentTimeFrame(int index) |
|---|
| 74 | { |
|---|
| 75 | currentTimeFrame = frames.get(index); |
|---|
| 76 | |
|---|
| 77 | setChanged(); |
|---|
| 78 | notifyObservers("select frame"); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public void setCurrentStation(int index) |
|---|
| 82 | { |
|---|
| 83 | currentStation = currentHighway.stations.get(index); |
|---|
| 84 | currentLoopDetector = null; |
|---|
| 85 | |
|---|
| 86 | setChanged(); |
|---|
| 87 | notifyObservers("select station"); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | public void setCurrentLoopDetector(int index) |
|---|
| 91 | { |
|---|
| 92 | currentLoopDetector = currentStation.loops.get(index); |
|---|
| 93 | |
|---|
| 94 | setChanged(); |
|---|
| 95 | notifyObservers("select loop"); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public void setCurrentTrafficLaneEvent(int index) |
|---|
| 99 | { |
|---|
| 100 | currentTrafficLaneEvent = currentTimeFrame.events.get(index); |
|---|
| 101 | |
|---|
| 102 | setChanged(); |
|---|
| 103 | notifyObservers("select event"); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | int getCurrentTrafficEventIndex() |
|---|
| 107 | { |
|---|
| 108 | return currentTimeFrame.events.indexOf(currentTrafficLaneEvent); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | int getCurrentTimeFrameIndex() |
|---|
| 112 | { |
|---|
| 113 | return frames.indexOf(currentTimeFrame); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | int getCurrentHighwayIndex() |
|---|
| 117 | { |
|---|
| 118 | return highways.highways.indexOf(currentHighway); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | public void deleteTrafficLaneEvent(int index) |
|---|
| 122 | { |
|---|
| 123 | currentTimeFrame.events.remove(index); |
|---|
| 124 | |
|---|
| 125 | if(currentTimeFrame.events.size() > 0) |
|---|
| 126 | { |
|---|
| 127 | if(index == 0) |
|---|
| 128 | { |
|---|
| 129 | currentTrafficLaneEvent = currentTimeFrame.events.get(0); |
|---|
| 130 | } |
|---|
| 131 | else |
|---|
| 132 | { |
|---|
| 133 | currentTrafficLaneEvent = currentTimeFrame.events.get(index - 1); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | { |
|---|
| 138 | currentTrafficLaneEvent = null; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | setChanged(); |
|---|
| 142 | notifyObservers("delete event"); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | void deleteTimeFrame(int selectedIndex) |
|---|
| 146 | { |
|---|
| 147 | frames.remove(selectedIndex); |
|---|
| 148 | |
|---|
| 149 | if(frames.size() > 0) |
|---|
| 150 | { |
|---|
| 151 | if(selectedIndex == 0) |
|---|
| 152 | { |
|---|
| 153 | currentTimeFrame = frames.get(0); |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | currentTimeFrame = frames.get(selectedIndex - 1); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | else |
|---|
| 161 | { |
|---|
| 162 | currentTimeFrame = null; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | setChanged(); |
|---|
| 166 | notifyObservers("delete frame"); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | void singlePreviewStation() |
|---|
| 170 | { |
|---|
| 171 | highways.reset(); |
|---|
| 172 | for(TrafficLaneEvent event : currentTimeFrame.events) |
|---|
| 173 | { |
|---|
| 174 | if(event.station.equals(currentStation)) |
|---|
| 175 | { |
|---|
| 176 | highways.applyTrafficLaneEvent(event); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | writeToFEP(); |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | void singlePreviewHighways() |
|---|
| 183 | { |
|---|
| 184 | highways.reset(); |
|---|
| 185 | for(TrafficLaneEvent event : currentTimeFrame.events) |
|---|
| 186 | { |
|---|
| 187 | highways.applyTrafficLaneEvent(event); |
|---|
| 188 | } |
|---|
| 189 | writeToFEP(); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void writeToFEP() |
|---|
| 193 | { |
|---|
| 194 | try |
|---|
| 195 | { |
|---|
| 196 | highways.writeToFEP(); |
|---|
| 197 | } |
|---|
| 198 | catch (SimulationException ex) |
|---|
| 199 | { |
|---|
| 200 | System.out.println("writeToFEP() failed"); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | void cumulativePreviewStation() |
|---|
| 205 | { |
|---|
| 206 | highways.reset(); |
|---|
| 207 | for(TimeFrame tf : frames) |
|---|
| 208 | { |
|---|
| 209 | for(TrafficLaneEvent e : tf.events) |
|---|
| 210 | { |
|---|
| 211 | if(e.station.equals(currentStation)) |
|---|
| 212 | { |
|---|
| 213 | highways.applyTrafficLaneEvent(e); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if(tf.equals(currentTimeFrame)) |
|---|
| 218 | break; |
|---|
| 219 | } |
|---|
| 220 | writeToFEP(); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | void cumulativePreviewHighways() |
|---|
| 224 | { |
|---|
| 225 | highways.reset(); |
|---|
| 226 | for(TimeFrame tf : frames) |
|---|
| 227 | { |
|---|
| 228 | for(TrafficLaneEvent e : tf.events) |
|---|
| 229 | { |
|---|
| 230 | highways.applyTrafficLaneEvent(e); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | if(tf.equals(currentTimeFrame)) |
|---|
| 234 | break; |
|---|
| 235 | } |
|---|
| 236 | writeToFEP(); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|