| 1 | package scriptbuilder.gui.panels; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.awt.Graphics; |
|---|
| 5 | import java.awt.Graphics2D; |
|---|
| 6 | import javax.swing.JPanel; |
|---|
| 7 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 8 | import scriptbuilder.structures.ScriptIncident; |
|---|
| 9 | import scriptbuilder.structures.SimulationScript; |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Represents the timestamp bar at the top of the GUI timeline. Displays |
|---|
| 13 | * timestamps at significant intervals. Updates based on zoom level and scroll |
|---|
| 14 | * position. |
|---|
| 15 | * |
|---|
| 16 | * @author Greg Eddington |
|---|
| 17 | * @author Bryan McGuffin |
|---|
| 18 | */ |
|---|
| 19 | public class TimeStampPanel extends JPanel |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | private int longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Just refresh the panel. |
|---|
| 26 | * |
|---|
| 27 | * @param zoom UNUSED |
|---|
| 28 | */ |
|---|
| 29 | public void setZoom(float zoom) |
|---|
| 30 | { |
|---|
| 31 | repaint(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Constructor. |
|---|
| 36 | */ |
|---|
| 37 | public TimeStampPanel() |
|---|
| 38 | { |
|---|
| 39 | super(); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Update the length interval and the dimensions of the panel. NOTE: This |
|---|
| 44 | * method implementation is an exact duplication of the update method in |
|---|
| 45 | * panels.TimelineTickPanel. I'm not sure if it actually accomplishes |
|---|
| 46 | * anything here. |
|---|
| 47 | * |
|---|
| 48 | * @param script The simulation script model |
|---|
| 49 | */ |
|---|
| 50 | public void update(SimulationScript script) |
|---|
| 51 | { |
|---|
| 52 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 53 | |
|---|
| 54 | // Get the stats on the incidents |
|---|
| 55 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 56 | for (ScriptIncident incident : script.incidents) |
|---|
| 57 | { |
|---|
| 58 | if (incident != null) |
|---|
| 59 | { |
|---|
| 60 | height += incident.collapsed |
|---|
| 61 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 62 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 63 | if ((incident.length + incident.offset) > longestLength) |
|---|
| 64 | { |
|---|
| 65 | longestLength = incident.length + incident.offset; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | for (ScriptIncident i : script.incidents) |
|---|
| 70 | { |
|---|
| 71 | if (i != null && i.number == 100) |
|---|
| 72 | { |
|---|
| 73 | i.length = longestLength; |
|---|
| 74 | i.offset = 0; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | Dimension newSize = new Dimension(longestLength |
|---|
| 80 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 81 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 82 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 83 | height); |
|---|
| 84 | this.setPreferredSize(newSize); |
|---|
| 85 | this.setSize(newSize); |
|---|
| 86 | |
|---|
| 87 | this.invalidate(); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Update the length interval and the dimensions of the panel. NOTE: This |
|---|
| 92 | * method implementation is an exact duplication of the update method in |
|---|
| 93 | * panels.TimelineTickPanel. I'm not sure if it actually accomplishes |
|---|
| 94 | * anything here. |
|---|
| 95 | * |
|---|
| 96 | * @param script The simulation script model |
|---|
| 97 | */ |
|---|
| 98 | public void update(ScriptIncident incident) |
|---|
| 99 | { |
|---|
| 100 | longestLength = ScriptBuilderGuiConstants.TICK_TIMELINE_SMALLEST_LENGTH; |
|---|
| 101 | |
|---|
| 102 | // Get the stats on the incidents |
|---|
| 103 | int height = ScriptBuilderGuiConstants.TICK_TOP_MARGIN * 4; |
|---|
| 104 | if (incident != null) |
|---|
| 105 | { |
|---|
| 106 | height += incident.collapsed |
|---|
| 107 | ? ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT |
|---|
| 108 | : ScriptBuilderGuiConstants.TIMELINE_OPENED_HEIGHT; |
|---|
| 109 | if ((incident.length + incident.offset) > longestLength) |
|---|
| 110 | { |
|---|
| 111 | longestLength = incident.length + incident.offset; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if (incident != null && incident.number == 100) |
|---|
| 116 | { |
|---|
| 117 | incident.length = longestLength; |
|---|
| 118 | incident.offset = 0; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | Dimension newSize = new Dimension(longestLength |
|---|
| 122 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 123 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 124 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN + 50, |
|---|
| 125 | height); |
|---|
| 126 | this.setPreferredSize(newSize); |
|---|
| 127 | this.setSize(newSize); |
|---|
| 128 | |
|---|
| 129 | this.invalidate(); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Refresh the panel. Draw the timestamps for the appropriate intervals |
|---|
| 134 | * based on zoom level. |
|---|
| 135 | * |
|---|
| 136 | * @param g The graphics component |
|---|
| 137 | */ |
|---|
| 138 | @Override |
|---|
| 139 | public void paint(Graphics g) |
|---|
| 140 | { |
|---|
| 141 | super.paint(g); |
|---|
| 142 | |
|---|
| 143 | Graphics2D g2d = (Graphics2D) g; |
|---|
| 144 | g2d.setFont(ScriptBuilderGuiConstants.TIMELINE_TICK_TIME_FONT); |
|---|
| 145 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 146 | int longestLengthPlusMargin = longestLength |
|---|
| 147 | / ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 148 | * ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 149 | + ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 150 | int seconds = 0; |
|---|
| 151 | |
|---|
| 152 | // Major Ticks |
|---|
| 153 | g2d.setColor(ScriptBuilderGuiConstants.TIMELINE_TICK_COLOR); |
|---|
| 154 | seconds = 0; |
|---|
| 155 | for (int i = ScriptBuilderGuiConstants.TICK_TIMELINE_LEFT_MARGIN; |
|---|
| 156 | i <= longestLengthPlusMargin; |
|---|
| 157 | i += ScriptBuilderGuiConstants.PIXEL_WIDTH_PER_HORIZONTAL_TICK |
|---|
| 158 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK, seconds += ScriptBuilderGuiConstants.HORIZONTAL_TICK_RESOLUTION |
|---|
| 159 | * ScriptBuilderGuiConstants.TICKS_PER_MAJOR_TICK) |
|---|
| 160 | { |
|---|
| 161 | g2d.drawString(seconds / 3600 + ":" |
|---|
| 162 | + (seconds / 60 % 60 > 9 ? seconds / 60 % 60 |
|---|
| 163 | : "0" + seconds / 60 % 60) |
|---|
| 164 | + ":00", i - 25, 18); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | } |
|---|