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

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

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

Added toXML implementation for Audio, CAD detail, Unit, Paramics, Telephone, and CHP radio events.

RevLine 
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    /**
26     * Reference to the incident which contains this timeslice.
27     */
28    private ScriptIncident thisIncident;
29
30    /**
31     * List of Script Events which begin at this instance.
32     */
33    public List<I_ScriptEvent> events;
34    //Absolute start time of this time slice
35    private int seconds;
36
37    /**
38     * Constructor.
39     *
40     * @param seconds Number of seconds from the beginning of the simulation
41     * that this timeslice occurs
42     */
43    public TimeSlice(int seconds, ScriptIncident inc)
44    {
45        this.seconds = seconds;
46        events = new ArrayList<I_ScriptEvent>();
47        thisIncident = inc;
48    }
49
50    /**
51     * Add a new script event to this time slice. Sort events by event type.
52     *
53     * @param event the ScriptEvent to be added
54     */
55    public void addEvent(I_ScriptEvent event)
56    {
57        events.add(event);
58        Collections.sort(events);
59    }
60
61    /**
62     * Get the X position of this timeslice on the GUI timeline. Affected by
63     * zoom level.
64     *
65     * @return Screen distance from the start of the timeline that this
66     * timeslice occurs
67     */
68    public int getX()
69    {
70        return seconds / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
71                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
72    }
73
74    /**
75     * Get the start time of this timeSlice.
76     *
77     * @return the start time of the timeslice in seconds, from the beginning of
78     * the simulation
79     */
80    public int getTime()
81    {
82        return seconds;
83    }
84
85    /**
86     * Shift the start position of this timeslice to the right by some amount.
87     *
88     * @param amnt the number of seconds forward in time to push this slice
89     */
90    public void shift(int amnt)
91    {
92        seconds += amnt;
93    }
94
95    /**
96     * Get the number of seconds that events which start in this timeslice are
97     * active.
98     *
99     * @return The duration of the longest-lasting event in this slice.
100     */
101    public int getEffectiveDuration()
102    {
103        int dur = 0;
104        for (I_ScriptEvent e : events)
105        {
106            if (e.getLength() > dur)
107            {
108                dur = e.getLength();
109            }
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        TimeSlice other = (TimeSlice) o;
182        if (this.getTime() < other.getTime())
183        {
184            return -1;
185        }
186        else if (this.getTime() > other.getTime())
187        {
188            return 1;
189        }
190        else
191        {
192            return 0;
193        }
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 = openTag(ELEMENT.SCRIPT_EVENT.tag);
215        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
216        df.setTimeZone(TimeZone.getTimeZone("GMT"));
217        output += openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000))
218                + closeTag(ELEMENT.TIME_INDEX.tag);
219
220        output += openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\"");
221        output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag);
222
223        output += openTag(ELEMENT.CAD_DATA.tag);
224        if (containsCADIncidentEvent())
225        {
226            output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
227            for (I_ScriptEvent ev : eventsCopy)
228            {
229                if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
230                {
231                    I_XML_Writable ex = (I_XML_Writable) ev;
232                    output += ex.toXML();
233                }
234                else
235                {
236                    eventsCopy2.add(ev);
237                }
238            }
239
240            eventsCopy = eventsCopy2;
241
242            output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
243        }
244        output += closeTag(ELEMENT.CAD_DATA.tag);
245
246        for (I_ScriptEvent ev : eventsCopy)
247        {
248            if (ev instanceof I_XML_Writable)
249            {
250                I_XML_Writable ex = (I_XML_Writable) ev;
251                output += ex.toXML();
252            }
253        }
254        output += closeTag(ELEMENT.SCRIPT_EVENT.tag);
255        System.out.println(output);
256        return output;
257    }
258
259    @Override
260    public String openTag(String s)
261    {
262        return "<" + s + ">\n";
263    }
264
265    @Override
266    public String closeTag(String s)
267    {
268        return "</" + s + ">\n";
269    }
270
271    @Override
272    public String emptyTag(String s)
273    {
274        return "<" + s + "/>\n";
275    }
276
277    private boolean containsCADIncidentEvent()
278    {
279        for (I_ScriptEvent ev : events)
280        {
281            if (isCADIncidentEvent(ev))
282            {
283                return true;
284            }
285        }
286        return false;
287    }
288
289    private boolean isCADIncidentEvent(I_ScriptEvent ev)
290    {
291        return ev instanceof AudioEvent || ev instanceof UnitEvent
292                || ev instanceof ParamicsEvent || ev instanceof TowEvent
293                || ev instanceof WitnessEvent;
294    }
295}
Note: See TracBrowser for help on using the repository browser.