| 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 | * |
|---|
| 23 | * @author Greg Eddington <geddingt@calpoly.edu> |
|---|
| 24 | */ |
|---|
| 25 | public class IncidentNumberPanel extends JPanel |
|---|
| 26 | { |
|---|
| 27 | ScriptIncident incident; |
|---|
| 28 | boolean visible; |
|---|
| 29 | public boolean collapsed = false; |
|---|
| 30 | private static Image collapseImage = null, expandImage = null; |
|---|
| 31 | |
|---|
| 32 | private static Image getCollapseImage() |
|---|
| 33 | { |
|---|
| 34 | if (collapseImage == null) |
|---|
| 35 | { |
|---|
| 36 | try |
|---|
| 37 | { |
|---|
| 38 | collapseImage = ImageIO.read(images.Images.getImage("Collapse.png")); |
|---|
| 39 | } |
|---|
| 40 | catch (IOException ex) |
|---|
| 41 | { |
|---|
| 42 | Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | return collapseImage; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | private static Image getExpandImage() |
|---|
| 50 | { |
|---|
| 51 | if (expandImage == null) |
|---|
| 52 | { |
|---|
| 53 | try |
|---|
| 54 | { |
|---|
| 55 | expandImage = ImageIO.read(images.Images.getImage("Expand.png")); |
|---|
| 56 | } |
|---|
| 57 | catch (IOException ex) |
|---|
| 58 | { |
|---|
| 59 | Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | return expandImage; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | private class IncidentNumberMouseListener extends MouseInputAdapter |
|---|
| 67 | { |
|---|
| 68 | private final Shape collapseExpandButtonShape = |
|---|
| 69 | new Rectangle(ScriptBuilderGuiConstants.COLLEX_BUTTON_X, |
|---|
| 70 | ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, |
|---|
| 71 | ScriptBuilderGuiConstants.COLLEX_BUTTON_WIDTH, |
|---|
| 72 | ScriptBuilderGuiConstants.COLLEX_BUTTON_HEIGHT); |
|---|
| 73 | |
|---|
| 74 | @Override |
|---|
| 75 | public void mouseClicked(MouseEvent e) |
|---|
| 76 | { |
|---|
| 77 | if (collapseExpandButtonShape.contains(e.getX(), e.getY())) |
|---|
| 78 | { |
|---|
| 79 | incident.setCollapsed(!collapsed); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public IncidentNumberPanel() |
|---|
| 85 | { |
|---|
| 86 | super(); |
|---|
| 87 | |
|---|
| 88 | // Add the mouse listener |
|---|
| 89 | IncidentNumberMouseListener mouseListener = |
|---|
| 90 | new IncidentNumberMouseListener(); |
|---|
| 91 | addMouseMotionListener(mouseListener); |
|---|
| 92 | addMouseListener(mouseListener); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public void update(ScriptIncident incident) |
|---|
| 96 | { |
|---|
| 97 | this.incident = incident; |
|---|
| 98 | this.visible = incident != null; |
|---|
| 99 | |
|---|
| 100 | if (incident != null) |
|---|
| 101 | this.collapsed = incident.collapsed; |
|---|
| 102 | |
|---|
| 103 | Dimension newSize; |
|---|
| 104 | if (visible) |
|---|
| 105 | { |
|---|
| 106 | if (collapsed) |
|---|
| 107 | { |
|---|
| 108 | newSize = new Dimension( |
|---|
| 109 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, |
|---|
| 110 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT); |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | { |
|---|
| 114 | int mostEvents = 0; |
|---|
| 115 | for (TimeSlice slice : incident.slices) |
|---|
| 116 | { |
|---|
| 117 | if (slice.events.size() > mostEvents) |
|---|
| 118 | mostEvents = slice.events.size(); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | newSize = new Dimension(ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, |
|---|
| 122 | ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT + |
|---|
| 123 | ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * (mostEvents + 1)); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | newSize = new Dimension(0, 0); |
|---|
| 129 | } |
|---|
| 130 | this.setSize(newSize); |
|---|
| 131 | this.setPreferredSize(newSize); |
|---|
| 132 | |
|---|
| 133 | if (incident != null) |
|---|
| 134 | this.setToolTipText(incident.name); |
|---|
| 135 | |
|---|
| 136 | invalidate(); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | @Override |
|---|
| 140 | public void paint(Graphics g) |
|---|
| 141 | { |
|---|
| 142 | super.paint(g); |
|---|
| 143 | |
|---|
| 144 | if (!visible) |
|---|
| 145 | return; |
|---|
| 146 | |
|---|
| 147 | Graphics2D g2d = (Graphics2D)g; |
|---|
| 148 | g2d.setColor(incident.color); |
|---|
| 149 | g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NUMBER_FONT); |
|---|
| 150 | g2d.drawString(Integer.toString(incident.number), |
|---|
| 151 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_LEFT_MARGIN, |
|---|
| 152 | ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN); |
|---|
| 153 | |
|---|
| 154 | g2d.drawImage((collapsed ? getExpandImage() : getCollapseImage()), |
|---|
| 155 | ScriptBuilderGuiConstants.COLLEX_BUTTON_X, |
|---|
| 156 | ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, null); |
|---|
| 157 | |
|---|
| 158 | if (!collapsed) |
|---|
| 159 | { |
|---|
| 160 | int y = ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN * 2 - 14; |
|---|
| 161 | g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NAME_FONT); |
|---|
| 162 | for (String line : incident.name.split(" ")) |
|---|
| 163 | { |
|---|
| 164 | g2d.drawString(line, 20, y); |
|---|
| 165 | y += 12; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | } |
|---|