source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/TimeSlice.java @ 67

Revision 67, 8.4 KB checked in by jdalbey, 9 years ago (diff)

TimeSlice?.java: improved implementation. TimeSliceTest?.java added a test.

Line 
1package scriptbuilder.structures;
2
3import java.text.SimpleDateFormat;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.Date;
7import java.util.List;
8import java.util.TimeZone;
9import scriptbuilder.gui.ScriptBuilderGuiConstants;
10import scriptbuilder.structures.events.*;
11import scriptbuilder.structures.events.I_ScriptEvent;
12
13/**
14 * An instance in time in the simulation. Each timeslice has a start time, an X
15 * position on the GUI timeline, and a list of ScriptEvents which occur at that
16 * time.
17 *
18 * @author Greg Eddington <geddingt@calpoly.edu>
19 * @author Bryan McGuffin
20 * @version 2017/06/30
21 */
22public class TimeSlice implements Comparable, I_XML_Writable
23{
24
25    CadData cadData = null;
26
27    /**
28     * Reference to the incident which contains this timeslice.
29     */
30    private ScriptIncident thisIncident;
31
32    /**
33     * List of Script Events which begin at this instance.
34     */
35    public List<I_ScriptEvent> events;
36    //Absolute start time of this time slice
37    private int seconds;
38
39    /**
40     * Constructor.
41     *
42     * @param seconds Number of seconds from the beginning of the simulation
43     * that this timeslice occurs
44     */
45    public TimeSlice(int seconds, ScriptIncident inc)
46    {
47        this.seconds = seconds;
48        events = new ArrayList<I_ScriptEvent>();
49        thisIncident = inc;
50    }
51
52    /**
53     * Add a new script event to this time slice. Sort events by event type.
54     *
55     * @param event the ScriptEvent to be added
56     */
57    public void addEvent(I_ScriptEvent event)
58    {
59        events.add(event);
60        Collections.sort(events);
61    }
62
63    /**
64     * Get the X position of this timeslice on the GUI timeline. Affected by
65     * zoom level.
66     *
67     * @return Screen distance from the start of the timeline that this
68     * timeslice occurs
69     */
70    public int getX()
71    {
72        return seconds / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
73                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
74    }
75
76    /**
77     * Get the start time of this timeSlice.
78     *
79     * @return the start time of the timeslice in seconds, from the beginning of
80     * the simulation
81     */
82    public int getTime()
83    {
84        return seconds;
85    }
86
87    /**
88     * Shift the start position of this timeslice to the right by some amount.
89     *
90     * @param amnt the number of seconds forward in time to push this slice
91     */
92    public void shift(int amnt)
93    {
94        seconds += amnt;
95    }
96
97    /**
98     * Get the number of seconds that events which start in this timeslice are
99     * active.
100     *
101     * @return The duration of the longest-lasting event in this slice.
102     */
103    public int getEffectiveDuration()
104    {
105        int dur = 0;
106        for (I_ScriptEvent evt : events)
107        {
108            // save the largest of current dur or this evt length
109            dur = Math.max(dur, evt.getLength());
110        }
111        return dur;
112    }
113
114    /**
115     * Get the text to be displayed when hovering over this timeSlice.
116     *
117     * @param y the y-position of the cursor on the GUI
118     * @return The text to be displayed: all events are listed if we're on the
119     * main incident line, but only one event is listed if we're hovering over
120     * that particular event
121     */
122    public String getToolTipText(int y)
123    {
124        int i = (y - ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_TOP_MARGIN);
125        if (i < 0)
126        {
127            if (i > (-1 * ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP))
128            {
129                String s = toString();
130                if (s.equals(""))
131                {
132                    return null;
133                }
134
135                return "<html>" + s.replace("\n", "<br/>") + "</html>";
136            }
137            else
138            {
139                return null;
140            }
141        }
142
143        i /= ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP;
144        if (i < events.size())
145        {
146            return events.get(i).toString();
147        }
148        return null;
149    }
150
151    /**
152     * List all the events attached to this timeslice, with line breaks between.
153     *
154     * @return A list of the form: event1_type - event1_description event2_type
155     * - event2_description ...
156     */
157    @Override
158    public String toString()
159    {
160        StringBuilder sb = new StringBuilder();
161
162        for (I_ScriptEvent event : events)
163        {
164            sb.append(event.toString());
165            sb.append('\n');
166        }
167
168        return sb.toString();
169    }
170
171    /**
172     * Order the timeslices by start time. Earlier start times come first.
173     *
174     * @param o the other timeSlice to be compared
175     * @return -1, 0 , or 1 depending on if this timeSlice comes before,
176     * simultaneously, or after the other
177     */
178    @Override
179    public int compareTo(Object o)
180    {
181        if (o instanceof TimeSlice)
182        {
183            TimeSlice other = (TimeSlice) o;
184            if (this.getTime() < other.getTime())
185            {
186                return -1;
187            }
188            else if (this.getTime() > other.getTime())
189            {
190                return 1;
191            }
192        }
193        return 0;
194    }
195
196    /**
197     * Converts the contents of this timeslice to a correctly formatted
198     * <ScriptEvent> XML element.
199     *
200     * @return XML conversion of this timeslice.
201     */
202    @Override
203    public String toXML()
204    {
205        ArrayList<I_ScriptEvent> eventsCopy = new ArrayList<I_ScriptEvent>();
206        ArrayList<I_ScriptEvent> eventsCopy2 = new ArrayList<I_ScriptEvent>();
207
208        for (I_ScriptEvent e : events)
209        {
210            eventsCopy.add(e);
211        }
212
213        System.out.println("Seconds:: " + seconds);
214        String output = XMLWriter.openTag(ELEMENT.SCRIPT_EVENT.tag);
215        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
216        df.setTimeZone(TimeZone.getTimeZone("GMT"));
217        output += XMLWriter.openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000))
218                + XMLWriter.closeTag(ELEMENT.TIME_INDEX.tag);
219
220        output += XMLWriter.openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\"");
221        output += thisIncident.name + XMLWriter.closeTag(ELEMENT.INCIDENT.tag);
222
223        if ((cadData != null && cadData.hasCadData()) || containsCADIncidentEvent())
224        {
225            output += XMLWriter.openTag(ELEMENT.CAD_DATA.tag);
226            if (cadData != null)
227            {
228                output += cadData.toXML();
229            }
230
231            if (containsCADIncidentEvent())
232            {
233                output += XMLWriter.openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
234                for (I_ScriptEvent ev : eventsCopy)
235                {
236                    if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
237                    {
238                        I_XML_Writable ex = (I_XML_Writable) ev;
239                        output += ex.toXML();
240                    }
241                    else
242                    {
243                        eventsCopy2.add(ev);
244                    }
245                }
246
247                eventsCopy = eventsCopy2;
248                output += XMLWriter.closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
249            }
250
251            output += XMLWriter.closeTag(ELEMENT.CAD_DATA.tag);
252        }
253
254        if (cadData != null && cadData.hasGeneralInfo())
255        {
256            output += XMLWriter.openTag(ELEMENT.GENERAL_INFO.tag);
257
258            output += XMLWriter.simpleTag(cadData.General_Title, ELEMENT.TITLE);
259
260            output += XMLWriter.simpleTag(cadData.General_Text, ELEMENT.TEXT);
261
262            output += XMLWriter.closeTag(ELEMENT.GENERAL_INFO.tag);
263
264        }
265
266        for (I_ScriptEvent ev : eventsCopy)
267        {
268            if (ev instanceof I_XML_Writable)
269            {
270                I_XML_Writable ex = (I_XML_Writable) ev;
271                output += ex.toXML();
272            }
273        }
274        output += XMLWriter.closeTag(ELEMENT.SCRIPT_EVENT.tag);
275        System.out.println(output);
276        return output;
277    }
278
279    private boolean containsCADIncidentEvent()
280    {
281        for (I_ScriptEvent ev : events)
282        {
283            if (isCADIncidentEvent(ev))
284            {
285                return true;
286            }
287        }
288        return false;
289    }
290
291    private boolean isCADIncidentEvent(I_ScriptEvent ev)
292    {
293        return ev instanceof AudioEvent || ev instanceof UnitEvent
294                || ev instanceof ParamicsEvent || ev instanceof TowEvent
295                || ev instanceof WitnessEvent || ev instanceof CADEvent;
296    }
297}
Note: See TracBrowser for help on using the repository browser.