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

Revision 172, 28.5 KB checked in by sdanthin, 6 years ago (diff)

IncidentTimelinePanel?.java changed time addition/removal message

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 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 in seconds(this will be automatically 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 in seconds(this will be automatically 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                        //creates a new script event
395                        I_ScriptEvent newScriptEvent = ScriptEvent.factoryByType(incidentEditFrameCurrent.currentEventType);
396                        if (incident.slices.get(newSlice) == null || incident.slices.get(newSlice).events.isEmpty())
397                        {
398                            //if there is no slice at the newSlice time, then create a new event with a new slice at that time
399                           
400                            incident.addNewEvent(newScriptEvent, newSlice);
401                            //find out where the new slice is added and then make it
402                            //possible to
403                            //editor.setSlice(incident.getSlices().get(newSlice));
404                        }
405                        else
406                        {
407                            //if there is already a slice there, just add the event to the
408                            incident.slices.get(newSlice).addEvent(newScriptEvent);
409                        }
410                        incidentEditFrameCurrent.update(null, incidentEditFrameCurrent.getIncident());
411                    }
412                }
413            }
414//            //code below may be useless
415//            if(SwingUtilities.isRightMouseButton(clickEvent))
416//            {
417//                if (getTopLevelAncestor() instanceof IncidentEditorFrame && false)
418//                {
419//                    incidentEditFrameCurrent = (IncidentEditorFrame) getTopLevelAncestor();
420//                    if(incidentEditFrameCurrent.currentEventType == null)
421//                    {
422//                        JPopupMenu timeAddMenu = new JPopupMenu();
423//                        JMenuItem addTimeButton = new JMenuItem("Add time here");
424//                        //addTimeButton.addActionListener(al);
425//                        timeAddMenu.add(addTimeButton);
426//                        timeAddMenu.show(clickEvent.getComponent(),clickEvent.getX(),clickEvent.getY());
427//                       
428//                    }
429//                   
430//                }
431//            }
432
433            if (editor != null)
434            {
435                editor.setSlice(incident.slices.get(newSlice));
436            }
437            if (incident.slices.get(newSlice) != null
438                    && getTopLevelAncestor() instanceof IncidentEditorFrame)
439            {
440                editor.setVisible(true);
441            }
442        }
443
444        /**
445         * Determine if the mouse is now hovering over a valid timeslice; if so,
446         * alter tooltip text and info window text to reflect the new timeslice.
447         *
448         * @param e the mouse event
449         */
450        @Override
451        public void mouseMoved(MouseEvent e)
452        {
453            x = cursorTime = e.getX();
454            y = e.getY();
455
456            if (e.getX() % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
457                    > ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK / 2)
458            {
459                cursorTime += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
460                        - e.getX()
461                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
462            }
463            else
464            {
465                cursorTime -= e.getX()
466                        % ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK;
467            }
468
469            if (incident != null)
470            {
471                int newSlice = (cursorTime / ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK);
472                newSlice *= ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION;
473                if (newSlice >= 0 && incident.slices.get(newSlice) != null)
474                {
475                    incident.setSliceActive(newSlice);
476                    lastSlice = newSlice;
477                    String newToolTip;
478
479                    newToolTip = incident.slices.get(newSlice).getToolTipText(y);
480
481                    setToolTipText((newToolTip == null || newToolTip.equals(""))
482                            ? null : newToolTip);
483                }
484            }
485
486            repaint();
487        }
488    }
489
490    /**
491     * Constructor. Generates a HashMap of all possible event types.
492     */
493    public IncidentTimelinePanel()
494    {
495        super();
496
497        hasPopupAccessRightClick = true;
498
499        requestedEditorFillerTime = 0;
500//        FACILITATOR_EVAL_EVENT, RADIO_EVAL_EVENT
501        eventTypeToPropertyMap = new HashMap();
502        eventTypeToPropertyMap.put(ScriptEventType.AUDIO_EVENT, Properties.Audio);
503        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVENT, Properties.CADLog);
504        eventTypeToPropertyMap.put(ScriptEventType.CCTV_EVENT, Properties.CCTV);
505        eventTypeToPropertyMap.put(ScriptEventType.CHP_RADIO_EVENT, Properties.CHPRadio);
506        eventTypeToPropertyMap.put(ScriptEventType.PARAMICS_EVENT, Properties.Paramics);
507        eventTypeToPropertyMap.put(ScriptEventType.TOW_EVENT, Properties.Tow);
508        eventTypeToPropertyMap.put(ScriptEventType.UNIT_EVENT, Properties.Unit);
509        eventTypeToPropertyMap.put(ScriptEventType.WITNESS_EVENT, Properties.Witness);
510        eventTypeToPropertyMap.put(ScriptEventType.MAINTENANCE_RADIO_EVENT, Properties.MaintenanceRadio);
511        eventTypeToPropertyMap.put(ScriptEventType.TMT_RADIO_EVENT, Properties.TMTRadio);
512        eventTypeToPropertyMap.put(ScriptEventType.TELEPHONE_EVENT, Properties.Telephone);
513        eventTypeToPropertyMap.put(ScriptEventType.ATMS_EVAL_EVENT, Properties.ATMS);
514        eventTypeToPropertyMap.put(ScriptEventType.ACTIVITY_LOG_EVAL_EVENT, Properties.ActivityLog);
515        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVAL_EVENT, Properties.CAD);
516        eventTypeToPropertyMap.put(ScriptEventType.CMS_EVAL_EVENT, Properties.CMS);
517        eventTypeToPropertyMap.put(ScriptEventType.FACILITATOR_EVAL_EVENT, Properties.Facilitator);
518        eventTypeToPropertyMap.put(ScriptEventType.RADIO_EVAL_EVENT, Properties.Radio);
519
520        // Add the mouse listener
521        IncidentTimelineMouseListener mouseListener
522                = new IncidentTimelineMouseListener();
523        addMouseMotionListener(mouseListener);
524        addMouseListener(mouseListener);
525    }
526
527    /**
528     * Constructor. Generates a HashMap of all possible event types.
529     *
530     * @param usesPopup determines whether or not right-clicking on this panel
531     * will display a popup menu.
532     */
533    public IncidentTimelinePanel(boolean usesPopup)
534    {
535        super();
536
537        hasPopupAccessRightClick = usesPopup;
538
539        requestedEditorFillerTime = 0;
540//        FACILITATOR_EVAL_EVENT, RADIO_EVAL_EVENT
541        eventTypeToPropertyMap = new HashMap();
542        eventTypeToPropertyMap.put(ScriptEventType.AUDIO_EVENT, Properties.Audio);
543        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVENT, Properties.CADLog);
544        eventTypeToPropertyMap.put(ScriptEventType.CCTV_EVENT, Properties.CCTV);
545        eventTypeToPropertyMap.put(ScriptEventType.CHP_RADIO_EVENT, Properties.CHPRadio);
546        eventTypeToPropertyMap.put(ScriptEventType.PARAMICS_EVENT, Properties.Paramics);
547        eventTypeToPropertyMap.put(ScriptEventType.TOW_EVENT, Properties.Tow);
548        eventTypeToPropertyMap.put(ScriptEventType.UNIT_EVENT, Properties.Unit);
549        eventTypeToPropertyMap.put(ScriptEventType.WITNESS_EVENT, Properties.Witness);
550        eventTypeToPropertyMap.put(ScriptEventType.MAINTENANCE_RADIO_EVENT, Properties.MaintenanceRadio);
551        eventTypeToPropertyMap.put(ScriptEventType.TMT_RADIO_EVENT, Properties.TMTRadio);
552        eventTypeToPropertyMap.put(ScriptEventType.TELEPHONE_EVENT, Properties.Telephone);
553        eventTypeToPropertyMap.put(ScriptEventType.ATMS_EVAL_EVENT, Properties.ATMS);
554        eventTypeToPropertyMap.put(ScriptEventType.ACTIVITY_LOG_EVAL_EVENT, Properties.ActivityLog);
555        eventTypeToPropertyMap.put(ScriptEventType.CAD_EVAL_EVENT, Properties.CAD);
556        eventTypeToPropertyMap.put(ScriptEventType.CMS_EVAL_EVENT, Properties.CMS);
557        eventTypeToPropertyMap.put(ScriptEventType.FACILITATOR_EVAL_EVENT, Properties.Facilitator);
558        eventTypeToPropertyMap.put(ScriptEventType.RADIO_EVAL_EVENT, Properties.Radio);
559
560        // Add the mouse listener
561        IncidentTimelineMouseListener mouseListener
562                = new IncidentTimelineMouseListener();
563        addMouseMotionListener(mouseListener);
564        addMouseListener(mouseListener);
565    }
566
567    /**
568     * Update the panel if it's changed collapsed status. Redraw it with the
569     * correct dimensions for its status.
570     *
571     * @param incident the incident this panel represents
572     */
573    public void timelinePanelUpdate(ScriptIncident incident)
574    {
575        this.incident = incident;
576        this.visible = (incident != null);
577
578        Dimension newSize;
579        if (visible)
580        {
581
582            if (getTopLevelAncestor() instanceof IncidentEditorFrame)
583            {
584
585                newSize = new Dimension(((incident.length + incident.offset + requestedEditorFillerTime)
586                        / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
587                        * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK)
588                        + ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
589                        ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT
590                        + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * 2);
591            }
592            else
593            {
594                newSize = new Dimension(((incident.length + incident.offset + requestedScriptBuilderFillerTime)
595                        / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
596                        * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK)
597                        + ScriptBuilderGuiConstants.EVENT_ICON_WIDTH,
598                        ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT
599                        + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * 2);
600            }
601
602        }
603        else
604        {
605            newSize = new Dimension(0, 0);
606        }
607        this.setSize(newSize);
608        this.setPreferredSize(newSize);
609
610        invalidate();
611    }
612
613    /**
614     * Redraw this panel and all the icons inside it. If user is holding an
615     * event, draw that icon under the mouse.
616     *
617     * @param g the graphics component
618     */
619    @Override
620    public void paint(Graphics g)
621    {
622        super.paint(g);
623
624        if (!visible)
625        {
626            return;
627        }
628
629        Graphics2D g2d = (Graphics2D) g;
630//        jdalbey removed this decision and replaced it with ELSE clause
631//                so it will work with any alternate ancestor.
632//        if (getTopLevelAncestor() instanceof ScriptBuilderFrame)
633//        {
634//            IncidentTimelineDrawer.DrawScriptBuilderTimeline(g2d, incident);
635//        }
636        if (getTopLevelAncestor() instanceof IncidentEditorFrame)
637        {
638            IncidentTimelineDrawer.DrawIncidentTimeline(g2d, incident, false);
639        }
640        else
641        {
642            IncidentTimelineDrawer.DrawScriptBuilderTimeline(g2d, incident);
643        }
644
645        if (focused)
646        {
647            //System.out.println("Cursor Time: " + cursorTime);
648            CursorDrawer.DrawCursor(g2d, cursorTime, false);
649            if (this.getTopLevelAncestor() instanceof ScriptBuilderFrame)
650            {
651                 
652                if (((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType != null)
653                {
654                    EventIconDrawer.DrawEventIcon(g2d,
655                            ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType,
656                            x + 5, y + 10);
657                }
658            }
659            if (this.getTopLevelAncestor() instanceof IncidentEditorFrame)
660            {
661                if (((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType != null)
662                {
663                    EventIconDrawer.DrawEventIcon(g2d,
664                            ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType,
665                            x + 5, y + 10);
666                   
667                }else
668                {
669                    //when we aren't selecting one of the incident adding buttons, this.
670                   
671                    //this.selectMode = ((IncidentEditorFrame)this.getTopLevelAncestor()).selectMode;
672                   
673                    if(((IncidentEditorFrame)this.getTopLevelAncestor()).selectMode)
674                    {
675                        //cursorTime dictates where we are in the x direction
676                        //there seems to be no way to tell where we are in the y direction
677                        //implement something?
678                       
679                    }
680                }
681            }
682        }
683    }
684
685    /**
686     * Local main for viewing this panel only.
687     *
688     * @author jdalbey
689     * @param args not used
690     */
691    public static void main(String[] args)
692    {
693        JFrame frame = new JFrame("ScriptBuilderTimelinePanel Demo");
694        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
695
696        IncidentTimelinePanel pnl = new IncidentTimelinePanel();
697
698        // Create a script
699        File inFile = new File("test/scriptbuilder/structures/test_input_file.xml");
700        SimulationScript script = new SimulationScript();
701        script.loadScriptFromFile(inFile);
702        // retrieve a single incident from the script
703        ScriptIncident inci = script.incidents.get(2);
704        // update this panel with an incident
705        pnl.timelinePanelUpdate(inci);
706
707        frame.getContentPane().add(pnl, BorderLayout.CENTER);
708        frame.pack();
709
710        frame.setVisible(true);
711
712    }
713}
Note: See TracBrowser for help on using the repository browser.