package scriptbuilder.gui.panels; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Rectangle; import java.awt.Shape; import java.awt.event.MouseEvent; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JPanel; import javax.swing.event.MouseInputAdapter; import scriptbuilder.gui.ScriptBuilderFrame; import scriptbuilder.gui.ScriptBuilderGuiConstants; import scriptbuilder.structures.ScriptIncident; import scriptbuilder.structures.TimeSlice; /** * Displays the Incident ID number and name. Holds the button to toggle * collapsed state. * * @author Greg Eddington * @author Bryan McGuffin */ public class IncidentNumberPanel extends JPanel { ScriptIncident incident; boolean visible; public boolean collapsed = false; private static Image collapseImage = null, expandImage = null; /** * Get the icon to use for the "collapse" button. * * @return Collapse.png */ private static Image getCollapseImage() { if (collapseImage == null) { try { collapseImage = ImageIO.read(images.Images.getImage("Collapse.png")); } catch (IOException ex) { Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); } } return collapseImage; } /** * Get the icon to use for the "expand" button. * * @return Expand.png */ private static Image getExpandImage() { if (expandImage == null) { try { expandImage = ImageIO.read(images.Images.getImage("Expand.png")); } catch (IOException ex) { Logger.getLogger(IncidentNumberPanel.class.getName()).log(Level.SEVERE, null, ex); } } return expandImage; } /** * Listener for the mouse. Receives notifications if the mouse is clicked * inside the button. */ private class IncidentNumberMouseListener extends MouseInputAdapter { private final Shape collapseExpandButtonShape = new Rectangle(ScriptBuilderGuiConstants.COLLEX_BUTTON_X, ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, ScriptBuilderGuiConstants.COLLEX_BUTTON_WIDTH, ScriptBuilderGuiConstants.COLLEX_BUTTON_HEIGHT); /** * If the click occurs inside the icon borders, toggle the collapsed * state. * * @param e the mouse event */ @Override public void mouseClicked(MouseEvent e) { if (collapseExpandButtonShape.contains(e.getX(), e.getY())) { incident.setCollapsed(!collapsed); } } } /** * Constructor. Sets up the mouse listener. */ public IncidentNumberPanel() { super(); // Add the mouse listener IncidentNumberMouseListener mouseListener = new IncidentNumberMouseListener(); addMouseMotionListener(mouseListener); addMouseListener(mouseListener); } /** * Update the dimensions of the panel. * * @param incident The incident to be represented by this panel. */ public void update(ScriptIncident incident) { this.incident = incident; this.visible = incident != null; if (incident != null) { this.collapsed = incident.collapsed; } Dimension newSize; if (visible) { if (collapsed) { newSize = new Dimension( ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT); } else { int mostEvents = 0; for (TimeSlice slice : incident.getSlices()) { if (slice.events.size() > mostEvents) { mostEvents = slice.events.size(); } } newSize = new Dimension(ScriptBuilderGuiConstants.INCIDENT_NUMBER_WIDTH, ScriptBuilderGuiConstants.TIMELINE_COLLAPSED_HEIGHT + ScriptBuilderGuiConstants.SCRIPT_EVENT_ICON_STEP * (mostEvents + 1)); } } else { newSize = new Dimension(0, 0); } this.setSize(newSize); this.setPreferredSize(newSize); if (incident != null) { this.setToolTipText(incident.name); } invalidate(); } /** * Refresh the panel. Choose button icon depending on collapsed state. * * @param g the graphics component */ @Override public void paint(Graphics g) { super.paint(g); if (!visible) { return; } Graphics2D g2d = (Graphics2D) g; g2d.setColor(incident.color); g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NUMBER_FONT); g2d.drawString(Integer.toString(incident.number), ScriptBuilderGuiConstants.INCIDENT_NUMBER_LEFT_MARGIN, ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN); g2d.drawImage((collapsed ? getExpandImage() : getCollapseImage()), ScriptBuilderGuiConstants.COLLEX_BUTTON_X, ScriptBuilderGuiConstants.COLLEX_BUTTON_Y, null); if (!collapsed) { int y = ScriptBuilderGuiConstants.INCIDENT_NUMBER_TOP_MARGIN * 2 - 14; g2d.setFont(ScriptBuilderGuiConstants.INCIDENT_NAME_FONT); for (String line : incident.name.split(" ")) { g2d.drawString(line, 20, y); y += 12; } } } }