source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/gui/panels/TimelineTickPanel.java @ 76

Revision 76, 9.0 KB checked in by bmcguffin, 9 years ago (diff)

Added javadoc for several files.

Line 
1package scriptbuilder.gui.panels;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.Graphics;
6import java.awt.Graphics2D;
7import java.awt.event.MouseEvent;
8import java.io.File;
9import javax.swing.JFrame;
10import javax.swing.JPanel;
11import javax.swing.event.MouseInputAdapter;
12import static junit.framework.Assert.assertEquals;
13import scriptbuilder.gui.IncidentEditorFrame;
14import scriptbuilder.gui.ScriptBuilderFrame;
15import scriptbuilder.gui.ScriptBuilderGuiConstants;
16import scriptbuilder.gui.drawers.EventIconDrawer;
17import scriptbuilder.structures.ScriptIncident;
18import scriptbuilder.structures.SimulationScript;
19
20/**
21 * Represents the underlying panel on the main timeline. All the
22 * IncidentTimelinePanel objects sit over it. This panel draws all the
23 * per-minute ticks on the timeline, and adjusts and scales them as necessary.
24 *
25 * @author Greg Eddington <geddingt@calpoly.edu>
26 * @author Bryan McGuffin
27 */
28public class TimelineTickPanel extends JPanel
29{
30
31    private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;
32    private int x, y;
33    private boolean focused = false;
34
35//    public void setZoom(float zoom)
36//    {
37//        repaint();
38//    }
39    /**
40     * Listener for the mouse. Is notified when the mouse enters, exits, or
41     * moves around on the panel.
42     */
43    public class TimelineTickMouseListener extends MouseInputAdapter
44    {
45
46        /**
47         * When the mouse enters the panel, the panel gets focus.
48         *
49         * @param e the mouse event
50         */
51        @Override
52        public void mouseEntered(MouseEvent e)
53        {
54            focused = true;
55        }
56
57        /**
58         * When the mouse leaves the panel, the panel loses focus and refreshes.
59         *
60         * @param e the mouse event
61         */
62        @Override
63        public void mouseExited(MouseEvent e)
64        {
65            focused = false;
66            repaint();
67        }
68
69        /**
70         * When the mouse moves around in the panel, the panel refreshes and
71         * updates its mouse tracker.
72         *
73         * @param e
74         */
75        @Override
76        public void mouseMoved(MouseEvent e)
77        {
78            x = e.getX();
79            y = e.getY();
80
81            repaint();
82        }
83    }
84
85    /**
86     * Constructor. Set up the mouse listener.
87     */
88    public TimelineTickPanel()
89    {
90        super();
91
92        TimelineTickMouseListener mouseListener
93                = new TimelineTickMouseListener();
94        addMouseMotionListener(mouseListener);
95        addMouseListener(mouseListener);
96    }
97
98    /**
99     * Update the panel's dimensions based on number of events, zoom level, and
100     * which events are collapsed. The version of the method is used in the main
101     * script builder.
102     *
103     * @param script The main script model
104     */
105    public void update(SimulationScript script)
106    {
107        longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;
108
109        // Get the stats on the incidents
110        int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;
111        for (ScriptIncident incident : script.incidents)
112        {
113            if (incident != null)
114            {
115                height += incident.collapsed
116                        ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT
117                        : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT;
118                if ((incident.length + incident.offset) > longestLength)
119                {
120                    longestLength = incident.length + incident.offset;
121                }
122            }
123        }
124
125        Dimension newSize = new Dimension(longestLength
126                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
127                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
128                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50,
129                height);
130        this.setPreferredSize(newSize);
131        this.setSize(newSize);
132
133        this.invalidate();
134    }
135
136    /**
137     * Update the panel's dimensions based on number of events, zoom level, and
138     * which events are collapsed. This version of the method is used in the
139     * individual incident editor.
140     *
141     * @param incident the incident being edited
142     */
143    public void update(ScriptIncident incident)
144    {
145        longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;
146
147        // Get the stats on the incidents
148        int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;
149
150        if (incident != null)
151        {
152            height += incident.collapsed
153                    ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT
154                    : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT;
155            if ((incident.length + incident.offset) > longestLength)
156            {
157                longestLength = incident.length + incident.offset;
158            }
159
160        }
161
162        Dimension newSize = new Dimension(longestLength
163                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
164                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
165                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50,
166                height);
167        this.setPreferredSize(newSize);
168        this.setSize(newSize);
169
170        this.invalidate();
171    }
172
173    /**
174     * Refresh the panel. Redraw the ticks based on zoom level, panel
175     * dimensions, and offset. If the user is trying to add an event, draw that
176     * event's icon under the mouse.
177     *
178     * @param g The graphics component
179     */
180    @Override
181    public void paint(Graphics g)
182    {
183        super.paint(g);
184
185        Graphics2D g2d = (Graphics2D) g;
186
187        // Draw the horizontal line
188        g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT);
189        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
190        g2d.fillRect(0, ScriptBuilderGuiConstants.TICK_TIMELINE_TOP_MARGIN,
191                longestLength, ScriptBuilderGuiConstants.TICK_TIMELINE_HEIGHT);
192
193        // Draw the ticks
194        int longestLengthPlusMargin = longestLength
195                / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
196                * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
197                + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
198
199        // Minutes
200        g2d.setColor(ScriptBuilderGuiConstants.MINOR_TICK_COLOR);
201        int seconds = 0;
202        for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
203                i <= longestLengthPlusMargin;
204                i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION)
205        {
206            g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN,
207                    i, ScriptBuilderGuiConstants.TICK_HEIGHT);
208        }
209
210        // Major Ticks
211        g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR);
212        seconds = 0;
213        for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;
214                i <= longestLengthPlusMargin;
215                i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK
216                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION
217                * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK)
218        {
219            g2d.drawLine(i, ScriptBuilderGuiConstants.TICK_TOP_MARGIN,
220                    i, ScriptBuilderGuiConstants.TICK_HEIGHT);
221
222        }
223
224        paintChildren(g);
225
226        if (this.getTopLevelAncestor() instanceof ScriptBuilderFrame)
227        {
228            if (focused
229                    && ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType != null)
230            {
231                EventIconDrawer.DrawEventIcon(g2d,
232                        ((ScriptBuilderFrame) this.getTopLevelAncestor()).currentEventType,
233                        x, y);
234            }
235        }
236        if (this.getTopLevelAncestor() instanceof IncidentEditorFrame)
237        {
238            if (focused
239                    && ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType != null)
240            {
241                EventIconDrawer.DrawEventIcon(g2d,
242                        ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType,
243                        x, y);
244            }
245        }
246    }
247
248    /**
249     * Local main for viewing this panel only.
250     *
251     * @author jdalbey
252     * @param args not used
253     */
254    public static void main(String[] args)
255    {
256        JFrame frame = new JFrame("FrameDemo");
257
258        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
259
260        TimelineTickPanel pnl = new TimelineTickPanel();
261        // Create a script
262        File inFile = new File("test/scriptbuilder/structures/test_input_file.xml");
263        SimulationScript instance = new SimulationScript();
264        instance.loadScriptFromFile(inFile);
265        // update this panel with the script
266        pnl.update(instance);
267        frame.getContentPane().add(pnl, BorderLayout.CENTER);
268        frame.setSize(300, 500);
269        frame.pack();
270
271        frame.setVisible(true);
272
273    }
274}
Note: See TracBrowser for help on using the repository browser.