| 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.TreeMap; |
|---|
| 9 | import scriptbuilder.structures.events.I_ScriptEvent; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * A script incident. It has an ID number, a name, and a description. It may |
|---|
| 13 | * contain several script events. It also has a color in the GUI window, and is |
|---|
| 14 | * collapsible. Incidents may start as soon as the script begins to run, or they |
|---|
| 15 | * can be offset from the start of the script. |
|---|
| 16 | * |
|---|
| 17 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 18 | * @author Bryan McGuffin |
|---|
| 19 | * @version 2017/06/29 |
|---|
| 20 | */ |
|---|
| 21 | public class ScriptIncident implements I_XML_Writable |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * The moments in time which have associated events. |
|---|
| 26 | */ |
|---|
| 27 | public TreeMap<Integer, TimeSlice> slices; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * GUI display color of this slice. |
|---|
| 31 | */ |
|---|
| 32 | public Color color; |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * ID number for this incident. |
|---|
| 36 | */ |
|---|
| 37 | public int number; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Name of the incident. |
|---|
| 41 | */ |
|---|
| 42 | public String name; |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Description of the incident. |
|---|
| 46 | */ |
|---|
| 47 | public String description; |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Length, in seconds, of the incident. |
|---|
| 51 | */ |
|---|
| 52 | public int length = 0; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * If true, incident appears minimized. |
|---|
| 56 | */ |
|---|
| 57 | public boolean collapsed = false; |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Number of seconds between start of simulation and start of this incident. |
|---|
| 61 | */ |
|---|
| 62 | public int offset = 0; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Start position of the latest timeslice. |
|---|
| 66 | */ |
|---|
| 67 | private int latestStart = 0; |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Number of events in this incident. |
|---|
| 71 | */ |
|---|
| 72 | public int eventCount = 0; |
|---|
| 73 | |
|---|
| 74 | public SimulationScript script; |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | * Basic constructor. |
|---|
| 78 | * |
|---|
| 79 | * @param number The incident ID number |
|---|
| 80 | * @param name The name of the incident |
|---|
| 81 | * @param description The description of the incident |
|---|
| 82 | * @param script The script object holding this incident |
|---|
| 83 | */ |
|---|
| 84 | public ScriptIncident(int number, String name, String description, |
|---|
| 85 | SimulationScript script) |
|---|
| 86 | { |
|---|
| 87 | color = Color.BLACK; |
|---|
| 88 | this.number = number; |
|---|
| 89 | this.name = name; |
|---|
| 90 | this.description = description; |
|---|
| 91 | this.script = script; |
|---|
| 92 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Constructor with color parameter. |
|---|
| 97 | * |
|---|
| 98 | * @param color The color to use in the GUI for this event |
|---|
| 99 | * @param number The incident ID number |
|---|
| 100 | * @param name The name of the incident |
|---|
| 101 | * @param description The description of the incident |
|---|
| 102 | * @param script The script object holding this incident |
|---|
| 103 | */ |
|---|
| 104 | public ScriptIncident(Color color, int number, String name, |
|---|
| 105 | String description, SimulationScript script) |
|---|
| 106 | { |
|---|
| 107 | this.color = color; |
|---|
| 108 | this.number = number; |
|---|
| 109 | this.name = name; |
|---|
| 110 | this.description = description; |
|---|
| 111 | this.script = script; |
|---|
| 112 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Constructor with color and offset parameters. |
|---|
| 117 | * |
|---|
| 118 | * @param color The color to use in the GUI for this event |
|---|
| 119 | * @param number The incident ID number |
|---|
| 120 | * @param name The name of the incident |
|---|
| 121 | * @param description The description of the incident |
|---|
| 122 | * @param script The script object holding this incident |
|---|
| 123 | * @param offset Number of seconds after 00:00:00 that this incident begins |
|---|
| 124 | */ |
|---|
| 125 | public ScriptIncident(Color color, int number, String name, |
|---|
| 126 | String description, SimulationScript script, |
|---|
| 127 | int offset) |
|---|
| 128 | { |
|---|
| 129 | this.color = color; |
|---|
| 130 | this.number = number; |
|---|
| 131 | this.name = name; |
|---|
| 132 | this.description = description; |
|---|
| 133 | this.script = script; |
|---|
| 134 | slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 135 | this.setOffset(offset); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | // /** |
|---|
| 139 | // * Constructor with type and location parameters. |
|---|
| 140 | // * @param color |
|---|
| 141 | // * @param number |
|---|
| 142 | // * @param name |
|---|
| 143 | // * @param description |
|---|
| 144 | // * @param script |
|---|
| 145 | // * @param offset |
|---|
| 146 | // * @param type |
|---|
| 147 | // * @param location |
|---|
| 148 | // */ |
|---|
| 149 | // public ScriptIncident(Color color, int number, String name, |
|---|
| 150 | // String description, SimulationScript script, |
|---|
| 151 | // int offset, String type, String location) |
|---|
| 152 | // { |
|---|
| 153 | // this.color = color; |
|---|
| 154 | // this.number = number; |
|---|
| 155 | // this.name = name; |
|---|
| 156 | // this.description = description; |
|---|
| 157 | // this.script = script; |
|---|
| 158 | // slices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 159 | // this.setOffset(offset); |
|---|
| 160 | // this.location = location; |
|---|
| 161 | // this.type = type; |
|---|
| 162 | // insertCadData(offset, new CadData(type,location)); |
|---|
| 163 | // } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | * Set whether or not the incident is fully visible or in a compacted state. |
|---|
| 167 | * |
|---|
| 168 | * @param collapsed True if the event is compacted |
|---|
| 169 | */ |
|---|
| 170 | public void setCollapsed(boolean collapsed) |
|---|
| 171 | { |
|---|
| 172 | this.collapsed = collapsed; |
|---|
| 173 | script.update(); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * Set the delay time between the start of the script and the start of this |
|---|
| 178 | * incident. |
|---|
| 179 | * |
|---|
| 180 | * @param offset Number of seconds after 00:00:00 that this incident begins |
|---|
| 181 | */ |
|---|
| 182 | public void setOffset(int offset) |
|---|
| 183 | { |
|---|
| 184 | int old = this.offset; |
|---|
| 185 | this.offset = offset; |
|---|
| 186 | TreeMap<Integer, TimeSlice> newSlices = new TreeMap<Integer, TimeSlice>(); |
|---|
| 187 | |
|---|
| 188 | int latest = 0; |
|---|
| 189 | |
|---|
| 190 | for (Integer k : slices.keySet()) |
|---|
| 191 | { |
|---|
| 192 | newSlices.put(k + (offset - old), slices.get(k)); |
|---|
| 193 | latest = k + (offset - old); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | latestStart = latest; |
|---|
| 197 | |
|---|
| 198 | for (TimeSlice ts : newSlices.values()) |
|---|
| 199 | { |
|---|
| 200 | ts.shift(offset - old); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | slices = newSlices; |
|---|
| 204 | updateLength(); |
|---|
| 205 | script.update(); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | /** |
|---|
| 209 | * Add a new script event to this incident. |
|---|
| 210 | * |
|---|
| 211 | * @param ev The new event |
|---|
| 212 | * @param start Start time of this event, in seconds, from the beginning of |
|---|
| 213 | * the simulation |
|---|
| 214 | */ |
|---|
| 215 | public void addNewEvent(I_ScriptEvent ev, int start) |
|---|
| 216 | { |
|---|
| 217 | /* |
|---|
| 218 | int leftoverSeconds = start % 60; |
|---|
| 219 | |
|---|
| 220 | //round start time to the nearest minute |
|---|
| 221 | if(leftoverSeconds != 0) |
|---|
| 222 | { |
|---|
| 223 | if(leftoverSeconds > 30) |
|---|
| 224 | { |
|---|
| 225 | start += (60 - leftoverSeconds); |
|---|
| 226 | } |
|---|
| 227 | else |
|---|
| 228 | { |
|---|
| 229 | start -= leftoverSeconds; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | */ |
|---|
| 233 | |
|---|
| 234 | //Check to see if there's already a timeslice here |
|---|
| 235 | TimeSlice t = slices.get(start); |
|---|
| 236 | //If not, make one; then, add the event to it |
|---|
| 237 | if (t == null) |
|---|
| 238 | { |
|---|
| 239 | t = new TimeSlice(start, this); |
|---|
| 240 | t.addEvent(ev); |
|---|
| 241 | slices.put(start, t); |
|---|
| 242 | } |
|---|
| 243 | else |
|---|
| 244 | { |
|---|
| 245 | t.addEvent(ev); |
|---|
| 246 | } |
|---|
| 247 | eventCount++; |
|---|
| 248 | |
|---|
| 249 | //If this is the latest start time in the incident, update that number |
|---|
| 250 | if (start > latestStart) |
|---|
| 251 | { |
|---|
| 252 | latestStart = start; |
|---|
| 253 | } |
|---|
| 254 | //If this event is earlier than all previous events, or if there are |
|---|
| 255 | //no other events, the offset is equal to this event's start time |
|---|
| 256 | if (start < offset || eventCount == 1) |
|---|
| 257 | { |
|---|
| 258 | offset = start; |
|---|
| 259 | //System.out.println("Offset: " + offset); |
|---|
| 260 | } |
|---|
| 261 | updateLength(); |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * Get an array of all valid timeSlices. |
|---|
| 266 | * |
|---|
| 267 | * @return List of timeSlices which are not null |
|---|
| 268 | */ |
|---|
| 269 | public ArrayList<TimeSlice> getSlices() |
|---|
| 270 | { |
|---|
| 271 | ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>(); |
|---|
| 272 | for (int i = 0; i <= latestStart; i++) |
|---|
| 273 | { |
|---|
| 274 | TimeSlice ts = slices.get(i); |
|---|
| 275 | if (ts != null) |
|---|
| 276 | { |
|---|
| 277 | arr.add(ts); |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | return arr; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | /** |
|---|
| 284 | * Write this incident, in proper XML form, to the file in question. |
|---|
| 285 | * |
|---|
| 286 | * @param f the destination savefile |
|---|
| 287 | */ |
|---|
| 288 | public void saveIncidentToFile(File f) |
|---|
| 289 | { |
|---|
| 290 | try |
|---|
| 291 | { |
|---|
| 292 | f.createNewFile(); |
|---|
| 293 | |
|---|
| 294 | BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
|---|
| 295 | bw.write(this.toXML()); |
|---|
| 296 | bw.flush(); |
|---|
| 297 | bw.close(); |
|---|
| 298 | |
|---|
| 299 | } |
|---|
| 300 | catch (Exception ex) |
|---|
| 301 | { |
|---|
| 302 | System.out.println("ERROR SAVING SCRIPT"); |
|---|
| 303 | ex.printStackTrace(); |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | @Override |
|---|
| 308 | public String toXML() |
|---|
| 309 | { |
|---|
| 310 | String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 311 | output += XMLWriter.internalDTD(); |
|---|
| 312 | output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\""); |
|---|
| 313 | |
|---|
| 314 | for (TimeSlice slice : slices.values()) |
|---|
| 315 | { |
|---|
| 316 | output += slice.toXML(); |
|---|
| 317 | } |
|---|
| 318 | output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag); |
|---|
| 319 | return output; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | public void insertCadData(long currentEventTime, CadData cad) |
|---|
| 323 | { |
|---|
| 324 | int time = (int) currentEventTime; |
|---|
| 325 | |
|---|
| 326 | TimeSlice slice; |
|---|
| 327 | |
|---|
| 328 | if (slices.get(time) == null) |
|---|
| 329 | { |
|---|
| 330 | slices.put(time, new TimeSlice(time, this)); |
|---|
| 331 | } |
|---|
| 332 | slice = slices.get(time); |
|---|
| 333 | slice.cadData = cad; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | public CadData getCadData(long currentEventTime) |
|---|
| 337 | { |
|---|
| 338 | int time = (int) currentEventTime; |
|---|
| 339 | TimeSlice slice; |
|---|
| 340 | |
|---|
| 341 | slice = slices.get(time); |
|---|
| 342 | return slice.cadData; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | /** |
|---|
| 346 | * Update the offset and apparent length of this incident. The offset is the |
|---|
| 347 | * start time of the earliest event in the incident. The length is the time |
|---|
| 348 | * that the latest, longest-lasting event ends, minus the offset. |
|---|
| 349 | */ |
|---|
| 350 | public void updateLength() |
|---|
| 351 | { |
|---|
| 352 | int lengthSoFar = 0; |
|---|
| 353 | for (int i = 0; i <= latestStart; i++) |
|---|
| 354 | { |
|---|
| 355 | TimeSlice ts = slices.get(i); |
|---|
| 356 | if (ts != null) |
|---|
| 357 | { |
|---|
| 358 | int reach = ts.getTime() + ts.getEffectiveDuration() - offset; |
|---|
| 359 | if (reach > lengthSoFar) |
|---|
| 360 | { |
|---|
| 361 | lengthSoFar = reach; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | } |
|---|
| 365 | length = lengthSoFar; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | /** |
|---|
| 369 | * An event which is fired if the focused slice changes. |
|---|
| 370 | */ |
|---|
| 371 | public static class SliceChangedEvent |
|---|
| 372 | { |
|---|
| 373 | |
|---|
| 374 | /** |
|---|
| 375 | * The slice which has received focus in this event. |
|---|
| 376 | */ |
|---|
| 377 | public TimeSlice slice; |
|---|
| 378 | |
|---|
| 379 | SliceChangedEvent(TimeSlice slice) |
|---|
| 380 | { |
|---|
| 381 | this.slice = slice; |
|---|
| 382 | } |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | /** |
|---|
| 386 | * Update and cause the system to focus on the given timeslice. |
|---|
| 387 | * |
|---|
| 388 | * @param i Index of the slice to focus on |
|---|
| 389 | */ |
|---|
| 390 | public void setSliceActive(int i) |
|---|
| 391 | { |
|---|
| 392 | if (this.slices.get(i) != null) |
|---|
| 393 | { |
|---|
| 394 | script.broadcastEvent(new SliceChangedEvent(this.slices.get(i))); |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | |
|---|
| 398 | /** |
|---|
| 399 | * An event which is fired if the focused incident changes. |
|---|
| 400 | */ |
|---|
| 401 | public static class IncidentFocusedEvent |
|---|
| 402 | { |
|---|
| 403 | |
|---|
| 404 | /** |
|---|
| 405 | * The incident which has received focus in this event. |
|---|
| 406 | */ |
|---|
| 407 | public ScriptIncident incident; |
|---|
| 408 | |
|---|
| 409 | IncidentFocusedEvent(ScriptIncident i) |
|---|
| 410 | { |
|---|
| 411 | incident = i; |
|---|
| 412 | } |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | /** |
|---|
| 416 | * Update and cause the system to focus on this incident. |
|---|
| 417 | */ |
|---|
| 418 | public void setIncidentActive() |
|---|
| 419 | { |
|---|
| 420 | script.broadcastEvent(new IncidentFocusedEvent(this)); |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | /** |
|---|
| 424 | * String representation of this incident. |
|---|
| 425 | * |
|---|
| 426 | * @return String of the form "[Incident number] - [Incident name]" |
|---|
| 427 | */ |
|---|
| 428 | @Override |
|---|
| 429 | public String toString() |
|---|
| 430 | { |
|---|
| 431 | return this.number + " - " + this.name; |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | /** |
|---|
| 435 | * Remove the timeslice at the given position. |
|---|
| 436 | * |
|---|
| 437 | * @param seconds absolute start time of the timeslice to remove. |
|---|
| 438 | */ |
|---|
| 439 | public void removeTimeSlice(int seconds) |
|---|
| 440 | { |
|---|
| 441 | //remove the timeslice |
|---|
| 442 | this.slices.remove(seconds); |
|---|
| 443 | |
|---|
| 444 | //offset is equivalent to start time of earliest slice |
|---|
| 445 | if (seconds == offset) |
|---|
| 446 | { |
|---|
| 447 | if (slices.size() > 0) |
|---|
| 448 | { |
|---|
| 449 | offset = slices.firstKey(); |
|---|
| 450 | } |
|---|
| 451 | else |
|---|
| 452 | { |
|---|
| 453 | offset = 0; |
|---|
| 454 | } |
|---|
| 455 | } |
|---|
| 456 | this.updateLength(); |
|---|
| 457 | script.update(); |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | /** |
|---|
| 461 | * Shifts the start time of a given event in this incident. The first event |
|---|
| 462 | * in the incident cannot be shifted. |
|---|
| 463 | * |
|---|
| 464 | * @param evt the event to be relocated |
|---|
| 465 | * @param oldTime the current start time of the event |
|---|
| 466 | * @param newTime the destination start time of the event |
|---|
| 467 | * @return true if the event is shifted over properly |
|---|
| 468 | * @pre the event in question exists |
|---|
| 469 | * @pre oldTime != newTime |
|---|
| 470 | */ |
|---|
| 471 | public boolean changeEventStart(I_ScriptEvent evt, int oldTime, int newTime) |
|---|
| 472 | { |
|---|
| 473 | if (!(oldTime == 0 && newTime > oldTime)) |
|---|
| 474 | { |
|---|
| 475 | this.slices.get(oldTime).events.remove(evt); |
|---|
| 476 | this.addNewEvent(evt, newTime); |
|---|
| 477 | return true; |
|---|
| 478 | } |
|---|
| 479 | return false; |
|---|
| 480 | } |
|---|
| 481 | } |
|---|