| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Color; |
|---|
| 4 | import java.io.BufferedWriter; |
|---|
| 5 | import java.io.File; |
|---|
| 6 | import java.io.FileWriter; |
|---|
| 7 | import java.util.ArrayList; |
|---|
| 8 | import java.util.Collections; |
|---|
| 9 | import java.util.List; |
|---|
| 10 | import java.util.Observable; |
|---|
| 11 | import java.util.TreeMap; |
|---|
| 12 | import scriptbuilder.structures.events.I_ScriptEvent; |
|---|
| 13 | |
|---|
| 14 | /** |
|---|
| 15 | * A script incident. It has an ID number, a name, and a description. It may |
|---|
| 16 | * contain several script events. It also has a color in the GUI window, and is |
|---|
| 17 | * collapsible. Incidents may start as soon as the script begins to run, or they |
|---|
| 18 | * can be offset from the start of the script. |
|---|
| 19 | * |
|---|
| 20 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 21 | * @author Bryan McGuffin |
|---|
| 22 | * @version 2017/06/29 |
|---|
| 23 | */ |
|---|
| 24 | public class ScriptIncident implements I_XML_Writable |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | * The moments in time which have associated events. |
|---|
| 29 | */ |
|---|
| 30 | public TreeMap<Integer, TimeSlice> slices; |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * GUI display color of this slice. |
|---|
| 34 | */ |
|---|
| 35 | public Color color; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * ID number for this incident. |
|---|
| 39 | */ |
|---|
| 40 | public int number; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Name of the incident. |
|---|
| 44 | */ |
|---|
| 45 | public String name; |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Description of the incident. |
|---|
| 49 | */ |
|---|
| 50 | public String description; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Length, in seconds, of the incident. |
|---|
| 54 | */ |
|---|
| 55 | public int length = 0; |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * If true, incident appears minimized. |
|---|
| 59 | */ |
|---|
| 60 | public boolean collapsed = false; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Number of seconds between start of simulation and start of this incident. |
|---|
| 64 | */ |
|---|
| 65 | public int offset = 0; |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Start position of the latest timeslice. |
|---|
| 69 | */ |
|---|
| 70 | private int latestStart = 0; |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * Number of events in this incident. |
|---|
| 74 | */ |
|---|
| 75 | public int eventCount = 0; |
|---|
| 76 | |
|---|
| 77 | SimulationScript script; |
|---|
| 78 | |
|---|
| 79 | /** |
|---|
| 80 | * Basic constructor. |
|---|
| 81 | * |
|---|
| 82 | * @param number The incident ID number |
|---|
| 83 | * @param name The name of the incident |
|---|
| 84 | * @param description The description of the incident |
|---|
| 85 | * @param script The script object holding this incident |
|---|
| 86 | */ |
|---|
| 87 | public ScriptIncident(int number, String name, String description, |
|---|
| 88 | SimulationScript script) |
|---|
| 89 | { |
|---|
| 90 | color = Color.BLACK; |
|---|
| 91 | this.number = number; |
|---|
| 92 | this.name = name; |
|---|
| 93 | this.description = description; |
|---|
| 94 | this.script = script; |
|---|
| 95 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Constructor with color parameter. |
|---|
| 100 | * |
|---|
| 101 | * @param color The color to use in the GUI for this event |
|---|
| 102 | * @param number The incident ID number |
|---|
| 103 | * @param name The name of the incident |
|---|
| 104 | * @param description The description of the incident |
|---|
| 105 | * @param script The script object holding this incident |
|---|
| 106 | */ |
|---|
| 107 | public ScriptIncident(Color color, int number, String name, |
|---|
| 108 | String description, SimulationScript script) |
|---|
| 109 | { |
|---|
| 110 | this.color = color; |
|---|
| 111 | this.number = number; |
|---|
| 112 | this.name = name; |
|---|
| 113 | this.description = description; |
|---|
| 114 | this.script = script; |
|---|
| 115 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | * Constructor with color and offset parameters. |
|---|
| 120 | * |
|---|
| 121 | * @param color The color to use in the GUI for this event |
|---|
| 122 | * @param number The incident ID number |
|---|
| 123 | * @param name The name of the incident |
|---|
| 124 | * @param description The description of the incident |
|---|
| 125 | * @param script The script object holding this incident |
|---|
| 126 | * @param offset Number of seconds after 00:00:00 that this incident begins |
|---|
| 127 | */ |
|---|
| 128 | public ScriptIncident(Color color, int number, String name, |
|---|
| 129 | String description, SimulationScript script, |
|---|
| 130 | int offset) |
|---|
| 131 | { |
|---|
| 132 | this.color = color; |
|---|
| 133 | this.number = number; |
|---|
| 134 | this.name = name; |
|---|
| 135 | this.description = description; |
|---|
| 136 | this.script = script; |
|---|
| 137 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 138 | this.setOffset(offset); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Set whether or not the incident is fully visible or in a compacted state. |
|---|
| 143 | * |
|---|
| 144 | * @param collapsed True if the event is compacted |
|---|
| 145 | */ |
|---|
| 146 | public void setCollapsed(boolean collapsed) |
|---|
| 147 | { |
|---|
| 148 | this.collapsed = collapsed; |
|---|
| 149 | script.update(); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Set the delay time between the start of the script and the start of this |
|---|
| 154 | * incident. |
|---|
| 155 | * |
|---|
| 156 | * @param offset Number of seconds after 00:00:00 that this incident begins |
|---|
| 157 | */ |
|---|
| 158 | public void setOffset(int offset) |
|---|
| 159 | { |
|---|
| 160 | this.offset = offset; |
|---|
| 161 | script.update(); |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Add a new script event to this incident. |
|---|
| 166 | * |
|---|
| 167 | * @param ev The new event |
|---|
| 168 | * @param start Start time of this event, in seconds, from the beginning of |
|---|
| 169 | * the simulation |
|---|
| 170 | */ |
|---|
| 171 | public void addNewEvent(I_ScriptEvent ev, int start) |
|---|
| 172 | { |
|---|
| 173 | TimeSlice t = slices.get(start); |
|---|
| 174 | if (t == null) |
|---|
| 175 | { |
|---|
| 176 | //System.out.println("Generating new slice at time " + start); |
|---|
| 177 | t = new TimeSlice(start, this); |
|---|
| 178 | t.addEvent(ev); |
|---|
| 179 | slices.put(start, t); |
|---|
| 180 | } |
|---|
| 181 | else |
|---|
| 182 | { |
|---|
| 183 | t.addEvent(ev); |
|---|
| 184 | } |
|---|
| 185 | eventCount++; |
|---|
| 186 | |
|---|
| 187 | if (start > latestStart) |
|---|
| 188 | { |
|---|
| 189 | latestStart = start; |
|---|
| 190 | //System.out.println("Latest Start: " + latestStart); |
|---|
| 191 | } |
|---|
| 192 | if (start < offset) |
|---|
| 193 | { |
|---|
| 194 | offset = start; |
|---|
| 195 | //System.out.println("Offset: " + offset); |
|---|
| 196 | } |
|---|
| 197 | updateLength(); |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /** |
|---|
| 201 | * Get an array of all valid timeSlices. |
|---|
| 202 | * |
|---|
| 203 | * @return List of timeSlices which are not null |
|---|
| 204 | */ |
|---|
| 205 | public ArrayList<TimeSlice> getSlices() |
|---|
| 206 | { |
|---|
| 207 | ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>(); |
|---|
| 208 | for (int i = 0; i <= latestStart; i++) |
|---|
| 209 | { |
|---|
| 210 | TimeSlice ts = slices.get(i); |
|---|
| 211 | if (ts != null) |
|---|
| 212 | { |
|---|
| 213 | arr.add(ts); |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | return arr; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | public void saveIncidentToFile(File f) |
|---|
| 220 | { |
|---|
| 221 | try |
|---|
| 222 | { |
|---|
| 223 | f.createNewFile(); |
|---|
| 224 | |
|---|
| 225 | BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
|---|
| 226 | bw.write(this.toXML()); |
|---|
| 227 | bw.flush(); |
|---|
| 228 | bw.close(); |
|---|
| 229 | |
|---|
| 230 | } |
|---|
| 231 | catch (Exception ex) |
|---|
| 232 | { |
|---|
| 233 | System.out.println("ERROR SAVING SCRIPT"); |
|---|
| 234 | ex.printStackTrace(); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | @Override |
|---|
| 239 | public String toXML() |
|---|
| 240 | { |
|---|
| 241 | String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 242 | output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; |
|---|
| 243 | output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\""); |
|---|
| 244 | |
|---|
| 245 | for (TimeSlice slice : slices.values()) |
|---|
| 246 | { |
|---|
| 247 | output += slice.toXML(); |
|---|
| 248 | } |
|---|
| 249 | output += closeTag(ELEMENT.TMC_SCRIPT.tag); |
|---|
| 250 | return output; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | @Override |
|---|
| 254 | public String openTag(String s) |
|---|
| 255 | { |
|---|
| 256 | return "<" + s + ">"; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | @Override |
|---|
| 260 | public String closeTag(String s) |
|---|
| 261 | { |
|---|
| 262 | return "</" + s + ">\n"; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | @Override |
|---|
| 266 | public String emptyTag(String s) |
|---|
| 267 | { |
|---|
| 268 | return "<" + s + "/>\n"; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | void insertCadData(long currentEventTime, CadData cad) |
|---|
| 272 | { |
|---|
| 273 | if(number == 100) |
|---|
| 274 | { |
|---|
| 275 | System.out.println("\n\n~~~INCIDENT 100 GOT HERE~~~\n"); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | int time = (int) currentEventTime; |
|---|
| 280 | |
|---|
| 281 | TimeSlice slice; |
|---|
| 282 | |
|---|
| 283 | if (slices.get(time) == null) |
|---|
| 284 | { |
|---|
| 285 | slices.put(time, new TimeSlice(time, this)); |
|---|
| 286 | } |
|---|
| 287 | slice = slices.get(time); |
|---|
| 288 | slice.cadData = cad; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | /** |
|---|
| 292 | * Update the offset and apparent length of this incident. The offset is the |
|---|
| 293 | * start time of the earliest event in the incident. The length is the time |
|---|
| 294 | * that the latest, longest-lasting event ends, minus the offset. |
|---|
| 295 | */ |
|---|
| 296 | private void updateLength() |
|---|
| 297 | { |
|---|
| 298 | int lengthSoFar = 0; |
|---|
| 299 | for (int i = 0; i <= latestStart; i++) |
|---|
| 300 | { |
|---|
| 301 | TimeSlice ts = slices.get(i); |
|---|
| 302 | if (ts != null) |
|---|
| 303 | { |
|---|
| 304 | int reach = ts.getTime() + ts.getEffectiveDuration() - offset; |
|---|
| 305 | if (reach > lengthSoFar) |
|---|
| 306 | { |
|---|
| 307 | lengthSoFar = reach; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | length = lengthSoFar; |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | /** |
|---|
| 315 | * An event which is fired if the focused slice changes. |
|---|
| 316 | */ |
|---|
| 317 | public static class SliceChangedEvent |
|---|
| 318 | { |
|---|
| 319 | |
|---|
| 320 | public TimeSlice slice; |
|---|
| 321 | |
|---|
| 322 | SliceChangedEvent(TimeSlice slice) |
|---|
| 323 | { |
|---|
| 324 | this.slice = slice; |
|---|
| 325 | } |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | /** |
|---|
| 329 | * Update and cause the system to focus on the given timeslice. |
|---|
| 330 | * |
|---|
| 331 | * @param i Index of the slice to focus on |
|---|
| 332 | */ |
|---|
| 333 | public void setSliceActive(int i) |
|---|
| 334 | { |
|---|
| 335 | if (this.slices.get(i) != null) |
|---|
| 336 | { |
|---|
| 337 | script.broadcastEvent(new SliceChangedEvent(this.slices.get(i))); |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | /** |
|---|
| 342 | * An event which is fired if the focused incident changes. |
|---|
| 343 | */ |
|---|
| 344 | public static class IncidentFocusedEvent |
|---|
| 345 | { |
|---|
| 346 | |
|---|
| 347 | public ScriptIncident incident; |
|---|
| 348 | |
|---|
| 349 | IncidentFocusedEvent(ScriptIncident i) |
|---|
| 350 | { |
|---|
| 351 | incident = i; |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | * Update and cause the system to focus on this incident. |
|---|
| 357 | */ |
|---|
| 358 | public void setIncidentActive() |
|---|
| 359 | { |
|---|
| 360 | script.broadcastEvent(new IncidentFocusedEvent(this)); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | /** |
|---|
| 364 | * String representation of this incident. |
|---|
| 365 | * |
|---|
| 366 | * @return String of the form "[Incident number] - [Incident name]" |
|---|
| 367 | */ |
|---|
| 368 | @Override |
|---|
| 369 | public String toString() |
|---|
| 370 | { |
|---|
| 371 | return this.number + " - " + this.name; |
|---|
| 372 | } |
|---|
| 373 | } |
|---|