Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/structures/ScriptIncident.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 116, 10.5 KB checked in by bmcguffin, 9 years ago (diff)

The first event added to any incident is automatically placed at time 0 relative to the start of the incident.

RevLine 
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.TreeMap;
9import 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 */
21public 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        TreeMap<Integer, TimeSlice> newSlices = new TreeMap<Integer, TimeSlice>();
160
161        int latest = 0;
162
163        for (Integer k : slices.keySet())
164        {
165            newSlices.put(k + (offset - old), slices.get(k));
166            latest = k + (offset - old);
167        }
168
169        latestStart = latest;
170
171        for (TimeSlice ts : newSlices.values())
172        {
173            ts.shift(offset - old);
174        }
175
176        slices = newSlices;
177        updateLength();
178        script.update();
179    }
180
181    /**
182     * Add a new script event to this incident.
183     *
184     * @param ev The new event
185     * @param start Start time of this event, in seconds, from the beginning of
186     * the simulation
187     */
188    public void addNewEvent(I_ScriptEvent ev, int start)
189    {
190        //The first event in the incident defines its start point, so it should
191        //be placed at time 0
192        if (start != 0 && slices.size() == 0)
193        {
194            addNewEvent(ev, 0);
195        }
196
197        else
198        {
199            //Check to see if there's already a timeslice here
200            TimeSlice t = slices.get(start);
201            //If not, make one; then, add the event to it
202            if (t == null)
203            {
204                t = new TimeSlice(start, this);
205                t.addEvent(ev);
206                slices.put(start, t);
207            }
208            else
209            {
210                t.addEvent(ev);
211            }
212            eventCount++;
213
214            //If this is the latest start time in the incident, update that number
215            if (start > latestStart)
216            {
217                latestStart = start;
218            }
219            //If this event is earlier than all previous events, or if there are
220            //no other events, the offset is equal to this event's start time
221            if (start < offset || eventCount == 1)
222            {
223                offset = start;
224                //System.out.println("Offset: " + offset);
225            }
226            updateLength();
227        }
228    }
229
230    /**
231     * Get an array of all valid timeSlices.
232     *
233     * @return List of timeSlices which are not null
234     */
235    public ArrayList<TimeSlice> getSlices()
236    {
237        ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>();
238        for (int i = 0; i <= latestStart; i++)
239        {
240            TimeSlice ts = slices.get(i);
241            if (ts != null)
242            {
243                arr.add(ts);
244            }
245        }
246        return arr;
247    }
248
249    /**
250     * Write this incident, in proper XML form, to the file in question.
251     *
252     * @param f the destination savefile
253     */
254    public void saveIncidentToFile(File f)
255    {
256        try
257        {
258            f.createNewFile();
259
260            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
261            bw.write(this.toXML());
262            bw.flush();
263            bw.close();
264
265        }
266        catch (Exception ex)
267        {
268            System.out.println("ERROR SAVING SCRIPT");
269            ex.printStackTrace();
270        }
271    }
272
273    @Override
274    public String toXML()
275    {
276        String output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
277        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"script.dtd\">\n";
278        output += XMLWriter.openTag(ELEMENT.TMC_SCRIPT.tag + " title=\"" + this.script.title + "\"");
279
280        for (TimeSlice slice : slices.values())
281        {
282            output += slice.toXML();
283        }
284        output += XMLWriter.closeTag(ELEMENT.TMC_SCRIPT.tag);
285        return output;
286    }
287
288    void insertCadData(long currentEventTime, CadData cad)
289    {
290        int time = (int) currentEventTime;
291
292        TimeSlice slice;
293
294        if (slices.get(time) == null)
295        {
296            slices.put(time, new TimeSlice(time, this));
297        }
298        slice = slices.get(time);
299        slice.cadData = cad;
300    }
301
302    /**
303     * Update the offset and apparent length of this incident. The offset is the
304     * start time of the earliest event in the incident. The length is the time
305     * that the latest, longest-lasting event ends, minus the offset.
306     */
307    public void updateLength()
308    {
309        int lengthSoFar = 0;
310        for (int i = 0; i <= latestStart; i++)
311        {
312            TimeSlice ts = slices.get(i);
313            if (ts != null)
314            {
315                int reach = ts.getTime() + ts.getEffectiveDuration() - offset;
316                if (reach > lengthSoFar)
317                {
318                    lengthSoFar = reach;
319                }
320            }
321        }
322        length = lengthSoFar;
323    }
324
325    /**
326     * An event which is fired if the focused slice changes.
327     */
328    public static class SliceChangedEvent
329    {
330
331        /**
332         * The slice which has received focus in this event.
333         */
334        public TimeSlice slice;
335
336        SliceChangedEvent(TimeSlice slice)
337        {
338            this.slice = slice;
339        }
340    }
341
342    /**
343     * Update and cause the system to focus on the given timeslice.
344     *
345     * @param i Index of the slice to focus on
346     */
347    public void setSliceActive(int i)
348    {
349        if (this.slices.get(i) != null)
350        {
351            script.broadcastEvent(new SliceChangedEvent(this.slices.get(i)));
352        }
353    }
354
355    /**
356     * An event which is fired if the focused incident changes.
357     */
358    public static class IncidentFocusedEvent
359    {
360
361        /**
362         * The incident which has received focus in this event.
363         */
364        public ScriptIncident incident;
365
366        IncidentFocusedEvent(ScriptIncident i)
367        {
368            incident = i;
369        }
370    }
371
372    /**
373     * Update and cause the system to focus on this incident.
374     */
375    public void setIncidentActive()
376    {
377        script.broadcastEvent(new IncidentFocusedEvent(this));
378    }
379
380    /**
381     * String representation of this incident.
382     *
383     * @return String of the form "[Incident number] - [Incident name]"
384     */
385    @Override
386    public String toString()
387    {
388        return this.number + " - " + this.name;
389    }
390
391    /**
392     * Remove the timeslice at the given position.
393     *
394     * @param seconds absolute start time of the timeslice to remove.
395     */
396    public void removeTimeSlice(int seconds)
397    {
398        //remove the timeslice
399        this.slices.remove(seconds);
400
401        //offset is equivalent to start time of earliest slice
402        if (seconds == offset)
403        {
404            offset = slices.firstKey();
405        }
406        this.updateLength();
407        script.update();
408    }
409}
Note: See TracBrowser for help on using the repository browser.