source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/SimulationScript.java @ 38

Revision 38, 6.8 KB checked in by bmcguffin, 9 years ago (diff)

Removed the newLine at the end of each openTag method.

Line 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileWriter;
7import java.io.OutputStream;
8import java.io.OutputStreamWriter;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.Observable;
12import java.util.Random;
13import java.util.Vector;
14import java.util.logging.Level;
15import java.util.logging.Logger;
16import javax.xml.parsers.ParserConfigurationException;
17import javax.xml.parsers.SAXParserFactory;
18import scriptbuilder.structures.ScriptEvent.ScriptEventType;
19import scriptbuilder.structures.ScriptIncident.IncidentFocusedEvent;
20import scriptbuilder.structures.ScriptIncident.SliceChangedEvent;
21import scriptbuilder.structures.units.Unit;
22
23/**
24 * Representation of the script to be run by the TMC Simulator. Holds a list of
25 * incidents, which have start and end times and contain events.
26 *
27 * @author Greg Eddington <geddingt@calpoly.edu>
28 *
29 * @author Bryan McGuffin <bmcguffi@calpoly.edu>
30 * @version 2017/06/22
31 */
32public class SimulationScript extends Observable implements I_XML_Writable
33{
34
35    /**
36     * All default options for GUI colorings of incidents.
37     */
38    public static final Color[] incidentColors =
39    {
40        Color.BLACK,
41        Color.BLUE,
42        Color.RED,
43        new Color(0x38, 0x5E, 0x0F),
44        new Color(128, 0, 128),
45        Color.MAGENTA,
46        new Color(0x23, 0x6B, 0x8E),
47        Color.ORANGE,
48        new Color(0x60, 0x33, 0x11),
49        Color.GRAY
50    };
51
52    public String title = "";
53
54    /**
55     * The incidents displayed by the GUI.
56     */
57    public List<ScriptIncident> incidents;
58
59    /**
60     * The units which participate in Unit events.
61     */
62    public List<Unit> units;
63
64    //Somewhere in the code, something assumes that the list of incidents
65    //contains exactly 10 items. Until I can find and un-break that, this will do.
66    private final int INCIDENT_FILL_COUNT = 10;
67
68    /**
69     * Number of incidents currently displayed.
70     */
71    private int numberOfIncidents;
72
73    /**
74     * Script handler for parsing incoming XML files.
75     */
76    private MyScriptHandler sh;
77
78    //TODO: Pretty much everything in this constructor is dummy data.
79    //Replace all of it.
80    /**
81     * Constructor. Backfill incident list with null objects, to be replaced
82     * later.
83     */
84    public SimulationScript()
85    {
86        sh = new MyScriptHandler(this);
87        incidents = new ArrayList<ScriptIncident>();
88        units = new ArrayList<Unit>();
89        numberOfIncidents = 0;
90
91        //Backfill with null incidents
92        for (int i = numberOfIncidents; i < INCIDENT_FILL_COUNT; i++)
93        {
94            incidents.add(null);
95        }
96    }
97
98    /**
99     * Update the script's observers.
100     *
101     */
102    public void update()
103    {
104        // The script has changed, notify observers
105        setChanged();
106        notifyObservers(this);
107    }
108
109    /**
110     * Tell this script's observers that there is a new slice event.
111     *
112     * @param e the slice focus event
113     */
114    public void broadcastEvent(SliceChangedEvent e)
115    {
116        // The slice focus has changed; pass the message
117        setChanged();
118        notifyObservers(e);
119    }
120
121    /**
122     * Tell this script's observers that there is a new slice event.
123     *
124     * @param e the incident focus event
125     */
126    public void broadcastEvent(IncidentFocusedEvent e)
127    {
128        // The slice focus has changed; pass the message
129        setChanged();
130        notifyObservers(e);
131    }
132
133    /**
134     * Load in an existing script from an XML file.
135     *
136     * @param f the file containing the script
137     */
138    public void loadScriptFromFile(File f)
139    {
140        try
141        {
142
143            SAXParserFactory.newInstance().newSAXParser().parse(f, sh);
144
145            Vector<ScriptIncident> inc = sh.getIncidents();
146            units = sh.getUnits();
147            for (ScriptIncident sci : inc)
148            {
149                if (numberOfIncidents < INCIDENT_FILL_COUNT)
150                {
151                    incidents.set(numberOfIncidents++, sci);
152                }
153            }
154        }
155        catch (Exception ex)
156        {
157            System.out.println("ERROR LOADING SCRIPT");
158            ex.printStackTrace();
159        }
160        this.update();
161    }
162
163    public void saveScriptToFile(File f)
164    {
165        try
166        {
167            f.createNewFile();
168
169            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
170            bw.write(this.toXML());
171            bw.flush();
172            bw.close();
173
174        }
175        catch (Exception ex)
176        {
177            System.out.println("ERROR SAVING SCRIPT");
178            ex.printStackTrace();
179        }
180    }
181
182    @Override
183    public String toXML()
184    {
185        ArrayList<TimeSlice> slices = allSlices();
186        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
187        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n";
188        output += openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.title + "\"");
189
190        if (units.size() > 0)
191        {
192            output += openTag(ELEMENT.SCRIPT_DATA.tag);
193            for (Unit unit : units)
194            {
195                output += unit.toXML();
196            }
197            output += closeTag(ELEMENT.SCRIPT_DATA.tag);
198        }
199        for (TimeSlice slice : slices)
200        {
201            output += slice.toXML();
202        }
203        output += closeTag(ELEMENT.TMC_SCRIPT.tag);
204        return output;
205    }
206
207    @Override
208    public String openTag(String s)
209    {
210        return "<" + s + ">";
211    }
212
213    @Override
214    public String closeTag(String s)
215    {
216        return "</" + s + ">\n";
217    }
218
219    @Override
220    public String emptyTag(String s)
221    {
222        return "<" + s + "/>\n";
223    }
224
225    /**
226     * Arranges all timeslices in this script in chronological order, then by
227     * incident number.
228     *
229     * @return a list of all timeslices in the simulation script
230     */
231    public ArrayList<TimeSlice> allSlices()
232    {
233        ArrayList<TimeSlice> list = new ArrayList<TimeSlice>();
234        int length = absoluteLength();
235
236        for (int i = 0; i < length; i++)
237        {
238            for (ScriptIncident inc : incidents)
239            {
240
241                if (inc != null && inc.slices.get(i) != null)
242                {
243                    list.add(inc.slices.get(i));
244                }
245            }
246        }
247        return list;
248    }
249
250    /**
251     * Gets the total length of the simulation in seconds.
252     *
253     * @return
254     */
255    public int absoluteLength()
256    {
257        int length = 0;
258        for (ScriptIncident inc : incidents)
259        {
260            if (inc != null)
261            {
262                int currentLength = inc.length + inc.offset;
263                if (currentLength > length)
264                {
265                    length = currentLength;
266                }
267            }
268        }
269        return length;
270    }
271
272}
Note: See TracBrowser for help on using the repository browser.