source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/panels/IncidentTimelinePanel.java @ 155

Revision 155, 29.0 KB checked in by sdanthin, 6 years ago (diff)

units.xml moved to Incidents folder

Line 
1package scriptbuilder.gui.panels;
2
3import event.editor.frame.Editor;
4import event.editor.frame.Properties;
5import java.awt.BorderLayout;
6import java.awt.Dimension;
7import java.awt.Graphics;
8import java.awt.Graphics2D;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
11import java.awt.event.MouseEvent;
12import java.io.File;
13import java.util.HashMap;
14import java.util.Map;
15import javax.swing.JFrame;
16import javax.swing.JMenuItem;
17import javax.swing.JOptionPane;
18import javax.swing.JPanel;
19import javax.swing.JPopupMenu;
20import javax.swing.SwingUtilities;
21import javax.swing.event.MouseInputAdapter;
22import scriptbuilder.gui.IncidentEditorFrame;
23import scriptbuilder.gui.ScriptBuilderFrame;
24import scriptbuilder.gui.ScriptBuilderGuiConstants;
25import scriptbuilder.gui.drawers.CursorDrawer;
26import scriptbuilder.gui.drawers.EventIconDrawer;
27import scriptbuilder.gui.drawers.IncidentTimelineDrawer;
28import scriptbuilder.structures.ScriptEvent;
29import scriptbuilder.structures.ScriptEvent.ScriptEventType;
30import scriptbuilder.structures.ScriptIncident;
31import scriptbuilder.structures.SimulationScript;
32import scriptbuilder.structures.TimeSlice;
33import scriptbuilder.structures.events.I_ScriptEvent;
34
35/**
36 * Represents a single incident timeline in the GUI. Listens for mouse actions.
37 *
38 * @author Greg Eddington <geddingt@calpoly.edu>
39 * @author Bryan McGuffin
40 * @version 2017/06/30
41 */
42public class IncidentTimelinePanel extends JPanel
43{
44
45    /**
46     * The incident this panel represents.
47     */
48    ScriptIncident incident;
49    /**
50     * If true, this panel is in its minimized state.
51     */
52    //boolean collapsed;
53    /**
54     * If false, this panel won't be drawn.
55     */
56    boolean visible;
57    /**
58     * If true, this panel has focus.
59     */
60    boolean focused;
61
62    /**
63     * If true, right-clicking on this panel will produce the popup menu.
64     */
65    private boolean hasPopupAccessRightClick;
66    /**
67     * If true, left-clicking on this panel will produce the popup menu.
68     */
69    private boolean hasPopupAccessLeftClick;
70   
71    int cursorTime, lastSlice, x, y;
72
73    /**
74     * Filler time at the end of the screen for a particular incident
75     */
76    public int requestedEditorFillerTime;
77
78    /**
79     * Filler time at the end of the screen for the whole script
80     */
81    public static int requestedScriptBuilderFillerTime = 0;
82
83    /**
84     * Constant for amount of filler to add, in seconds. Set to 15 minutes
85     */
86    public static final int FILLER_INTERVAL_SECONDS = 900;
87
88    /**
89     * The map representing the properties of this incident's events. Keys:
90     * event types. Values: Properties objects for those events.
91     */
92    public static Map<ScriptEventType, Properties> eventTypeToPropertyMap;
93
94    /**
95     * Listener for the mouse. Receives notifications when the mouse enters,
96     * exits, moves through, or clicks inside the panel.
97     */
98    public class IncidentTimelineMouseListener extends MouseInputAdapter
99    {
100
101        /**
102         * Action to take when the mouse enters the panel. Here, the incident
103         * corresponding to this panel gains focus.
104         *
105         * @param e the mouse event
106         */
107        @Override
108        public void mouseEntered(MouseEvent e)
109        {
110            incident.setIncidentActive();
111            focused = true;
112        }
113
114        /**
115         * Action to take when the mouse leaves the panel. Here, the incident
116         * loses focus and the panel gets repainted.
117         *
118         * @param e the mouse event
119         */
120        @Override
121        public void mouseExited(MouseEvent e)
122        {
123            focused = false;
124            repaint();
125        }
126
127        /*
128         *   Popup menu for incident actions
129         */
130        private JPopupMenu createPopup()
131        {
132            JPopupMenu menu = new JPopupMenu();
133            PopupMenuItemListener menuItemListener = new PopupMenuItemListener();
134            if(getTopLevelAncestor() instanceof ScriptBuilderFrame)
135            {
136                JMenuItem eventsMenuItem = new JMenuItem("Events");
137                JMenuItem propsMenuItem = new JMenuItem("Properties");
138                eventsMenuItem.setActionCommand("Edit Events");
139                propsMenuItem.setActionCommand("Modify Incident Properties");
140
141               
142
143                eventsMenuItem.addActionListener(menuItemListener);
144                propsMenuItem.addActionListener(menuItemListener);
145
146                menu.add(eventsMenuItem);
147                menu.add(propsMenuItem);
148            }
149           
150           
151           
152            return menu;
153        }
154        private JPopupMenu createPopup(int x, int y)
155        {
156            JPopupMenu menu = new JPopupMenu();
157            PopupMenuItemListener menuItemListener = new PopupMenuItemListener();
158            if(getTopLevelAncestor() instanceof IncidentEditorFrame)
159            {
160                JMenuItem addTimeMenuItem = new JMenuItem("Add time here");
161                JMenuItem removeTimeMenuItem = new JMenuItem("Remove time here");
162                cursorTime =x;
163               
164     
165                //logic that follows is used to "snap" the cursor location to the lines displayed on the timeline panel
166                if (x % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
167                        > ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2)
168                {
169                    cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
170                            - x
171                            % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
172                }
173                else
174                {
175                    cursorTime -= x
176                            % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
177                }
178
179                int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK);
180
181                newSlice *= ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION;
182               
183                    addTimeMenuItem.setActionCommand("Add Time " + newSlice);
184                    removeTimeMenuItem.setActionCommand("Remove Time " + newSlice);
185                    removeTimeMenuItem.addActionListener(menuItemListener);
186                    addTimeMenuItem.addActionListener(menuItemListener);
187                    menu.add(addTimeMenuItem);
188                    menu.add(removeTimeMenuItem);
189            }
190            return menu;
191        }
192
193        class PopupMenuItemListener implements ActionListener
194        {
195
196            public void actionPerformed(ActionEvent e)
197            {
198                JFrame topFrame = (JFrame) getTopLevelAncestor();
199                if (topFrame instanceof ScriptBuilderFrame)
200                {
201                    SimulationScript script = ((ScriptBuilderFrame) topFrame).getScript();
202                    if (e.getActionCommand().equals("Edit Events"))
203                    {
204                        IncidentEditorFrame editor = new IncidentEditorFrame(incident, (ScriptBuilderFrame) topFrame);
205                        script.addObserver(editor);
206                        editor.setVisible(true);
207                        ((ScriptBuilderFrame) topFrame).update(script, script);
208                    }
209                    if (e.getActionCommand().equals("Modify Incident Properties"))
210                    {
211                        ((ScriptBuilderFrame) topFrame).incidentDetailsScreen(incident);
212                        ((ScriptBuilderFrame) topFrame).update(script, script);
213                    }
214                }
215                if(topFrame instanceof IncidentEditorFrame)
216                {
217                    if(e.getActionCommand().substring(0,8).equals("Add Time"))
218                    {
219                        int offset = Integer.parseInt(e.getActionCommand().substring(9,e.getActionCommand().length()));
220                        String addTimeInput = JOptionPane.showInputDialog("Add time(rounded to lowest 20s): ");
221                        int addTime = 0;
222                       
223                        try
224                        {
225                            addTime = Integer.parseInt(addTimeInput);
226                        }catch(NumberFormatException exception){
227                            addTime = 0;
228                            System.err.print("an invalid number was inputted");
229                        }
230                       
231                       
232                        addTime = addTime-(addTime%20);
233                       
234                        if(offset<=incident.offset+incident.length)
235                        {
236                            incident.moveAllFollowingEvents(offset, addTime);
237                        }
238                    }
239                    if(e.getActionCommand().substring(0,11).equals("Remove Time"))
240                    {
241                        //does addTime but with a negative time
242                        int offset = Integer.parseInt(e.getActionCommand().substring(11,e.getActionCommand().length()).trim());
243                        String addTimeInput = JOptionPane.showInputDialog("Remove time(rounded to lowest 20s): ");
244                        int addTime = 0;
245                       
246                        try
247                        {
248                            addTime = Integer.parseInt(addTimeInput);
249                        }catch(NumberFormatException exception){
250                            addTime = 0;
251                            System.err.print("an invalid number was inputted");
252                        }
253                       
254                       
255                        addTime = -(addTime-(addTime%20));
256                        //this if statement might be useless?
257                        if(offset<=incident.offset+incident.length)
258                        {
259                            incident.moveAllFollowingEvents(offset, addTime);
260                        }
261                    }
262                    //add my own handling of click menu here
263                }
264                topFrame.repaint();
265            }
266        }
267        /**
268         * Note: Popup menus are triggered differently on different systems.
269         * Therefore, isPopupTrigger should be checked in both mousePressed and
270         * mouseReleased for proper cross-platform functionality.
271         * @param e event that triggered this method
272         */
273        @Override
274        public void mousePressed(MouseEvent e)
275        {
276            int currentMouseX = e.getX();
277            int currentMouseY = e.getY();
278
279            // Does user want a popup menu?
280            if (e.isPopupTrigger() && hasPopupAccessRightClick)
281            {
282                if(getTopLevelAncestor() instanceof IncidentEditorFrame)
283                {
284                    if (((IncidentEditorFrame)getTopLevelAncestor()).currentEventType == null){
285                        JPopupMenu popup = createPopup(currentMouseX,currentMouseY);
286                        popup.show(e.getComponent(), currentMouseX, currentMouseY);
287                       
288                    }
289                   
290                }else
291                {
292                    JPopupMenu popup = createPopup();
293                    popup.show(e.getComponent(), currentMouseX, currentMouseY);
294                }
295               
296               
297               
298            }
299        }
300        @Override
301        public void mouseReleased(MouseEvent e)
302        {
303            int currentMouseX = e.getX();
304            int currentMouseY = e.getY();
305
306            // Does user want a popup menu?
307            if (e.isPopupTrigger() && hasPopupAccessRightClick)
308            {
309                if(getTopLevelAncestor() instanceof IncidentEditorFrame)
310                {
311                    JPopupMenu popup = createPopup(currentMouseX,currentMouseY);
312                    popup.show(e.getComponent(), currentMouseX, currentMouseY);
313                }else
314                {
315                    JPopupMenu popup = createPopup();
316                    popup.show(e.getComponent(), currentMouseX, currentMouseY);
317                }
318            }
319        }
320
321        /**
322         * Determine if the mouse click happened within a valid timeSlice on
323         * this incident; if so, activate the Editor window for that timeSlice.
324         *todo: fix bug where the event editor window appears even when a event type is not selected.
325         * @param clickEvent the mouse event
326         */
327        @Override
328        public void mouseClicked(MouseEvent clickEvent)
329        {
330            Editor editor = null;
331            ScriptBuilderFrame scriptBuilderFrameCurrent = null;
332            IncidentEditorFrame incidentEditFrameCurrent = null;
333            if (getTopLevelAncestor() instanceof ScriptBuilderFrame)
334            {
335                scriptBuilderFrameCurrent = (ScriptBuilderFrame) getTopLevelAncestor();
336
337            }
338            else if (getTopLevelAncestor() instanceof IncidentEditorFrame)
339            {
340               
341                incidentEditFrameCurrent = (IncidentEditorFrame) getTopLevelAncestor();
342                editor = new Editor(incidentEditFrameCurrent);
343            }
344
345            x = cursorTime = clickEvent.getX();
346            y = clickEvent.getY();
347            //System.out.println(cursorTime);
348            //logic that follows is used to "snap" the cursor location to the lines displayed on the timeline panel
349            if (clickEvent.getX() % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
350                    > ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2)
351            {
352                cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
353                        - clickEvent.getX()
354                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
355            }
356            else
357            {
358                cursorTime -= clickEvent.getX()
359                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
360            }
361
362            int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK);
363           
364            newSlice *= ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION;
365            /**
366             * Check if click is out of bounds *
367             */
368            if (newSlice < 0 || incident == null)
369            {
370                return;
371            }
372
373           
374
375            /**
376             * Add a new icon if left mouse button was clicked
377             */
378            if (clickEvent.getButton() == MouseEvent.BUTTON1)
379            {
380               
381                if (getTopLevelAncestor() instanceof IncidentEditorFrame)
382                {
383                   
384                    if (incidentEditFrameCurrent.currentEventType != null)
385                    {
386                        if ((incident.slices.size() == 0 && newSlice != 0)||(incident.slices.get(0).events.isEmpty()))
387                        {
388                           
389                            JOptionPane.showMessageDialog(incidentEditFrameCurrent, "This is the first event in the incident.\n"
390                                    + "Therefore, it will be automatically positioned at time 00:00:00,\n"
391                                    + "relative to the start of the incident.", "Event will be moved", JOptionPane.INFORMATION_MESSAGE);
392                            newSlice = 0;
393                        }
394                        //add the time of addition to be passed through to the event itself somewhere in here? Stored in newSlice
395                        I_ScriptEvent newScriptEvent = ScriptEvent.factoryByType(incidentEditFrameCurrent.currentEventType);
396//                        if (editor != null)
397//                        {
398//                            //this is not necessary if we just load the slices after we create a new one in the window, creates some extra dummy slice for no reason.
399//                            //this adds a new event to the existing editor window
400//                            editor.addEvent(eventTypeToPropertyMap.get(incidentEditFrameCurrent.currentEventType), newScriptEvent);
401//                        }
402                        if (incident.slices.get(newSlice) == null || incident.slices.get(newSlice).events.isEmpty())
403                        {
404                            //if there is no slice at the newSlice time, then create a new event with a new slice at that time
405                           
406                            incident.addNewEvent(newScriptEvent, newSlice);
407                            //find out where the new slice is added and then make it
408                            //possible to
409                            //editor.setSlice(incident.getSlices().get(newSlice));
410                        }
411                        else
412                        {
413                            //if there is already a slice there, just add the event to the
414                            incident.slices.get(newSlice).addEvent(newScriptEvent);
415                        }
416                        incidentEditFrameCurrent.update(null, incidentEditFrameCurrent.getIncident());
417                    }
418                }
419            }
420//            //code below may be useless
421//            if(SwingUtilities.isRightMouseButton(clickEvent))
422//            {
423//                if (getTopLevelAncestor() instanceof IncidentEditorFrame && false)
424//                {
425//                    incidentEditFrameCurrent = (IncidentEditorFrame) getTopLevelAncestor();
426//                    if(incidentEditFrameCurrent.currentEventType == null)
427//                    {
428//                        JPopupMenu timeAddMenu = new JPopupMenu();
429//                        JMenuItem addTimeButton = new JMenuItem("Add time here");
430//                        //addTimeButton.addActionListener(al);
431//                        timeAddMenu.add(addTimeButton);
432//                        timeAddMenu.show(clickEvent.getComponent(),clickEvent.getX(),clickEvent.getY());
433//                       
434//                    }
435//                   
436//                }
437//            }
438
439            if (editor != null)
440            {
441                editor.setSlice(incident.slices.get(newSlice));
442            }
443            if (incident.slices.get(newSlice) != null
444                    && getTopLevelAncestor() instanceof IncidentEditorFrame)
445            {
446                editor.setVisible(true);
447            }
448        }
449
450        /**
451         * Determine if the mouse is now hovering over a valid timeslice; if so,
452         * alter tooltip text and info window text to reflect the new timeslice.
453         *
454         * @param e the mouse event
455         */
456        @Override
457        public void mouseMoved(MouseEvent e)
458        {
459            x = cursorTime = e.getX();
460            y = e.getY();
461
462            if (e.getX() % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
463                    > ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2)
464            {
465                cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
466                        - e.getX()
467                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
468            }
469            else
470            {
471                cursorTime -= e.getX()
472                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
473            }
474
475            if (incident != null)
476            {
477                int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK);
478                newSlice *= ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION;
479                if (newSlice >= 0 && incident.slices.get(newSlice) != null)
480                {
481                    incident.setSliceActive(newSlice);
482                    lastSlice = newSlice;
483                    String newToolTip;
484
485                    newToolTip = incident.slices.get(newSlice).getToolTipText(y);
486
487                    setToolTipText((newToolTip == null || newToolTip.equals(""))
488                            ? null : newToolTip);
489                }
490            }
491
492            repaint();
493        }
494    }
495
496    /**
497     * Constructor. Generates a HashMap of all possible event types.
498     */
499    public IncidentTimelinePanel()
500    {
501        super();
502
503        hasPopupAccessRightClick = true;
504
505        requestedEditorFillerTime = 0;
506//        FACILITATOR_EVAL_EVENT, RADIO_EVAL_EVENT
507        eventTypeToPropertyMap = new HashMap();
508        eventTypeToPropertyMap.put(ScriptEventType.AUDIO_EVENT, Properties.Audio);
509        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVENT, Properties.CADLog);
510        eventTypeToPropertyMap.put(ScriptEventType.CCTV_EVENT, Properties.CCTV);
511        eventTypeToPropertyMap.put(ScriptEventType.CHP_RADIO_EVENT, Properties.CHPRadio);
512        eventTypeToPropertyMap.put(ScriptEventType.PARAMICS_EVENT, Properties.Paramics);
513        eventTypeToPropertyMap.put(ScriptEventType.TOW_EVENT, Properties.Tow);
514        eventTypeToPropertyMap.put(ScriptEventType.UNIT_EVENT, Properties.Unit);
515        eventTypeToPropertyMap.put(ScriptEventType.WITNESS_EVENT, Properties.Witness);
516        eventTypeToPropertyMap.put(ScriptEventType.MAINTENANCE_RADIO_EVENT, Properties.MaintenanceRadio);
517        eventTypeToPropertyMap.put(ScriptEventType.TMT_RADIO_EVENT, Properties.TMTRadio);
518        eventTypeToPropertyMap.put(ScriptEventType.TELEPHONE_EVENT, Properties.Telephone);
519        eventTypeToPropertyMap.put(ScriptEventType.ATMS_EVAL_EVENT, Properties.ATMS);
520        eventTypeToPropertyMap.put(ScriptEventType.ACTIVITY_LOG_EVAL_EVENT, Properties.ActivityLog);
521        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVAL_EVENT, Properties.CAD);
522        eventTypeToPropertyMap.put(ScriptEventType.CMS_EVAL_EVENT, Properties.CMS);
523        eventTypeToPropertyMap.put(ScriptEventType.FACILITATOR_EVAL_EVENT, Properties.Facilitator);
524        eventTypeToPropertyMap.put(ScriptEventType.RADIO_EVAL_EVENT, Properties.Radio);
525
526        // Add the mouse listener
527        IncidentTimelineMouseListener mouseListener
528                = new IncidentTimelineMouseListener();
529        addMouseMotionListener(mouseListener);
530        addMouseListener(mouseListener);
531    }
532
533    /**
534     * Constructor. Generates a HashMap of all possible event types.
535     *
536     * @param usesPopup determines whether or not right-clicking on this panel
537     * will display a popup menu.
538     */
539    public IncidentTimelinePanel(boolean usesPopup)
540    {
541        super();
542
543        hasPopupAccessRightClick = usesPopup;
544
545        requestedEditorFillerTime = 0;
546//        FACILITATOR_EVAL_EVENT, RADIO_EVAL_EVENT
547        eventTypeToPropertyMap = new HashMap();
548        eventTypeToPropertyMap.put(ScriptEventType.AUDIO_EVENT, Properties.Audio);
549        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVENT, Properties.CADLog);
550        eventTypeToPropertyMap.put(ScriptEventType.CCTV_EVENT, Properties.CCTV);
551        eventTypeToPropertyMap.put(ScriptEventType.CHP_RADIO_EVENT, Properties.CHPRadio);
552        eventTypeToPropertyMap.put(ScriptEventType.PARAMICS_EVENT, Properties.Paramics);
553        eventTypeToPropertyMap.put(ScriptEventType.TOW_EVENT, Properties.Tow);
554        eventTypeToPropertyMap.put(ScriptEventType.UNIT_EVENT, Properties.Unit);
555        eventTypeToPropertyMap.put(ScriptEventType.WITNESS_EVENT, Properties.Witness);
556        eventTypeToPropertyMap.put(ScriptEventType.MAINTENANCE_RADIO_EVENT, Properties.MaintenanceRadio);
557        eventTypeToPropertyMap.put(ScriptEventType.TMT_RADIO_EVENT, Properties.TMTRadio);
558        eventTypeToPropertyMap.put(ScriptEventType.TELEPHONE_EVENT, Properties.Telephone);
559        eventTypeToPropertyMap.put(ScriptEventType.ATMS_EVAL_EVENT, Properties.ATMS);
560        eventTypeToPropertyMap.put(ScriptEventType.ACTIVITY_LOG_EVAL_EVENT, Properties.ActivityLog);
561        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVAL_EVENT, Properties.CAD);
562        eventTypeToPropertyMap.put(ScriptEventType.CMS_EVAL_EVENT, Properties.CMS);
563        eventTypeToPropertyMap.put(ScriptEventType.FACILITATOR_EVAL_EVENT, Properties.Facilitator);
564        eventTypeToPropertyMap.put(ScriptEventType.RADIO_EVAL_EVENT, Properties.Radio);
565
566        // Add the mouse listener
567        IncidentTimelineMouseListener mouseListener
568                = new IncidentTimelineMouseListener();
569        addMouseMotionListener(mouseListener);
570        addMouseListener(mouseListener);
571    }
572
573    /**
574     * Update the panel if it's changed collapsed status. Redraw it with the
575     * correct dimensions for its status.
576     *
577     * @param incident the incident this panel represents
578     */
579    public void timelinePanelUpdate(ScriptIncident incident)
580    {
581        this.incident = incident;
582        this.visible = (incident != null);
583
584        Dimension newSize;
585        if (visible)
586        {
587
588            if (getTopLevelAncestor() instanceof IncidentEditorFrame)
589            {
590
591                newSize = new Dimension(((incident.length + incident.offset + requestedEditorFillerTime)
592                        / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
593                        * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK)
594                        + ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
595                        ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT
596                        + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * 2);
597            }
598            else
599            {
600                newSize = new Dimension(((incident.length + incident.offset + requestedScriptBuilderFillerTime)
601                        / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
602                        * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK)
603                        + ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
604                        ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT
605                        + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * 2);
606            }
607
608        }
609        else
610        {
611            newSize = new Dimension(0, 0);
612        }
613        this.setSize(newSize);
614        this.setPreferredSize(newSize);
615
616        invalidate();
617    }
618
619    /**
620     * Redraw this panel and all the icons inside it. If user is holding an
621     * event, draw that icon under the mouse.
622     *
623     * @param g the graphics component
624     */
625    @Override
626    public void paint(Graphics g)
627    {
628        super.paint(g);
629
630        if (!visible)
631        {
632            return;
633        }
634
635        Graphics2D g2d = (Graphics2D) g;
636//        jdalbey removed this decision and replaced it with ELSE clause
637//                so it will work with any alternate ancestor.
638//        if (getTopLevelAncestor() instanceof ScriptBuilderFrame)
639//        {
640//            IncidentTimelineDrawer.DrawScriptBuilderTimeline(g2d, incident);
641//        }
642        if (getTopLevelAncestor() instanceof IncidentEditorFrame)
643        {
644            IncidentTimelineDrawer.DrawIncidentTimeline(g2d, incident, false);
645        }
646        else
647        {
648            IncidentTimelineDrawer.DrawScriptBuilderTimeline(g2d, incident);
649        }
650
651        if (focused)
652        {
653            //System.out.println("Cursor Time: " + cursorTime);
654            CursorDrawer.DrawCursor(g2d, cursorTime, false);
655            if (this.getTopLevelAncestor() instanceof ScriptBuilderFrame)
656            {
657                 
658                if (((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType != null)
659                {
660                    EventIconDrawer.DrawEventIcon(g2d,
661                            ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType,
662                            x + 5, y + 10);
663                }
664            }
665            if (this.getTopLevelAncestor() instanceof IncidentEditorFrame)
666            {
667                if (((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType != null)
668                {
669                    EventIconDrawer.DrawEventIcon(g2d,
670                            ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType,
671                            x + 5, y + 10);
672                   
673                }else
674                {
675                    //when we aren't selecting one of the incident adding buttons, this.
676                   
677                    //this.selectMode = ((IncidentEditorFrame)this.getTopLevelAncestor()).selectMode;
678                   
679                    if(((IncidentEditorFrame)this.getTopLevelAncestor()).selectMode)
680                    {
681                        //cursorTime dictates where we are in the x direction
682                        //there seems to be no way to tell where we are in the y direction
683                        //implement something?
684                       
685                    }
686                }
687            }
688        }
689    }
690
691    /**
692     * Local main for viewing this panel only.
693     *
694     * @author jdalbey
695     * @param args not used
696     */
697    public static void main(String[] args)
698    {
699        JFrame frame = new JFrame("ScriptBuilderTimelinePanel Demo");
700        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
701
702        IncidentTimelinePanel pnl = new IncidentTimelinePanel();
703
704        // Create a script
705        File inFile = new File("test/scriptbuilder/structures/test_input_file.xml");
706        SimulationScript script = new SimulationScript();
707        script.loadScriptFromFile(inFile);
708        // retrieve a single incident from the script
709        ScriptIncident inci = script.incidents.get(2);
710        // update this panel with an incident
711        pnl.timelinePanelUpdate(inci);
712
713        frame.getContentPane().add(pnl, BorderLayout.CENTER);
714        frame.pack();
715
716        frame.setVisible(true);
717
718    }
719}
Note: See TracBrowser for help on using the repository browser.