| 1 | package scriptbuilder.gui.panels; |
|---|
| 2 | |
|---|
| 3 | import java.awt.Dimension; |
|---|
| 4 | import java.awt.Graphics; |
|---|
| 5 | import java.awt.Graphics2D; |
|---|
| 6 | import java.awt.Image; |
|---|
| 7 | import java.awt.Rectangle; |
|---|
| 8 | import java.awt.Shape; |
|---|
| 9 | import java.awt.event.MouseEvent; |
|---|
| 10 | import java.io.IOException; |
|---|
| 11 | import java.util.logging.Level; |
|---|
| 12 | import java.util.logging.Logger; |
|---|
| 13 | import javax.imageio.ImageIO; |
|---|
| 14 | import javax.swing.JPanel; |
|---|
| 15 | import javax.swing.event.MouseInputAdapter; |
|---|
| 16 | import scriptbuilder.gui.ScriptBuilderFrame; |
|---|
| 17 | import scriptbuilder.gui.ScriptBuilderGuiConstants; |
|---|
| 18 | import scriptbuilder.structures.ScriptIncident; |
|---|
| 19 | import scriptbuilder.structures.TimeSlice; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * Displays the Incident ID number and name. Holds the button to toggle |
|---|
| 23 | * collapsed state. |
|---|
| 24 | * |
|---|
| 25 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 26 | * @author Bryan McGuffin |
|---|
| 27 | */ |
|---|
| 28 | public class IncidentNumberPanel extends JPanel |
|---|
| 29 | { |
|---|
| 30 | |
|---|
| 31 | ScriptIncident incident; |
|---|
| 32 | boolean visible; |
|---|
| 33 | public boolean collapsed = false; |
|---|
| 34 | private static Image collapseImage = null, expandImage = null; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Get the icon to use for the "collapse" button. |
|---|
| 38 | * |
|---|
| 39 | * @return Collapse.png |
|---|
| 40 | */ |
|---|
| 41 | private static Image getCollapseImage() |
|---|
| 42 | { |
|---|
| 43 | if (collapseImage == null) |
|---|
| 44 | { |
|---|
| 45 | try |
|---|
| 46 | { |
|---|
| 47 | collapseImage = ImageIO.read(images.Images.getImage("Collapse.png")); |
|---|
| 48 | } |
|---|
| 49 | catch (IOException ex) |
|---|
| 50 | { |
|---|
| 51 | Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | return collapseImage; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * Get the icon to use for the "expand" button. |
|---|
| 60 | * |
|---|
| 61 | * @return Expand.png |
|---|
| 62 | */ |
|---|
| 63 | private static Image getExpandImage() |
|---|
| 64 | { |
|---|
| 65 | if (expandImage == null) |
|---|
| 66 | { |
|---|
| 67 | try |
|---|
| 68 | { |
|---|
| 69 | expandImage = ImageIO.read(images.Images.getImage("Expand.png")); |
|---|
| 70 | } |
|---|
| 71 | catch (IOException ex) |
|---|
| 72 | { |
|---|
| 73 | Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | return expandImage; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| 81 | * Listener for the mouse. Receives notifications if the mouse is clicked |
|---|
| 82 | * inside the button. |
|---|
| 83 | */ |
|---|
| 84 | private class IncidentNumberMouseListener extends MouseInputAdapter |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | private final Shape collapseExpandButtonShape |
|---|
| 88 | = new Rectangle(ScriptBuilderGuiConstants.COLLEX_BUTTON_X, |
|---|
| 89 | ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, |
|---|
| 90 | ScriptBuilderGuiConstants.COLLEX_BUTTON_WIDTH, |
|---|
| 91 | ScriptBuilderGuiConstants.COLLEX_BUTTON_HEIGHT); |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 94 | * If the click occurs inside the icon borders, toggle the collapsed |
|---|
| 95 | * state. |
|---|
| 96 | * |
|---|
| 97 | * @param e the mouse event |
|---|
| 98 | */ |
|---|
| 99 | @Override |
|---|
| 100 | public void mouseClicked(MouseEvent e) |
|---|
| 101 | { |
|---|
| 102 | if (collapseExpandButtonShape.contains(e.getX(), e.getY())) |
|---|
| 103 | { |
|---|
| 104 | incident.setCollapsed(!collapsed); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Constructor. Sets up the mouse listener. |
|---|
| 111 | */ |
|---|
| 112 | public IncidentNumberPanel() |
|---|
| 113 | { |
|---|
| 114 | super(); |
|---|
| 115 | |
|---|
| 116 | // Add the mouse listener |
|---|
| 117 | IncidentNumberMouseListener mouseListener |
|---|
| 118 | = new IncidentNumberMouseListener(); |
|---|
| 119 | addMouseMotionListener(mouseListener); |
|---|
| 120 | addMouseListener(mouseListener); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Update the dimensions of the panel. |
|---|
| 125 | * |
|---|
| 126 | * @param incident The incident to be represented by this panel. |
|---|
| 127 | */ |
|---|
| 128 | public void update(ScriptIncident incident) |
|---|
| 129 | { |
|---|
| 130 | this.incident = incident; |
|---|
| 131 | this.visible = incident != null; |
|---|
| 132 | |
|---|
| 133 | if (incident != null) |
|---|
| 134 | { |
|---|
| 135 | this.collapsed = incident.collapsed; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | Dimension newSize; |
|---|
| 139 | if (visible) |
|---|
| 140 | { |
|---|
| 141 | if (collapsed) |
|---|
| 142 | { |
|---|
| 143 | newSize = new Dimension( |
|---|
| 144 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, |
|---|
| 145 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT); |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | int mostEvents = 0; |
|---|
| 150 | for (TimeSlice slice : incident.getSlices()) |
|---|
| 151 | { |
|---|
| 152 | if (slice.events.size() > mostEvents) |
|---|
| 153 | { |
|---|
| 154 | mostEvents = slice.events.size(); |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | newSize = new Dimension(ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, |
|---|
| 159 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT |
|---|
| 160 | + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * (mostEvents + 1)); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | else |
|---|
| 164 | { |
|---|
| 165 | newSize = new Dimension(0, 0); |
|---|
| 166 | } |
|---|
| 167 | this.setSize(newSize); |
|---|
| 168 | this.setPreferredSize(newSize); |
|---|
| 169 | |
|---|
| 170 | if (incident != null) |
|---|
| 171 | { |
|---|
| 172 | this.setToolTipText(incident.name); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | invalidate(); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * Refresh the panel. Choose button icon depending on collapsed state. |
|---|
| 180 | * |
|---|
| 181 | * @param g the graphics component |
|---|
| 182 | */ |
|---|
| 183 | @Override |
|---|
| 184 | public void paint(Graphics g) |
|---|
| 185 | { |
|---|
| 186 | super.paint(g); |
|---|
| 187 | |
|---|
| 188 | if (!visible) |
|---|
| 189 | { |
|---|
| 190 | return; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | Graphics2D g2d = (Graphics2D) g; |
|---|
| 194 | g2d.setColor(incident.color); |
|---|
| 195 | g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NUMBER_FONT); |
|---|
| 196 | g2d.drawString(Integer.toString(incident.number), |
|---|
| 197 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_LEFT_MARGIN, |
|---|
| 198 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN); |
|---|
| 199 | |
|---|
| 200 | g2d.drawImage((collapsed ? getExpandImage() : getCollapseImage()), |
|---|
| 201 | ScriptBuilderGuiConstants.COLLEX_BUTTON_X, |
|---|
| 202 | ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, null); |
|---|
| 203 | |
|---|
| 204 | if (!collapsed) |
|---|
| 205 | { |
|---|
| 206 | int y = ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN * 2 - 14; |
|---|
| 207 | g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NAME_FONT); |
|---|
| 208 | for (String line : incident.name.split(" ")) |
|---|
| 209 | { |
|---|
| 210 | g2d.drawString(line, 20, y); |
|---|
| 211 | y += 12; |
|---|
| 212 | } |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|