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

Revision 152, 9.3 KB checked in by jdalbey, 6 years ago (diff)

TimeSlice?.java: Prepend a newline character to fix ticket #203.

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