Changeset 30 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/structures/SimulationScript.java


Ignore:
Timestamp:
08/02/2017 12:48:06 PM (9 years ago)
Author:
bmcguffin
Message:

Made TimeSlice?, ScriptIncident?, and SimulationScript? all implement I_XML_Writable. Simulationscript also now implements a saveScriptToFile() method.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scriptbuilder/structures/SimulationScript.java

    r24 r30  
    22 
    33import java.awt.Color; 
     4import java.io.BufferedWriter; 
    45import java.io.File; 
     6import java.io.FileWriter; 
     7import java.io.OutputStream; 
     8import java.io.OutputStreamWriter; 
    59import java.util.ArrayList; 
    610import java.util.List; 
     
    2529 * @version 2017/06/22 
    2630 */ 
    27 public class SimulationScript extends Observable 
     31public class SimulationScript extends Observable implements I_XML_Writable 
    2832{ 
    2933 
     
    4549    }; 
    4650 
     51    public String title = ""; 
     52 
    4753    /** 
    4854     * The incidents displayed by the GUI. 
     
    9399        } 
    94100        /* 
    95          // Add some demo events 
     101         Add some demo events 
    96102         incidents.add(new ScriptIncident(incidentColors[1], 174, "Blueberry Truck", 
    97103         "Blueberry truck crashed on the freeway.", 8800, this)); 
     
    109115         "Truck stalled on the freeway.", 2800, this)); 
    110116         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)); 
    117123         incidents.add(null); 
    118124         incidents.add(null); 
     
    146152         incidents.get(4).setOffset(4400); 
    147153         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);  
    151157         } 
    152158         */ 
     
    217223        this.update(); 
    218224    } 
     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 
    219325} 
Note: See TracChangeset for help on using the changeset viewer.