| 1 | package scriptbuilder.gui.panels; |
|---|
| 2 | |
|---|
| 3 | import event.editor.Editor; |
|---|
| 4 | import event.editor.Properties; |
|---|
| 5 | import java.awt.Dimension; |
|---|
| 6 | import java.awt.Graphics; |
|---|
| 7 | import java.awt.Graphics2D; |
|---|
| 8 | import java.awt.event.MouseEvent; |
|---|
| 9 | import java.util.HashMap; |
|---|
| 10 | import java.util.Map; |
|---|
| 11 | import javax.swing.JPanel; |
|---|
| 12 | import javax.swing.event.MouseInputAdapter; |
|---|
| 13 | import scriptbuilder.gui.ScriptBuilderFrame; |
|---|
| 14 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 15 | import scriptbuilder.gui.drawers.CursorDrawer; |
|---|
| 16 | import scriptbuilder.gui.drawers.EventIconDrawer; |
|---|
| 17 | import scriptbuilder.gui.drawers.IncidentTimelineDrawer; |
|---|
| 18 | import scriptbuilder.structures.ScriptEvent; |
|---|
| 19 | import scriptbuilder.structures.ScriptEvent.ScriptEventType; |
|---|
| 20 | import scriptbuilder.structures.ScriptIncident; |
|---|
| 21 | import scriptbuilder.structures.TimeSlice; |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * |
|---|
| 25 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 26 | */ |
|---|
| 27 | public class IncidentTimelinePanel extends JPanel |
|---|
| 28 | { |
|---|
| 29 | ScriptIncident incident; |
|---|
| 30 | boolean collapsed; |
|---|
| 31 | boolean visible; |
|---|
| 32 | boolean focused; |
|---|
| 33 | int cursorTime, lastSlice, x, y; |
|---|
| 34 | Map<ScriptEventType, Properties> eventTypeToPropertyMap; |
|---|
| 35 | |
|---|
| 36 | public class IncidentTimelineMouseListener extends MouseInputAdapter |
|---|
| 37 | { |
|---|
| 38 | @Override |
|---|
| 39 | public void mouseEntered(MouseEvent e) |
|---|
| 40 | { |
|---|
| 41 | incident.setIncidentActive(); |
|---|
| 42 | focused = true; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | @Override |
|---|
| 46 | public void mouseExited(MouseEvent e) |
|---|
| 47 | { |
|---|
| 48 | focused = false; |
|---|
| 49 | repaint(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | @Override |
|---|
| 53 | public void mouseClicked(MouseEvent e) |
|---|
| 54 | { |
|---|
| 55 | Editor ed = new Editor(); |
|---|
| 56 | ScriptBuilderFrame f = (ScriptBuilderFrame)getTopLevelAncestor(); |
|---|
| 57 | |
|---|
| 58 | x = cursorTime = e.getX(); |
|---|
| 59 | y = e.getY(); |
|---|
| 60 | |
|---|
| 61 | if (e.getX() % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK > |
|---|
| 62 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2) |
|---|
| 63 | { |
|---|
| 64 | cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK - |
|---|
| 65 | e.getX() % |
|---|
| 66 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK; |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | cursorTime -= e.getX() % |
|---|
| 71 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK) - |
|---|
| 75 | (incident.offset / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION); |
|---|
| 76 | |
|---|
| 77 | /** Check if click is out of bounds **/ |
|---|
| 78 | if (!(newSlice < incident.slices.size() && newSlice >= 0) || incident == null) |
|---|
| 79 | { |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | for (ScriptEvent se : incident.slices.get(newSlice).events) |
|---|
| 84 | { |
|---|
| 85 | ed.addProperty(eventTypeToPropertyMap.get(se.getScriptEventType())); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** Add a new icon if left mouse button was clicked **/ |
|---|
| 89 | if (e.getButton() == MouseEvent.BUTTON1) |
|---|
| 90 | { |
|---|
| 91 | if (f.currentEventType != null) |
|---|
| 92 | { |
|---|
| 93 | ed.addProperty(eventTypeToPropertyMap.get(f.currentEventType)); |
|---|
| 94 | incident.slices.get(newSlice).addEvent(new ScriptEvent(f.currentEventType)); |
|---|
| 95 | f.update(f.getScript(), f.getScript()); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | ed.setVisible(true); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | @Override |
|---|
| 103 | public void mouseMoved(MouseEvent e) |
|---|
| 104 | { |
|---|
| 105 | x = cursorTime = e.getX(); |
|---|
| 106 | y = e.getY(); |
|---|
| 107 | |
|---|
| 108 | if (e.getX() % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK > |
|---|
| 109 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2) |
|---|
| 110 | { |
|---|
| 111 | cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK - |
|---|
| 112 | e.getX() % |
|---|
| 113 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK; |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | { |
|---|
| 117 | cursorTime -= e.getX() % |
|---|
| 118 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | if (incident != null) |
|---|
| 123 | { |
|---|
| 124 | int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK) - |
|---|
| 125 | (incident.offset / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION); |
|---|
| 126 | if (newSlice < incident.slices.size() && newSlice >= 0) |
|---|
| 127 | { |
|---|
| 128 | incident.setSliceActive(newSlice); |
|---|
| 129 | lastSlice = newSlice; |
|---|
| 130 | String newToolTip; |
|---|
| 131 | if (collapsed) |
|---|
| 132 | newToolTip = incident.slices.get(newSlice).toString(); |
|---|
| 133 | else |
|---|
| 134 | newToolTip = incident.slices.get(newSlice).getToolTipText(y); |
|---|
| 135 | setToolTipText((newToolTip == null || newToolTip.equals("")) |
|---|
| 136 | ? null : newToolTip); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | repaint(); |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | public IncidentTimelinePanel() |
|---|
| 145 | { |
|---|
| 146 | super(); |
|---|
| 147 | |
|---|
| 148 | // FACILITATOR_EVAL_EVENT, RADIO_EVAL_EVENT |
|---|
| 149 | |
|---|
| 150 | eventTypeToPropertyMap = new HashMap(); |
|---|
| 151 | eventTypeToPropertyMap.put(ScriptEventType.AUDIO_EVENT, Properties.Audio); |
|---|
| 152 | eventTypeToPropertyMap.put(ScriptEventType.CAD_EVENT, Properties.CADLog); |
|---|
| 153 | eventTypeToPropertyMap.put(ScriptEventType.CCTV_EVENT, Properties.CCTV); |
|---|
| 154 | eventTypeToPropertyMap.put(ScriptEventType.CHP_RADIO_EVENT, Properties.CHPRadio); |
|---|
| 155 | eventTypeToPropertyMap.put(ScriptEventType.PARAMICS_EVENT, Properties.Paramics); |
|---|
| 156 | eventTypeToPropertyMap.put(ScriptEventType.TOW_EVENT, Properties.Tow); |
|---|
| 157 | eventTypeToPropertyMap.put(ScriptEventType.UNIT_EVENT, Properties.Unit); |
|---|
| 158 | eventTypeToPropertyMap.put(ScriptEventType.WITNESS_EVENT, Properties.Witness); |
|---|
| 159 | eventTypeToPropertyMap.put(ScriptEventType.MAINTENANCE_RADIO_EVENT, Properties.MaintenanceRadio); |
|---|
| 160 | eventTypeToPropertyMap.put(ScriptEventType.TMT_RADIO_EVENT, Properties.TMTRadio); |
|---|
| 161 | eventTypeToPropertyMap.put(ScriptEventType.TELEPHONE_EVENT, Properties.Telephone); |
|---|
| 162 | eventTypeToPropertyMap.put(ScriptEventType.ATMS_EVAL_EVENT, Properties.ATMS); |
|---|
| 163 | eventTypeToPropertyMap.put(ScriptEventType.ACTIVITY_LOG_EVAL_EVENT, Properties.ActivityLog); |
|---|
| 164 | eventTypeToPropertyMap.put(ScriptEventType.CAD_EVAL_EVENT, Properties.CAD); |
|---|
| 165 | eventTypeToPropertyMap.put(ScriptEventType.CMS_EVAL_EVENT, Properties.CMS); |
|---|
| 166 | eventTypeToPropertyMap.put(ScriptEventType.FACILITATOR_EVAL_EVENT, Properties.Facilitator); |
|---|
| 167 | eventTypeToPropertyMap.put(ScriptEventType.RADIO_EVAL_EVENT, Properties.Radio); |
|---|
| 168 | |
|---|
| 169 | // Add the mouse listener |
|---|
| 170 | IncidentTimelineMouseListener mouseListener = |
|---|
| 171 | new IncidentTimelineMouseListener(); |
|---|
| 172 | addMouseMotionListener(mouseListener); |
|---|
| 173 | addMouseListener(mouseListener); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | public void update(ScriptIncident incident) |
|---|
| 177 | { |
|---|
| 178 | this.incident = incident; |
|---|
| 179 | if (incident != null) |
|---|
| 180 | this.collapsed = incident.collapsed; |
|---|
| 181 | this.visible = incident != null; |
|---|
| 182 | |
|---|
| 183 | Dimension newSize; |
|---|
| 184 | if (visible) |
|---|
| 185 | { |
|---|
| 186 | if (collapsed) |
|---|
| 187 | { |
|---|
| 188 | newSize = new Dimension((incident.length + incident.offset) / |
|---|
| 189 | ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION * |
|---|
| 190 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK, |
|---|
| 191 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT); |
|---|
| 192 | } |
|---|
| 193 | else |
|---|
| 194 | { |
|---|
| 195 | int mostEvents = 0; |
|---|
| 196 | for (TimeSlice slice : incident.slices) |
|---|
| 197 | { |
|---|
| 198 | if (slice.events.size() > mostEvents) |
|---|
| 199 | mostEvents = slice.events.size(); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | newSize = new Dimension((incident.length + incident.offset) / |
|---|
| 203 | ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION * |
|---|
| 204 | ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK, |
|---|
| 205 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT + |
|---|
| 206 | ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * (mostEvents + 1)); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | else |
|---|
| 210 | { |
|---|
| 211 | newSize = new Dimension(0, 0); |
|---|
| 212 | } |
|---|
| 213 | this.setSize(newSize); |
|---|
| 214 | this.setPreferredSize(newSize); |
|---|
| 215 | |
|---|
| 216 | invalidate(); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | @Override |
|---|
| 220 | public void paint(Graphics g) |
|---|
| 221 | { |
|---|
| 222 | super.paint(g); |
|---|
| 223 | |
|---|
| 224 | if (!visible) |
|---|
| 225 | return; |
|---|
| 226 | |
|---|
| 227 | Graphics2D g2d = (Graphics2D)g; |
|---|
| 228 | IncidentTimelineDrawer.DrawIncidentTimeline(g2d, incident, collapsed); |
|---|
| 229 | |
|---|
| 230 | if (focused) |
|---|
| 231 | { |
|---|
| 232 | CursorDrawer.DrawCursor(g2d, cursorTime, false); |
|---|
| 233 | if (((ScriptBuilderFrame)this.getTopLevelAncestor()).currentEventType != null) |
|---|
| 234 | EventIconDrawer.DrawEventIcon(g2d, |
|---|
| 235 | ((ScriptBuilderFrame)this.getTopLevelAncestor()).currentEventType, |
|---|
| 236 | x+5, y+10); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | } |
|---|