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

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

2017/07/18: Uploaded entire prototype to SVN repo.

Line 
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.ScriptEventInterface;
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 extends Observable
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     * Basic constructor.
71     *
72     * @param number The incident ID number
73     * @param name The name of the incident
74     * @param description The description of the incident
75     * @param script The script object holding this incident
76     */
77    public ScriptIncident(int number, String name, String description,
78            SimulationScript script)
79    {
80        color = Color.BLACK;
81        this.number = number;
82        this.name = name;
83        this.description = description;
84        this.addObserver(script);
85        slices = new TreeMap<Integer, TimeSlice>();
86    }
87
88    /**
89     * Constructor with color parameter.
90     *
91     * @param color The color to use in the GUI for this event
92     * @param number The incident ID number
93     * @param name The name of the incident
94     * @param description The description of the incident
95     * @param script The script object holding this incident
96     */
97    public ScriptIncident(Color color, int number, String name,
98            String description, SimulationScript script)
99    {
100        this.color = color;
101        this.number = number;
102        this.name = name;
103        this.description = description;
104        this.addObserver(script);
105        slices = new TreeMap<Integer, TimeSlice>();
106    }
107
108    /**
109     * Constructor with color and offset parameters.
110     *
111     * @param color The color to use in the GUI for this event
112     * @param number The incident ID number
113     * @param name The name of the incident
114     * @param description The description of the incident
115     * @param script The script object holding this incident
116     * @param offset Number of seconds after 00:00:00 that this incident begins
117     */
118    public ScriptIncident(Color color, int number, String name,
119            String description, SimulationScript script,
120            int offset)
121    {
122        this.color = color;
123        this.number = number;
124        this.name = name;
125        this.description = description;
126        this.addObserver(script);
127        slices = new TreeMap<Integer, TimeSlice>();
128        this.setOffset(offset);
129    }
130
131    /**
132     * Set whether or not the incident is fully visible or in a compacted state.
133     *
134     * @param collapsed True if the event is compacted
135     */
136    public void setCollapsed(boolean collapsed)
137    {
138        this.collapsed = collapsed;
139        setChanged();
140        notifyObservers();
141    }
142
143    /**
144     * Set the delay time between the start of the script and the start of this
145     * incident.
146     *
147     * @param offset Number of seconds after 00:00:00 that this incident begins
148     */
149    public void setOffset(int offset)
150    {
151        this.offset = offset;
152        setChanged();
153        notifyObservers();
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(ScriptEventInterface 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            setChanged();
256            notifyObservers(new SliceChangedEvent(this.slices.get(i)));
257        }
258    }
259
260    /**
261     * An event which is fired if the focused incident changes.
262     */
263    public static class IncidentFocusedEvent
264    {
265
266        public ScriptIncident incident;
267
268        IncidentFocusedEvent(ScriptIncident i)
269        {
270            incident = i;
271        }
272    }
273
274    /**
275     * Update and cause the system to focus on this incident.
276     */
277    public void setIncidentActive()
278    {
279        setChanged();
280        notifyObservers(new IncidentFocusedEvent(this));
281    }
282
283    /**
284     * String representation of this incident.
285     *
286     * @return String of the form "[Incident number] - [Incident name]"
287     */
288    @Override
289    public String toString()
290    {
291        return this.number + " - " + this.name;
292    }
293}
Note: See TracBrowser for help on using the repository browser.