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

Revision 145, 9.3 KB checked in by sdanthin, 6 years ago (diff)

Move from Git to Svn (LARGE COMMIT)

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     * 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        output += XMLWriter.openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000))
231                + XMLWriter.closeTag(ELEMENT.TIME_INDEX.tag);
232
233        output += XMLWriter.openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\"");
234        output += thisIncident.name + XMLWriter.closeTag(ELEMENT.INCIDENT.tag);
235        output += XMLWriter.emptyTag(ELEMENT.COLOR.tag
236                + " r=\"" + thisIncident.color.getRed() + "\""
237                + " g=\"" + thisIncident.color.getGreen() + "\""
238                + " b=\"" + thisIncident.color.getBlue() + "\"");
239
240        if ((cadData != null && cadData.hasCadData()) || containsCADIncidentEvent())
241        {
242            output += XMLWriter.openTag(ELEMENT.CAD_DATA.tag);
243            if (cadData != null)
244            {
245                output += cadData.toXML();
246            }
247
248            if (containsCADIncidentEvent())
249            {
250                output += XMLWriter.openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
251                for (I_ScriptEvent ev : eventsCopy)
252                {
253                    if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
254                    {
255                        I_XML_Writable ex = (I_XML_Writable) ev;
256                        output += ex.toXML();
257                    }
258                    else
259                    {
260                        eventsCopy2.add(ev);
261                    }
262                }
263
264                eventsCopy = eventsCopy2;
265                output += XMLWriter.closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
266            }
267
268            output += XMLWriter.closeTag(ELEMENT.CAD_DATA.tag);
269        }
270
271        if (cadData != null && cadData.hasGeneralInfo())
272        {
273            output += XMLWriter.openTag(ELEMENT.GENERAL_INFO.tag);
274
275            output += XMLWriter.simpleTag(cadData.General_Title, ELEMENT.TITLE);
276
277            output += XMLWriter.simpleTag(cadData.General_Text, ELEMENT.TEXT);
278
279            output += XMLWriter.closeTag(ELEMENT.GENERAL_INFO.tag);
280
281        }
282
283        for (I_ScriptEvent ev : eventsCopy)
284        {
285            if (ev instanceof I_XML_Writable)
286            {
287                I_XML_Writable ex = (I_XML_Writable) ev;
288                output += ex.toXML();
289            }
290        }
291        output += XMLWriter.closeTag(ELEMENT.SCRIPT_EVENT.tag);
292        return output;
293    }
294
295    private boolean containsCADIncidentEvent()
296    {
297        for (I_ScriptEvent ev : events)
298        {
299            if (isCADIncidentEvent(ev))
300            {
301                return true;
302            }
303        }
304        return false;
305    }
306
307    private boolean isCADIncidentEvent(I_ScriptEvent ev)
308    {
309        return ev instanceof AudioEvent || ev instanceof UnitEvent
310                || ev instanceof ParamicsEvent || ev instanceof TowEvent
311                || ev instanceof WitnessEvent || ev instanceof CADEvent;
312    }
313
314    public void checkEmpty()
315    {
316        if (this.events.isEmpty())
317        {
318            this.thisIncident.removeTimeSlice(this.seconds);
319        }
320    }
321}
Note: See TracBrowser for help on using the repository browser.