| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.text.SimpleDateFormat; |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Collections; |
|---|
| 6 | import java.util.Date; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | import java.util.TimeZone; |
|---|
| 9 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 10 | import scriptbuilder.structures.events.I_ScriptEvent; |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * An instance in time in the simulation. Each timeslice has a start time, an X |
|---|
| 14 | * position on the GUI timeline, and a list of ScriptEvents which occur at that |
|---|
| 15 | * time. |
|---|
| 16 | * |
|---|
| 17 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 18 | * @author Bryan McGuffin |
|---|
| 19 | * @version 2017/06/30 |
|---|
| 20 | */ |
|---|
| 21 | public class TimeSlice implements Comparable, I_XML_Writable |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Reference to the incident which contains this timeslice. |
|---|
| 26 | */ |
|---|
| 27 | private ScriptIncident thisIncident; |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * List of Script Events which begin at this instance. |
|---|
| 31 | */ |
|---|
| 32 | public List<I_ScriptEvent> events; |
|---|
| 33 | //Absolute start time of this time slice |
|---|
| 34 | private int seconds; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Constructor. |
|---|
| 38 | * |
|---|
| 39 | * @param seconds Number of seconds from the beginning of the simulation |
|---|
| 40 | * that this timeslice occurs |
|---|
| 41 | */ |
|---|
| 42 | public TimeSlice(int seconds, ScriptIncident inc) |
|---|
| 43 | { |
|---|
| 44 | this.seconds = seconds; |
|---|
| 45 | events = new ArrayList<I_ScriptEvent>(); |
|---|
| 46 | thisIncident = inc; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Add a new script event to this time slice. Sort events by event type. |
|---|
| 51 | * |
|---|
| 52 | * @param event the ScriptEvent to be added |
|---|
| 53 | */ |
|---|
| 54 | public void addEvent(I_ScriptEvent event) |
|---|
| 55 | { |
|---|
| 56 | events.add(event); |
|---|
| 57 | Collections.sort(events); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Get the X position of this timeslice on the GUI timeline. Affected by |
|---|
| 62 | * zoom level. |
|---|
| 63 | * |
|---|
| 64 | * @return Screen distance from the start of the timeline that this |
|---|
| 65 | * timeslice occurs |
|---|
| 66 | */ |
|---|
| 67 | public int getX() |
|---|
| 68 | { |
|---|
| 69 | return seconds / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 70 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Get the start time of this timeSlice. |
|---|
| 75 | * |
|---|
| 76 | * @return the start time of the timeslice in seconds, from the beginning of |
|---|
| 77 | * the simulation |
|---|
| 78 | */ |
|---|
| 79 | public int getTime() |
|---|
| 80 | { |
|---|
| 81 | return seconds; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Shift the start position of this timeslice to the right by some amount. |
|---|
| 86 | * |
|---|
| 87 | * @param amnt the number of seconds forward in time to push this slice |
|---|
| 88 | */ |
|---|
| 89 | public void shift(int amnt) |
|---|
| 90 | { |
|---|
| 91 | seconds += amnt; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Get the number of seconds that events which start in this timeslice are |
|---|
| 96 | * active. |
|---|
| 97 | * |
|---|
| 98 | * @return The duration of the longest-lasting event in this slice. |
|---|
| 99 | */ |
|---|
| 100 | public int getEffectiveDuration() |
|---|
| 101 | { |
|---|
| 102 | int dur = 0; |
|---|
| 103 | for (I_ScriptEvent e : events) |
|---|
| 104 | { |
|---|
| 105 | if (e.getLength() > dur) |
|---|
| 106 | { |
|---|
| 107 | dur = e.getLength(); |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | return dur; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Get the text to be displayed when hovering over this timeSlice. |
|---|
| 115 | * |
|---|
| 116 | * @param y the y-position of the cursor on the GUI |
|---|
| 117 | * @return The text to be displayed: all events are listed if we're on the |
|---|
| 118 | * main incident line, but only one event is listed if we're hovering over |
|---|
| 119 | * that particular event |
|---|
| 120 | */ |
|---|
| 121 | public String getToolTipText(int y) |
|---|
| 122 | { |
|---|
| 123 | int i = (y - ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_TOP_MARGIN); |
|---|
| 124 | if (i < 0) |
|---|
| 125 | { |
|---|
| 126 | if (i > (-1 * ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP)) |
|---|
| 127 | { |
|---|
| 128 | String s = toString(); |
|---|
| 129 | if (s.equals("")) |
|---|
| 130 | { |
|---|
| 131 | return null; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | return "<html>" + s.replace("\n", "<br/>") + "</html>"; |
|---|
| 135 | } |
|---|
| 136 | else |
|---|
| 137 | { |
|---|
| 138 | return null; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | i /= ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP; |
|---|
| 143 | if (i < events.size()) |
|---|
| 144 | { |
|---|
| 145 | return events.get(i).toString(); |
|---|
| 146 | } |
|---|
| 147 | return null; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * List all the events attached to this timeslice, with line breaks between. |
|---|
| 152 | * |
|---|
| 153 | * @return A list of the form: event1_type - event1_description event2_type |
|---|
| 154 | * - event2_description ... |
|---|
| 155 | */ |
|---|
| 156 | @Override |
|---|
| 157 | public String toString() |
|---|
| 158 | { |
|---|
| 159 | StringBuilder sb = new StringBuilder(); |
|---|
| 160 | |
|---|
| 161 | for (I_ScriptEvent event : events) |
|---|
| 162 | { |
|---|
| 163 | sb.append(event.toString()); |
|---|
| 164 | sb.append('\n'); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | return sb.toString(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * Order the timeslices by start time. Earlier start times come first. |
|---|
| 172 | * |
|---|
| 173 | * @param o the other timeSlice to be compared |
|---|
| 174 | * @return -1, 0 , or 1 depending on if this timeSlice comes before, |
|---|
| 175 | * simultaneously, or after the other |
|---|
| 176 | */ |
|---|
| 177 | @Override |
|---|
| 178 | public int compareTo(Object o) |
|---|
| 179 | { |
|---|
| 180 | TimeSlice other = (TimeSlice) o; |
|---|
| 181 | if (this.getTime() < other.getTime()) |
|---|
| 182 | { |
|---|
| 183 | return -1; |
|---|
| 184 | } |
|---|
| 185 | else if (this.getTime() > other.getTime()) |
|---|
| 186 | { |
|---|
| 187 | return 1; |
|---|
| 188 | } |
|---|
| 189 | else |
|---|
| 190 | { |
|---|
| 191 | return 0; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Converts the contents of this timeslice to a correctly |
|---|
| 197 | * formatted <ScriptEvent> XML element. |
|---|
| 198 | * |
|---|
| 199 | * @return XML conversion of this timeslice. |
|---|
| 200 | */ |
|---|
| 201 | @Override |
|---|
| 202 | public String toXML() |
|---|
| 203 | { |
|---|
| 204 | System.out.println("Seconds:: " + seconds); |
|---|
| 205 | String output = openTag(ELEMENT.SCRIPT_EVENT.tag); |
|---|
| 206 | SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); |
|---|
| 207 | df.setTimeZone(TimeZone.getTimeZone("GMT")); |
|---|
| 208 | output += openTag(ELEMENT.TIME_INDEX.tag) + df.format(new Date(seconds * 1000)) |
|---|
| 209 | + closeTag(ELEMENT.TIME_INDEX.tag); |
|---|
| 210 | |
|---|
| 211 | output += openTag(ELEMENT.INCIDENT.tag + " LogNum=\"" + thisIncident.number + "\""); |
|---|
| 212 | output += thisIncident.name + closeTag(ELEMENT.INCIDENT.tag); |
|---|
| 213 | |
|---|
| 214 | for (I_ScriptEvent ev : events) |
|---|
| 215 | { |
|---|
| 216 | if (ev instanceof I_XML_Writable) |
|---|
| 217 | { |
|---|
| 218 | I_XML_Writable ex = (I_XML_Writable) ev; |
|---|
| 219 | output += ex.toXML(); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | output += closeTag(ELEMENT.SCRIPT_EVENT.tag); |
|---|
| 223 | System.out.println(output); |
|---|
| 224 | return output; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | @Override |
|---|
| 228 | public String openTag(String s) |
|---|
| 229 | { |
|---|
| 230 | return "<" + s + ">\n"; |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | @Override |
|---|
| 234 | public String closeTag(String s) |
|---|
| 235 | { |
|---|
| 236 | return "</" + s + ">\n"; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | @Override |
|---|
| 240 | public String emptyTag(String s) |
|---|
| 241 | { |
|---|
| 242 | return "<" + s + "/>\n"; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|