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 @ 9

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

Decoupled ScriptIncident? from the observer-observable pattern. SimulationScript? no longer implements Observer; instead, ScriptIncident? calls methods in SimulationScript? which notify the script's observers of a change.

Also: TimeSlice? now contains a stub method, toXML(), which returns the contents of the timeslice in XML text format.

RevLine 
1package scriptbuilder.structures;
2
3import java.awt.Color;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.List;
7import java.util.Observable;
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
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    SimulationScript script;
70
71    /**
72     * Basic constructor.
73     *
74     * @param number The incident ID number
75     * @param name The name of the incident
76     * @param description The description of the incident
77     * @param script The script object holding this incident
78     */
79    public ScriptIncident(int number, String name, String description,
80            SimulationScript script)
81    {
82        color = Color.BLACK;
83        this.number = number;
84        this.name = name;
85        this.description = description;
86        this.script = script;
87        slices = new TreeMap<Integer, TimeSlice>();
88    }
89
90    /**
91     * Constructor with color parameter.
92     *
93     * @param color The color to use in the GUI for this event
94     * @param number The incident ID number
95     * @param name The name of the incident
96     * @param description The description of the incident
97     * @param script The script object holding this incident
98     */
99    public ScriptIncident(Color color, int number, String name,
100            String description, SimulationScript script)
101    {
102        this.color = color;
103        this.number = number;
104        this.name = name;
105        this.description = description;
106        this.script = script;
107        slices = new TreeMap<Integer, TimeSlice>();
108    }
109
110    /**
111     * Constructor with color and offset parameters.
112     *
113     * @param color The color to use in the GUI for this event
114     * @param number The incident ID number
115     * @param name The name of the incident
116     * @param description The description of the incident
117     * @param script The script object holding this incident
118     * @param offset Number of seconds after 00:00:00 that this incident begins
119     */
120    public ScriptIncident(Color color, int number, String name,
121            String description, SimulationScript script,
122            int offset)
123    {
124        this.color = color;
125        this.number = number;
126        this.name = name;
127        this.description = description;
128        this.script = script;
129        slices = new TreeMap<Integer, TimeSlice>();
130        this.setOffset(offset);
131    }
132
133    /**
134     * Set whether or not the incident is fully visible or in a compacted state.
135     *
136     * @param collapsed True if the event is compacted
137     */
138    public void setCollapsed(boolean collapsed)
139    {
140        this.collapsed = collapsed;
141        script.update();
142    }
143
144    /**
145     * Set the delay time between the start of the script and the start of this
146     * incident.
147     *
148     * @param offset Number of seconds after 00:00:00 that this incident begins
149     */
150    public void setOffset(int offset)
151    {
152        this.offset = offset;
153        script.update();
154    }
155
156    /**
157     * Add a new script event to this incident.
158     *
159     * @param ev The new event
160     * @param start Start time of this event, in seconds, from the beginning of
161     * the simulation
162     */
163    public void addNewEvent(I_ScriptEvent ev, int start)
164    {
165        TimeSlice t = slices.get(start);
166        if (t == null)
167        {
168            //System.out.println("Generating new slice at time " + start);
169            t = new TimeSlice(start);
170            t.addEvent(ev);
171            slices.put(start, t);
172        }
173        else
174        {
175            t.addEvent(ev);
176        }
177        if (start > latestStart)
178        {
179            latestStart = start;
180            //System.out.println("Latest Start: " + latestStart);
181        }
182        if (start < offset)
183        {
184            offset = start;
185            //System.out.println("Offset: " + offset);
186        }
187        updateLength();
188    }
189
190    /**
191     * Get an array of all valid timeSlices.
192     *
193     * @return List of timeSlices which are not null
194     */
195    public ArrayList<TimeSlice> getSlices()
196    {
197        ArrayList<TimeSlice> arr = new ArrayList<TimeSlice>();
198        for (int i = 0; i <= latestStart; i++)
199        {
200            TimeSlice ts = slices.get(i);
201            if (ts != null)
202            {
203                arr.add(ts);
204            }
205        }
206        return arr;
207    }
208
209    /**
210     * Update the offset and apparent length of this incident. The offset is the
211     * start time of the earliest event in the incident. The length is the time
212     * that the latest, longest-lasting event ends, minus the offset.
213     */
214    private void updateLength()
215    {
216        int lengthSoFar = 0;
217        for (int i = 0; i <= latestStart; i++)
218        {
219            TimeSlice ts = slices.get(i);
220            if (ts != null)
221            {
222                int reach = ts.getTime() + ts.getEffectiveDuration() - offset;
223                if (reach > lengthSoFar)
224                {
225                    lengthSoFar = reach;
226                }
227            }
228        }
229        length = lengthSoFar + 100;
230    }
231
232    /**
233     * An event which is fired if the focused slice changes.
234     */
235    public static class SliceChangedEvent
236    {
237
238        public TimeSlice slice;
239
240        SliceChangedEvent(TimeSlice slice)
241        {
242            this.slice = slice;
243        }
244    }
245
246    /**
247     * Update and cause the system to focus on the given timeslice.
248     *
249     * @param i Index of the slice to focus on
250     */
251    public void setSliceActive(int i)
252    {
253        if (this.slices.get(i) != null)
254        {
255            script.broadcastEvent(new SliceChangedEvent(this.slices.get(i)));
256        }
257    }
258
259    /**
260     * An event which is fired if the focused incident changes.
261     */
262    public static class IncidentFocusedEvent
263    {
264
265        public ScriptIncident incident;
266
267        IncidentFocusedEvent(ScriptIncident i)
268        {
269            incident = i;
270        }
271    }
272
273    /**
274     * Update and cause the system to focus on this incident.
275     */
276    public void setIncidentActive()
277    {
278        script.broadcastEvent(new IncidentFocusedEvent(this));
279    }
280
281    /**
282     * String representation of this incident.
283     *
284     * @return String of the form "[Incident number] - [Incident name]"
285     */
286    @Override
287    public String toString()
288    {
289        return this.number + " - " + this.name;
290    }
291}
Note: See TracBrowser for help on using the repository browser.