Changeset 86 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/gui
- Timestamp:
- 08/27/2017 02:58:02 PM (9 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/scriptbuilder/gui/panels/OverviewWinSpike.java
r76 r86 2 2 3 3 import java.awt.BorderLayout; 4 import java.awt.Dimension;5 import java.awt.Graphics;6 import java.awt.Graphics2D;7 import java.awt.event.MouseEvent;8 4 import java.io.File; 5 import javax.swing.BoxLayout; 9 6 import javax.swing.JFrame; 10 7 import javax.swing.JPanel; 11 import javax.swing.event.MouseInputAdapter; 12 import static junit.framework.Assert.assertEquals; 13 import scriptbuilder.gui.IncidentEditorFrame; 14 import scriptbuilder.gui.ScriptBuilderFrame; 15 import scriptbuilder.gui.ScriptBuilderGuiConstants; 16 import scriptbuilder.gui.drawers.EventIconDrawer; 8 import scriptbuilder.gui.drawers.RangeSlider; 17 9 import scriptbuilder.structures.ScriptIncident; 18 10 import scriptbuilder.structures.SimulationScript; 19 11 20 12 /** 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 13 * Exploration (spike) for Overview Window. 14 * The background is a TimelineTickPanel. 15 * IncidentTimelinePanel objects sit over it. 16 * @author jdalbey 27 17 */ 28 public class TimelineTickPanel extends JPanel18 public class OverviewWinSpike 29 19 { 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, or41 * moves around on the panel.42 */43 public class TimelineTickMouseListener extends MouseInputAdapter44 {45 46 /**47 * When the mouse enters the panel, the panel gets focus.48 *49 * @param e the mouse event50 */51 @Override52 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 event61 */62 @Override63 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 and71 * updates its mouse tracker.72 *73 * @param e74 */75 @Override76 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 mouseListener93 = new TimelineTickMouseListener();94 addMouseMotionListener(mouseListener);95 addMouseListener(mouseListener);96 }97 98 /**99 * Update the panel's dimensions based on number of events, zoom level, and100 * which events are collapsed. The version of the method is used in the main101 * script builder.102 *103 * @param script The main script model104 */105 public void update(SimulationScript script)106 {107 longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;108 109 // Get the stats on the incidents110 int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;111 for (ScriptIncident incident : script.incidents)112 {113 if (incident != null)114 {115 height += incident.collapsed116 ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT117 : 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(longestLength126 / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION127 * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK128 + 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, and138 * which events are collapsed. This version of the method is used in the139 * individual incident editor.140 *141 * @param incident the incident being edited142 */143 public void update(ScriptIncident incident)144 {145 longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH;146 147 // Get the stats on the incidents148 int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4;149 150 if (incident != null)151 {152 height += incident.collapsed153 ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT154 : 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(longestLength163 / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION164 * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK165 + 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, panel175 * dimensions, and offset. If the user is trying to add an event, draw that176 * event's icon under the mouse.177 *178 * @param g The graphics component179 */180 @Override181 public void paint(Graphics g)182 {183 super.paint(g);184 185 Graphics2D g2d = (Graphics2D) g;186 187 // Draw the horizontal line188 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 ticks194 int longestLengthPlusMargin = longestLength195 / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION196 * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK197 + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN;198 199 // Minutes200 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 Ticks211 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_TICK216 * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION217 * 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 (focused229 && ((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 (focused239 && ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType != null)240 {241 EventIconDrawer.DrawEventIcon(g2d,242 ((IncidentEditorFrame) this.getTopLevelAncestor()).currentEventType,243 x, y);244 }245 }246 }247 248 20 /** 249 21 * Local main for viewing this panel only. … … 254 26 public static void main(String[] args) 255 27 { 256 JFrame frame = new JFrame(" FrameDemo");28 JFrame frame = new JFrame("Overview Window Spike"); 257 29 258 30 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 259 31 260 32 TimelineTickPanel pnl = new TimelineTickPanel(); 33 pnl.setLayout(new BoxLayout(pnl, BoxLayout.PAGE_AXIS)); 261 34 // Create a script 262 35 File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); 263 SimulationScript instance = new SimulationScript(); 264 instance.loadScriptFromFile(inFile); 36 SimulationScript script = new SimulationScript(); 37 script.loadScriptFromFile(inFile); 38 ScriptIncident incident = script.incidents.get(2); 39 IncidentNumberPanel inciNum = new IncidentNumberPanel(); 40 inciNum.update(incident); 41 JPanel row1 = new JPanel(); 42 row1.setOpaque(false); // so tick lines will show through 43 //row1.setSize(200, 500); 44 row1.add(inciNum); 45 RangeSlider slider = new RangeSlider(0,25); 46 slider.setValue(5); 47 slider.setUpperValue(10); 48 row1.add(slider); 49 pnl.add(row1); 265 50 // update this panel with the script 266 pnl.update( instance);51 pnl.update(script); 267 52 frame.getContentPane().add(pnl, BorderLayout.CENTER); 268 frame.setSize(300, 500);269 53 frame.pack(); 270 54
Note: See TracChangeset
for help on using the changeset viewer.
