Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/gui/panels/IncidentTimelinePanel.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 150, 28.3 KB checked in by sdanthin, 6 years ago (diff)

IncidentTimelinePanel?.java added right-click menu popup functionality to add time
ScriptIncident?.java Added moveAllFollowingEvents function
ScriptIncidentTest?.java Added test to test moveAllFollowingEvents - may need better edge cases

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