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

Revision 30, 7.6 KB checked in by bmcguffin, 9 years ago (diff)

Made TimeSlice?, ScriptIncident?, and SimulationScript? all implement I_XML_Writable. Simulationscript also now implements a saveScriptToFile() method.

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
207        for (I_ScriptEvent e : events)
208        {
209            eventsCopy.add(e);
210        }
211
212        System.out.println("Seconds:: " + seconds);
213        String output = openTag(ELEMENT.SCRIPT_EVENT.tag);
214        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
215        df.setTimeZone(TimeZone.getTimeZone("GMT"));
216        output += openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000))
217                + closeTag(ELEMENT.TIME_INDEX.tag);
218
219        output += openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\"");
220        output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag);
221
222        output += openTag(ELEMENT.CAD_DATA.tag);
223        if (containsCADIncidentEvent())
224        {
225            output += openTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
226            for (I_ScriptEvent ev : eventsCopy)
227            {
228                if (ev instanceof I_XML_Writable && isCADIncidentEvent(ev))
229                {
230                    I_XML_Writable ex = (I_XML_Writable) ev;
231                    output += ex.toXML();
232                    eventsCopy.remove(ev);
233                }
234            }
235            output += closeTag(ELEMENT.CAD_INCIDENT_EVENT.tag);
236        }
237        output += closeTag(ELEMENT.CAD_DATA.tag);
238
239        for (I_ScriptEvent ev : eventsCopy)
240        {
241            if (ev instanceof I_XML_Writable)
242            {
243                I_XML_Writable ex = (I_XML_Writable) ev;
244                output += ex.toXML();
245            }
246        }
247        output += closeTag(ELEMENT.SCRIPT_EVENT.tag);
248        System.out.println(output);
249        return output;
250    }
251
252    @Override
253    public String openTag(String s)
254    {
255        return "<" + s + ">\n";
256    }
257
258    @Override
259    public String closeTag(String s)
260    {
261        return "</" + s + ">\n";
262    }
263
264    @Override
265    public String emptyTag(String s)
266    {
267        return "<" + s + "/>\n";
268    }
269
270    private boolean containsCADIncidentEvent()
271    {
272        for (I_ScriptEvent ev : events)
273        {
274            if (isCADIncidentEvent(ev))
275            {
276                return true;
277            }
278        }
279        return false;
280    }
281
282    private boolean isCADIncidentEvent(I_ScriptEvent ev)
283    {
284        return ev instanceof AudioEvent || ev instanceof UnitEvent
285                || ev instanceof ParamicsEvent || ev instanceof TowEvent
286                || ev instanceof WitnessEvent;
287    }
288}
Note: See TracBrowser for help on using the repository browser.