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

Revision 89, 8.5 KB checked in by bmcguffin, 9 years ago (diff)

Added dropdown menu item to ScriptBuilderFrame?: "Delete Incident". When clicked, user may select an existing incident to delete. Program will prompt user to confirm the deletion, then remove the incident from the script and refresh the display.

Added button to individual event editor window: "Remove this event". When clicked, the currently displayed event will be removed from the timeslice it is in. The display will be refreshed accordingly. NOTE: This still has some bugs, namely that the last remaining event in a timeslice fails to be deleted.

Restructured Interface ScriptEventEditorPanel? to include a removeAssociatedEvent method, which calls a new method in I_ScriptEvent called removeThis, which causes the event to be removed from its timeslice.

Editor.Java previously contained several classes and enums, none of which were set to private scope. Moved these extra classes to their own files to decrease clutter in Editor.java and increase readability of all files.

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