* This class is also a MouseListener which listens to MOUSE_ENTERED * and MOUSE_EXITED events from Action derived components so that * the value of the Action.LONG_DESCRIPTION key is sent as a message * to the status bar. *
* To enable this behavior, add the StatusBar * instance as a MouseListener to the component that was created from * an Action. * * @version 1.6 01/15/03 * @author Mark Davidson */ public class StatusBar extends JPanel implements MouseListener { private JLabel label; private Dimension preferredSize; private static StatusBar INSTANCE; public StatusBar() { this.setLayout(new FlowLayout(FlowLayout.LEFT)); this.setBorder(BorderFactory.createEtchedBorder()); // Set a large blank label to set the preferred size. label = new JLabel(" "); preferredSize = new Dimension(getWidth(label.getText()), 2 * getFontHeight()); this.add(label); } /** * Return the instance of the StatusBar. If this has not been explicity * set then it will be created. * * @return the StatusBar instance. * @see #setInstance */ public static StatusBar getInstance() { if (INSTANCE == null) { INSTANCE = new StatusBar(); } return INSTANCE; } /** * Sets the StatusBar instance. */ public static void setInstance(StatusBar status) { INSTANCE = status; } /** * Returns the string width * @param s the string * @return the string width */ protected int getWidth(String s) { FontMetrics fm = this.getFontMetrics(this.getFont()); if (fm == null) { return 0; } return fm.stringWidth(s); } /** * Returns the height of a line of text * @return the height of a line of text */ protected int getFontHeight() { FontMetrics fm = this.getFontMetrics(this.getFont()); if (fm == null) { return 0; } return fm.getHeight(); } /** * Returns the perferred size * @return the preferred size */ public Dimension getPreferredSize() { return preferredSize; } /** * Sets non-transient status bar message * @param message the message to display on the status bar */ public void setMessage(String message) { label.setText(message); } // // MouseListener methods // public void mouseClicked(MouseEvent evt) {} public void mousePressed(MouseEvent evt) {} public void mouseReleased(MouseEvent evt) {} public void mouseExited(MouseEvent evt) { setMessage(""); } /** * Takes the LONG_DESCRIPTION of the Action based components * and sends them to the Status bar */ public void mouseEntered(MouseEvent evt) { if (evt.getSource() instanceof AbstractButton) { AbstractButton button = (AbstractButton)evt.getSource(); Action action = button.getAction(); if (action != null) { String message = (String)action.getValue(Action.LONG_DESCRIPTION); setMessage(message); } } } /** * Helper method to recursively register all MenuElements with * a mouse listener. */ public void registerMouseListener(MenuElement[] elements) { for (int i = 0; i < elements.length; i++) { if (elements[i] instanceof JMenuItem) { ((JMenuItem)elements[i]).addMouseListener(this); } registerMouseListener(elements[i].getSubElements()); } } /** * Helper method to register all components with a mouse listener. */ public void registerMouseListener(Component[] components) { for (int i = 0; i < components.length; i++) { if (components[i] instanceof AbstractButton) { ((AbstractButton)components[i]).addMouseListener(this); } } } } // end class StatusBar