| 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 | 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 | * Set whether or not the incident is fully visible or in a compacted state. |
|---|
| 140 | * |
|---|
| 141 | * @param collapsed True if the event is compacted |
|---|
| 142 | */ |
|---|
| 143 | public void setCollapsed(boolean collapsed) |
|---|
| 144 | { |
|---|
| 145 | this.collapsed = collapsed; |
|---|
| 146 | script.update(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Set the delay time between the start of the script and the start of this |
|---|
| 151 | * incident. |
|---|
| 152 | * |
|---|
| 153 | * @param offset Number of seconds after 00:00:00 that this incident begins |
|---|
| 154 | */ |
|---|
| 155 | public void setOffset(int offset) |
|---|
| 156 | { |
|---|
| 157 | int old = this.offset; |
|---|
| 158 | this.offset = offset; |
|---|
| 159 | |
|---|
| 160 | for (TimeSlice ts : slices.values()) |
|---|
| 161 | { |
|---|
| 162 | ts.shift(offset - old); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | script.update(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | /** |
|---|
| 169 | * Add a new script event to this incident. |
|---|
| 170 | * |
|---|
| 171 | * @param ev The new event |
|---|
| 172 | * @param start Start time of this event, in seconds, from the beginning of |
|---|
| 173 | * the simulation |
|---|
| 174 | */ |
|---|
| 175 | public void addNewEvent(I_ScriptEvent ev, int start) |
|---|
| 176 | { |
|---|
| 177 | TimeSlice t = slices.get(start); |
|---|
| 178 | if (t == null) |
|---|
| 179 | { |
|---|
| 180 | //System.out.println("Generating new slice at time " + start); |
|---|
| 181 | t = new TimeSlice(start, this); |
|---|
| 182 | t.addEvent(ev); |
|---|
| 183 | slices.put(start, t); |
|---|
| 184 | } |
|---|
| 185 | else |
|---|
| 186 | { |
|---|
| 187 | t.addEvent(ev); |
|---|
| 188 | } |
|---|
| 189 | eventCount++; |
|---|
| 190 | |
|---|
| 191 | if (start > latestStart) |
|---|
| 192 | { |
|---|
| 193 | latestStart = start; |
|---|
| 194 | //System.out.println("Latest Start: " + latestStart); |
|---|
| 195 | } |
|---|
| 196 | if (start < offset) |
|---|
| 197 | { |
|---|
| 198 | offset = start; |
|---|
| 199 | //System.out.println("Offset: " + offset); |
|---|
| 200 | } |
|---|
| 201 | updateLength(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | /** |
|---|
| 205 | * Get an array of all valid timeSlices. |
|---|
| 206 | * |
|---|
| 207 | * @return List of timeSlices which are not null |
|---|
| 208 | */ |
|---|
| 209 | public ArrayList<TimeSlice> getSlices() |
|---|
| 210 | { |
|---|
| 211 | ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>(); |
|---|
| 212 | for (int i = 0; i <= latestStart; i++) |
|---|
| 213 | { |
|---|
| 214 | TimeSlice ts = slices.get(i); |
|---|
| 215 | if (ts != null) |
|---|
| 216 | { |
|---|
| 217 | arr.add(ts); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | return arr; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /** |
|---|
| 224 | * Write this incident, in proper XML form, to the file in question. |
|---|
| 225 | * |
|---|
| 226 | * @param f the destination savefile |
|---|
| 227 | */ |
|---|
| 228 | public void saveIncidentToFile(File f) |
|---|
| 229 | { |
|---|
| 230 | try |
|---|
| 231 | { |
|---|
| 232 | f.createNewFile(); |
|---|
| 233 | |
|---|
| 234 | BufferedWriter bw = new BufferedWriter(new FileWriter(f)); |
|---|
| 235 | bw.write(this.toXML()); |
|---|
| 236 | bw.flush(); |
|---|
| 237 | bw.close(); |
|---|
| 238 | |
|---|
| 239 | } |
|---|
| 240 | catch (Exception ex) |
|---|
| 241 | { |
|---|
| 242 | System.out.println("ERROR SAVING SCRIPT"); |
|---|
| 243 | ex.printStackTrace(); |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | @Override |
|---|
| 248 | public String toXML() |
|---|
| 249 | { |
|---|
| 250 | String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 251 | output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n"; |
|---|
| 252 | output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\""); |
|---|
| 253 | |
|---|
| 254 | for (TimeSlice slice : slices.values()) |
|---|
| 255 | { |
|---|
| 256 | output += slice.toXML(); |
|---|
| 257 | } |
|---|
| 258 | output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag); |
|---|
| 259 | return output; |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void insertCadData(long currentEventTime, CadData cad) |
|---|
| 263 | { |
|---|
| 264 | int time = (int) currentEventTime; |
|---|
| 265 | |
|---|
| 266 | TimeSlice slice; |
|---|
| 267 | |
|---|
| 268 | if (slices.get(time) == null) |
|---|
| 269 | { |
|---|
| 270 | slices.put(time, new TimeSlice(time, this)); |
|---|
| 271 | } |
|---|
| 272 | slice = slices.get(time); |
|---|
| 273 | slice.cadData = cad; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | /** |
|---|
| 277 | * Update the offset and apparent length of this incident. The offset is the |
|---|
| 278 | * start time of the earliest event in the incident. The length is the time |
|---|
| 279 | * that the latest, longest-lasting event ends, minus the offset. |
|---|
| 280 | */ |
|---|
| 281 | public void updateLength() |
|---|
| 282 | { |
|---|
| 283 | int lengthSoFar = 0; |
|---|
| 284 | for (int i = 0; i <= latestStart; i++) |
|---|
| 285 | { |
|---|
| 286 | TimeSlice ts = slices.get(i); |
|---|
| 287 | if (ts != null) |
|---|
| 288 | { |
|---|
| 289 | int reach = ts.getTime() + ts.getEffectiveDuration() - offset; |
|---|
| 290 | if (reach > lengthSoFar) |
|---|
| 291 | { |
|---|
| 292 | lengthSoFar = reach; |
|---|
| 293 | } |
|---|
| 294 | } |
|---|
| 295 | } |
|---|
| 296 | length = lengthSoFar; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | /** |
|---|
| 300 | * An event which is fired if the focused slice changes. |
|---|
| 301 | */ |
|---|
| 302 | public static class SliceChangedEvent |
|---|
| 303 | { |
|---|
| 304 | |
|---|
| 305 | /** |
|---|
| 306 | * The slice which has received focus in this event. |
|---|
| 307 | */ |
|---|
| 308 | public TimeSlice slice; |
|---|
| 309 | |
|---|
| 310 | SliceChangedEvent(TimeSlice slice) |
|---|
| 311 | { |
|---|
| 312 | this.slice = slice; |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | |
|---|
| 316 | /** |
|---|
| 317 | * Update and cause the system to focus on the given timeslice. |
|---|
| 318 | * |
|---|
| 319 | * @param i Index of the slice to focus on |
|---|
| 320 | */ |
|---|
| 321 | public void setSliceActive(int i) |
|---|
| 322 | { |
|---|
| 323 | if (this.slices.get(i) != null) |
|---|
| 324 | { |
|---|
| 325 | script.broadcastEvent(new SliceChangedEvent(this.slices.get(i))); |
|---|
| 326 | } |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | /** |
|---|
| 330 | * An event which is fired if the focused incident changes. |
|---|
| 331 | */ |
|---|
| 332 | public static class IncidentFocusedEvent |
|---|
| 333 | { |
|---|
| 334 | |
|---|
| 335 | /** |
|---|
| 336 | * The incident which has received focus in this event. |
|---|
| 337 | */ |
|---|
| 338 | public ScriptIncident incident; |
|---|
| 339 | |
|---|
| 340 | IncidentFocusedEvent(ScriptIncident i) |
|---|
| 341 | { |
|---|
| 342 | incident = i; |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | /** |
|---|
| 347 | * Update and cause the system to focus on this incident. |
|---|
| 348 | */ |
|---|
| 349 | public void setIncidentActive() |
|---|
| 350 | { |
|---|
| 351 | script.broadcastEvent(new IncidentFocusedEvent(this)); |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | /** |
|---|
| 355 | * String representation of this incident. |
|---|
| 356 | * |
|---|
| 357 | * @return String of the form "[Incident number] - [Incident name]" |
|---|
| 358 | */ |
|---|
| 359 | @Override |
|---|
| 360 | public String toString() |
|---|
| 361 | { |
|---|
| 362 | return this.number + " - " + this.name; |
|---|
| 363 | } |
|---|
| 364 | } |
|---|