Changeset 30 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/structures/SimulationScript.java
- Timestamp:
- 08/02/2017 12:48:06 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/scriptbuilder/structures/SimulationScript.java
r24 r30 2 2 3 3 import java.awt.Color; 4 import java.io.BufferedWriter; 4 5 import java.io.File; 6 import java.io.FileWriter; 7 import java.io.OutputStream; 8 import java.io.OutputStreamWriter; 5 9 import java.util.ArrayList; 6 10 import java.util.List; … … 25 29 * @version 2017/06/22 26 30 */ 27 public class SimulationScript extends Observable 31 public class SimulationScript extends Observable implements I_XML_Writable 28 32 { 29 33 … … 45 49 }; 46 50 51 public String title = ""; 52 47 53 /** 48 54 * The incidents displayed by the GUI. … … 93 99 } 94 100 /* 95 //Add some demo events101 Add some demo events 96 102 incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", 97 103 "Blueberry truck crashed on the freeway.", 8800, this)); … … 109 115 "Truck stalled on the freeway.", 2800, this)); 110 116 numberOfIncidents++; 111 //incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill",112 //"Tomato trucked spilt tomatos all over the freeway.", 3200, this));113 //incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection",114 //"Crash at the intersection of Tree Road and Red Road.", 4300, this));115 //incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat",116 //"Bomb threat and Road X.", 6000, this));117 incidents.add(new ScriptIncident(incidentColors[6], 179, "Tomato Truck Spill", 118 "Tomato trucked spilt tomatos all over the freeway.", 3200, this)); 119 incidents.add(new ScriptIncident(incidentColors[7], 180, "Crash at Intersection", 120 "Crash at the intersection of Tree Road and Red Road.", 4300, this)); 121 incidents.add(new ScriptIncident(incidentColors[8], 181, "Bomb Threat", 122 "Bomb threat and Road X.", 6000, this)); 117 123 incidents.add(null); 118 124 incidents.add(null); … … 146 152 incidents.get(4).setOffset(4400); 147 153 incidents.get(5).setOffset(1400); 148 //incidents.get(6).setOffset(7600);149 //incidents.get(7).setOffset(2400);150 //incidents.get(8).setOffset(4800);154 incidents.get(6).setOffset(7600); 155 incidents.get(7).setOffset(2400); 156 incidents.get(8).setOffset(4800); 151 157 } 152 158 */ … … 217 223 this.update(); 218 224 } 225 226 public void saveScriptToFile(File f) 227 { 228 try 229 { 230 f.createNewFile(); 231 232 BufferedWriter bw = new BufferedWriter(new FileWriter(f)); 233 bw.write(this.toXML()); 234 bw.flush(); 235 bw.close(); 236 237 } 238 catch (Exception ex) 239 { 240 System.out.println("ERROR SAVING SCRIPT"); 241 ex.printStackTrace(); 242 } 243 } 244 245 @Override 246 public String toXML() 247 { 248 String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; 249 output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">"; 250 ArrayList<TimeSlice> slices = allSlices(); 251 output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\""); 252 for (TimeSlice slice : slices) 253 { 254 output += slice.toXML(); 255 } 256 output += closeTag(ELEMENT.TMC_SCRIPT.tag); 257 return output; 258 } 259 260 @Override 261 public String openTag(String s) 262 { 263 return "<" + s + ">\n"; 264 } 265 266 @Override 267 public String closeTag(String s) 268 { 269 return "</" + s + ">\n"; 270 } 271 272 @Override 273 public String emptyTag(String s) 274 { 275 return "<" + s + "/>\n"; 276 } 277 278 /** 279 * Arranges all timeslices in this script in chronological order, then by 280 * incident number. 281 * 282 * @return a list of all timeslices in the simulation script 283 */ 284 public ArrayList<TimeSlice> allSlices() 285 { 286 ArrayList<TimeSlice> list = new ArrayList<TimeSlice>(); 287 int length = absoluteLength(); 288 289 for (int i = 0; i < length; i++) 290 { 291 for (ScriptIncident inc : incidents) 292 { 293 294 if (inc != null && inc.slices.get(i) != null) 295 { 296 list.add(inc.slices.get(i)); 297 } 298 } 299 } 300 return list; 301 } 302 303 /** 304 * Gets the total length of the simulation in seconds. 305 * 306 * @return 307 */ 308 public int absoluteLength() 309 { 310 int length = 0; 311 for (ScriptIncident inc : incidents) 312 { 313 if (inc != null) 314 { 315 int currentLength = inc.length + inc.offset; 316 if (currentLength > length) 317 { 318 length = currentLength; 319 } 320 } 321 } 322 return length; 323 } 324 219 325 }
Note: See TracChangeset
for help on using the changeset viewer.
