source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/ScriptIncident.java @ 47

Revision 47, 8.9 KB checked in by bmcguffin, 9 years ago (diff)

Updated toXML behaviors for several classes to bring final adjustments in line with desired output.

Line 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileWriter;
7import java.util.ArrayList;
8import java.util.Collections;
9import java.util.List;
10import java.util.Observable;
11import java.util.TreeMap;
12import scriptbuilder.structures.events.I_ScriptEvent;
13
14/**
15 * A script incident. It has an ID number, a name, and a description. It may
16 * contain several script events. It also has a color in the GUI window, and is
17 * collapsible. Incidents may start as soon as the script begins to run, or they
18 * can be offset from the start of the script.
19 *
20 * @author Greg Eddington <geddingt@calpoly.edu>
21 * @author Bryan McGuffin
22 * @version 2017/06/29
23 */
24public class ScriptIncident implements I_XML_Writable
25{
26
27    /**
28     * The moments in time which have associated events.
29     */
30    public TreeMap<Integer, TimeSlice> slices;
31
32    /**
33     * GUI display color of this slice.
34     */
35    public Color color;
36
37    /**
38     * ID number for this incident.
39     */
40    public int number;
41
42    /**
43     * Name of the incident.
44     */
45    public String name;
46
47    /**
48     * Description of the incident.
49     */
50    public String description;
51
52    /**
53     * Length, in seconds, of the incident.
54     */
55    public int length = 0;
56
57    /**
58     * If true, incident appears minimized.
59     */
60    public boolean collapsed = false;
61
62    /**
63     * Number of seconds between start of simulation and start of this incident.
64     */
65    public int offset = 0;
66
67    /**
68     * Start position of the latest timeslice.
69     */
70    private int latestStart = 0;
71
72    /**
73     * Number of events in this incident.
74     */
75    public int eventCount = 0;
76
77    SimulationScript script;
78
79    /**
80     * Basic constructor.
81     *
82     * @param number The incident ID number
83     * @param name The name of the incident
84     * @param description The description of the incident
85     * @param script The script object holding this incident
86     */
87    public ScriptIncident(int number, String name, String description,
88            SimulationScript script)
89    {
90        color = Color.BLACK;
91        this.number = number;
92        this.name = name;
93        this.description = description;
94        this.script = script;
95        slices = new TreeMap<Integer, TimeSlice>();
96    }
97
98    /**
99     * Constructor with color parameter.
100     *
101     * @param color The color to use in the GUI for this event
102     * @param number The incident ID number
103     * @param name The name of the incident
104     * @param description The description of the incident
105     * @param script The script object holding this incident
106     */
107    public ScriptIncident(Color color, int number, String name,
108            String description, SimulationScript script)
109    {
110        this.color = color;
111        this.number = number;
112        this.name = name;
113        this.description = description;
114        this.script = script;
115        slices = new TreeMap<Integer, TimeSlice>();
116    }
117
118    /**
119     * Constructor with color and offset parameters.
120     *
121     * @param color The color to use in the GUI for this event
122     * @param number The incident ID number
123     * @param name The name of the incident
124     * @param description The description of the incident
125     * @param script The script object holding this incident
126     * @param offset Number of seconds after 00:00:00 that this incident begins
127     */
128    public ScriptIncident(Color color, int number, String name,
129            String description, SimulationScript script,
130            int offset)
131    {
132        this.color = color;
133        this.number = number;
134        this.name = name;
135        this.description = description;
136        this.script = script;
137        slices = new TreeMap<Integer, TimeSlice>();
138        this.setOffset(offset);
139    }
140
141    /**
142     * Set whether or not the incident is fully visible or in a compacted state.
143     *
144     * @param collapsed True if the event is compacted
145     */
146    public void setCollapsed(boolean collapsed)
147    {
148        this.collapsed = collapsed;
149        script.update();
150    }
151
152    /**
153     * Set the delay time between the start of the script and the start of this
154     * incident.
155     *
156     * @param offset Number of seconds after 00:00:00 that this incident begins
157     */
158    public void setOffset(int offset)
159    {
160        this.offset = offset;
161        script.update();
162    }
163
164    /**
165     * Add a new script event to this incident.
166     *
167     * @param ev The new event
168     * @param start Start time of this event, in seconds, from the beginning of
169     * the simulation
170     */
171    public void addNewEvent(I_ScriptEvent ev, int start)
172    {
173        TimeSlice t = slices.get(start);
174        if (t == null)
175        {
176            //System.out.println("Generating new slice at time " + start);
177            t = new TimeSlice(start, this);
178            t.addEvent(ev);
179            slices.put(start, t);
180        }
181        else
182        {
183            t.addEvent(ev);
184        }
185        eventCount++;
186
187        if (start > latestStart)
188        {
189            latestStart = start;
190            //System.out.println("Latest Start: " + latestStart);
191        }
192        if (start < offset)
193        {
194            offset = start;
195            //System.out.println("Offset: " + offset);
196        }
197        updateLength();
198    }
199
200    /**
201     * Get an array of all valid timeSlices.
202     *
203     * @return List of timeSlices which are not null
204     */
205    public ArrayList<TimeSlice> getSlices()
206    {
207        ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>();
208        for (int i = 0; i <= latestStart; i++)
209        {
210            TimeSlice ts = slices.get(i);
211            if (ts != null)
212            {
213                arr.add(ts);
214            }
215        }
216        return arr;
217    }
218
219    public void saveIncidentToFile(File f)
220    {
221        try
222        {
223            f.createNewFile();
224
225            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
226            bw.write(this.toXML());
227            bw.flush();
228            bw.close();
229
230        }
231        catch (Exception ex)
232        {
233            System.out.println("ERROR SAVING SCRIPT");
234            ex.printStackTrace();
235        }
236    }
237
238    @Override
239    public String toXML()
240    {
241        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
242        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n";
243        output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\"");
244
245        for (TimeSlice slice : slices.values())
246        {
247            output += slice.toXML();
248        }
249        output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag);
250        return output;
251    }
252
253    void insertCadData(long currentEventTime, CadData cad)
254    {
255        if(number == 100)
256        {
257            System.out.println("\n\n~~~INCIDENT 100 GOT HERE~~~\n");
258        }
259       
260       
261        int time = (int) currentEventTime;
262
263        TimeSlice slice;
264
265        if (slices.get(time) == null)
266        {
267            slices.put(time, new TimeSlice(time, this));
268        }
269        slice = slices.get(time);
270        slice.cadData = cad;
271    }
272
273    /**
274     * Update the offset and apparent length of this incident. The offset is the
275     * start time of the earliest event in the incident. The length is the time
276     * that the latest, longest-lasting event ends, minus the offset.
277     */
278    private void updateLength()
279    {
280        int lengthSoFar = 0;
281        for (int i = 0; i <= latestStart; i++)
282        {
283            TimeSlice ts = slices.get(i);
284            if (ts != null)
285            {
286                int reach = ts.getTime() + ts.getEffectiveDuration() - offset;
287                if (reach > lengthSoFar)
288                {
289                    lengthSoFar = reach;
290                }
291            }
292        }
293        length = lengthSoFar;
294    }
295
296    /**
297     * An event which is fired if the focused slice changes.
298     */
299    public static class SliceChangedEvent
300    {
301
302        public TimeSlice slice;
303
304        SliceChangedEvent(TimeSlice slice)
305        {
306            this.slice = slice;
307        }
308    }
309
310    /**
311     * Update and cause the system to focus on the given timeslice.
312     *
313     * @param i Index of the slice to focus on
314     */
315    public void setSliceActive(int i)
316    {
317        if (this.slices.get(i) != null)
318        {
319            script.broadcastEvent(new SliceChangedEvent(this.slices.get(i)));
320        }
321    }
322
323    /**
324     * An event which is fired if the focused incident changes.
325     */
326    public static class IncidentFocusedEvent
327    {
328
329        public ScriptIncident incident;
330
331        IncidentFocusedEvent(ScriptIncident i)
332        {
333            incident = i;
334        }
335    }
336
337    /**
338     * Update and cause the system to focus on this incident.
339     */
340    public void setIncidentActive()
341    {
342        script.broadcastEvent(new IncidentFocusedEvent(this));
343    }
344
345    /**
346     * String representation of this incident.
347     *
348     * @return String of the form "[Incident number] - [Incident name]"
349     */
350    @Override
351    public String toString()
352    {
353        return this.number + " - " + this.name;
354    }
355}
Note: See TracBrowser for help on using the repository browser.