home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 15.8 KB | 540 lines |
- package symantec.itools.awt;
-
-
- import java.awt.*;
- import java.awt.event.*;
- import java.beans.PropertyVetoException;
- import java.beans.PropertyChangeListener;
- import java.beans.VetoableChangeListener;
- import java.beans.PropertyChangeEvent;
- import symantec.itools.beans.VetoableChangeSupport;
- import symantec.itools.beans.PropertyChangeSupport;
-
- // 06/01/97 RKM Updated to support Java 1.1
- // Made properties bound & constrained
- // Converted events
- // Removed internal enabled (it's in component)
- // Removed enable & disable overrides, no one else calls repaint
- // Changed invalidates to repaint
- // 07/25/97 CAR marked fields transient as needed
- // innerclasses implement java.io.Serializable
- // changed visibility of fields "pressed", "released", "state" and "type" from private to package
-
- /**
- * A two- or three-state checkbox that doesn't have an associated text label.
- * A two-state checkbox behaves just like the standard checkbox. In three-state
- * mode clicking the checkbox can switch the checkbox to a third state that
- * appears both gray and checked.
- * <p>
- * The third state is often used to indicate that an attribute of the current
- * selection varies across the selected items. For example, a checkbox used to
- * indicate "bold text" might use the third state if the currently selected
- * text included both bold and plain characters.
- * <p>
- * It is also used to indicate a default action or state.
- * <p>
- * @version 1.0, Nov 26, 1996
- *
- * @author Symantec
- *
- */
- public class StateCheckBox
- extends Canvas
- {
- //--------------------------------------------------
- // constants
- //--------------------------------------------------
-
- private static final int WIDTH = 14;
- private static final int HEIGHT = 14;
- /**
- * A style constant indicating that this is a two-state checkbox.
- */
- public static final int TWO_STATE = 0;
- /**
- * A style constant indicating that this is a three-state checkbox.
- */
- public static final int THREE_STATE = 1;
- /**
- * A state constant indicating that the current state is "unchecked".
- */
- public static final int STATE_UNCHECKED = 0;
- /**
- * A state constant indicating that the current state is "checked".
- */
- public static final int STATE_CHECKED = 1;
- /**
- * A state constant indicating that the current state is "default".
- * Default is the third state of a three-state checkbox.
- */
- public static final int STATE_DEFAULT = 2;
-
- //
- // Constructors
- //
-
- /**
- * Constructs a two-state StateCheckBox that is currently unchecked.
- */
- public StateCheckBox()
- {
- this.type = TWO_STATE;
- this.state = STATE_UNCHECKED;
-
- width = WIDTH;
- height = HEIGHT;
-
- state = 0;
-
- pressed = false;
- released = true;
- }
-
-
- //--------------------------------------------------
- // accessor methods
- //--------------------------------------------------
-
- /**
- * Sets the current style to two-state or three-state.
- * @param newStyle new StateCheckBox state; either TWO_STATE or THREE_STATE
- * @see #getStyle
- * @see #TWO_STATE
- * @see #THREE_STATE
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setStyle(int newStyle)
- throws PropertyVetoException
- {
- if (type != newStyle)
- {
- Integer oldStyleInt = new Integer(type);
- Integer newStyleInt = new Integer(newStyle);
-
- vetos.fireVetoableChange("Style",oldStyleInt,newStyleInt);
-
- type = newStyle;
-
- changes.firePropertyChange("Style",oldStyleInt,newStyleInt);
-
- repaint();
- }
- }
-
- /**
- * Gets the current style: two-state or three-state.
- * @return the current StateCheckBox style; either TWO_STATE or THREE_STATE
- * @see #setStyle
- * @see #TWO_STATE
- * @see #THREE_STATE
- */
- public int getStyle()
- {
- return type;
- }
-
- /**
- * Sets the current checkbox state to unchecked, checked, or default.
- * @param newState the new state: STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
- * @see #getState
- * @see #STATE_UNCHECKED
- * @see #STATE_CHECKED
- * @see #STATE_DEFAULT
- * @exception PropertyVetoException
- * if the specified property value is unacceptable
- */
- public void setState(int newState)
- throws PropertyVetoException
- {
- if (state != newState)
- {
- Integer oldStateInt = new Integer(state);
- Integer newStateInt = new Integer(newState);
-
- vetos.fireVetoableChange("State",oldStateInt,newStateInt);
-
- state = newState;
-
- changes.firePropertyChange("State",oldStateInt,newStateInt);
-
- repaint();
- }
- }
-
- /**
- * Gets the current checkbox state: unchecked, checked, or default.
- * @return the current state: STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
- * @see #getState
- * @see #STATE_UNCHECKED
- * @see #STATE_CHECKED
- * @see #STATE_DEFAULT
- */
- public int getState()
- {
- return state;
- }
-
- //
- // Methods
- //
-
- /**
- * Adds the specified action listener to receive action events
- * from this button.
- * @param l the action listener
- */
- public synchronized void addActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.add(actionListener, l);
- }
-
- /**
- * Removes the specified action listener so it no longer receives
- * action events from this button.
- * @param l the action listener
- */
- public synchronized void removeActionListener(ActionListener l)
- {
- actionListener = AWTEventMulticaster.remove(actionListener, l);
- }
-
- /**
- * Adds a listener for all event changes.
- * @param listener the listener to add.
- * @see #removePropertyChangeListener
- */
- public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
- {
- //super.addPropertyChangeListener(listener);
- changes.addPropertyChangeListener(listener);
- }
-
- /**
- * Removes a listener for all event changes.
- * @param listener the listener to remove.
- * @see #addPropertyChangeListener
- */
- public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
- {
- //super.removePropertyChangeListener(listener);
- changes.removePropertyChangeListener(listener);
- }
-
- /**
- * Adds a vetoable listener for all event changes.
- * @param listener the listener to add.
- * @see #removeVetoableChangeListener
- */
- public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.addVetoableChangeListener(listener);
- vetos.addVetoableChangeListener(listener);
- }
-
- /**
- * Removes a vetoable listener for all event changes.
- * @param listener the listener to remove.
- * @see #addVetoableChangeListener
- */
- public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- //super.removeVetoableChangeListener(listener);
- vetos.removeVetoableChangeListener(listener);
- }
-
- //
- // Implmentation
- //
-
- class Mouse extends MouseAdapter implements java.io.Serializable
- {
- /**
- * Processes MOUSE_DOWN events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_DOWN
- * event. These events occur when the mouse button is pressed while
- * inside this component.
- *
- * This method notes that the mouse is pressed inside the box and repaints it.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseUp
- * @see java.awt.Component#handleEvent
- */
- public void mousePressed(MouseEvent e)
- {
- pressed = true;
- released = false;
-
- repaint();
- }
-
- /**
- * Processes MOUSE_UP events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_UP
- * event. These events occur when the mouse button is released while
- * inside this component.
- *
- * If the mouse was pressed inside the box then the component is repainted
- * and an event is posted.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseDown
- * @see java.awt.Component#handleEvent
- */
- public void mouseReleased(MouseEvent e)
- {
- if (pressed)
- {
- pressed = false;
-
- state = (state + 1) % (type == TWO_STATE ? 2 : 3);
-
- repaint();
-
- sendActionEvent();
- }
-
- released = true;
- }
-
- /**
- * Processes MOUSE_ENTER events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_ENTER
- * event. These events occur when the mouse first moves over this
- * component.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseExit
- * @see java.awt.Component#handleEvent
- */
- public void mouseEntered(MouseEvent e)
- {
- if (!released)
- mousePressed(e);
- }
-
- /**
- * Processes MOUSE_EXIT events.
- * This is a standard Java AWT method which gets called by the AWT
- * method handleEvent() in response to receiving a MOUSE_EXIT
- * event. These events occur when the mouse first leaves this
- * component.
- *
- * @param e the event
- * @param x the component-relative horizontal coordinate of the mouse
- * @param y the component-relative vertical coordinate of the mouse
- *
- * @return always true since the event was handled
- *
- * @see #mouseEnter
- * @see java.awt.Component#handleEvent
- */
- public void mouseExited(MouseEvent e)
- {
- if (pressed)
- {
- pressed = false;
- repaint();
- }
- }
- }
-
- /**
- * Moves and/or resizes this component.
- * This is a standard Java AWT method which gets called to move and/or
- * resize this component. Components that are in containers with layout
- * managers should not call this method, but rely on the layout manager
- * instead.
- *
- * It is overriden here to note the requested width and height.
- *
- * @param x horizontal position in the parent's coordinate space
- * @param y vertical position in the parent's coordinate space
- * @param width the new width
- * @param height the new height
- */
- public void reshape(int x, int y, int width, int height)
- {
- this.width = WIDTH;
- this.height = HEIGHT;
-
- super.reshape(x, y, width, height);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * If this component has been reshaped, then the width and height of that
- * request are returned.
- *
- * @see java.awt.Component#minimumSize
- */
- public Dimension preferredSize()
- {
- return new Dimension(width, height);
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its [0,0]
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- g.clipRect(0, 0, width, height);
-
- g.setColor(Color.white);
- g.fillRect(0, 0, width, height);
-
- int w = width - 1, h = height - 1;
-
- if (pressed)
- {
- g.setColor(Color.lightGray);
- g.fillRect(2, 2, w - 4, h - 4);
- }
-
- g.setColor(Color.gray);
- g.drawLine(0, h - 1, 0, 0);
- g.drawLine(0, 0, w - 1, 0);
-
- g.setColor(Color.lightGray);
- g.drawLine(1, h - 2, w - 2, h - 2);
- g.drawLine(w - 2, h - 2, w - 2, 1);
-
- g.setColor(Color.black);
- g.drawLine(1, h - 2, 1, 1);
- g.drawLine(1, 1, w - 2, 1);
-
- switch (state)
- {
- case STATE_DEFAULT :
- {
- g.setColor(Color.lightGray);
- g.fillRect(2, 2, w - 4, h - 4);
- g.setColor(Color.black);
- // fall through
- }
- case STATE_CHECKED :
- {
- for (int i = 0; i < 3; ++i)
- {
- g.drawLine(3, 5 + i, 5, 7 + i);
- g.drawLine(5, 7 + i, 9, 3 + i);
- }
- break;
- }
- default :
- {
- break;
- }
- }
- }
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- *
- * @see #removeNotify
- */
- public synchronized void addNotify()
- {
- super.addNotify();
-
- //Hook up listeners
- if (mouse == null)
- {
- mouse = new Mouse();
- addMouseListener(mouse);
- }
- }
-
- /**
- * Tells this component that it is being removed from a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is removed from a container. Typically, it is used to
- * destroy the peers of this component and all its subcomponents.
- *
- * @see #addNotify
- */
- public synchronized void removeNotify()
- {
- //Unhook listeners
- if (mouse != null)
- {
- removeMouseListener(mouse);
- mouse = null;
- }
-
- super.removeNotify();
- }
-
- /**
- * Sends an action performed event to any action listeners, as needed.
- */
- protected void sendActionEvent()
- {
- if (actionListener != null)
- actionListener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
- }
-
- // Protected members
- /**
- * Handles tracking vetoable change listeners and notifying them of each change
- * to this component's properties.
- */
- protected VetoableChangeSupport vetos = new VetoableChangeSupport(this);
- /**
- * Handles tracking non-vetoable change listeners and notifying them of each change
- * to this component's properties.
- */
- protected PropertyChangeSupport changes = new PropertyChangeSupport(this);
- /**
- * The this component's mouse event listener.
- */
- protected Mouse mouse = null;
- /**
- * The action listener to keep track of listeners for our action event.
- */
- protected ActionListener actionListener = null;
-
- // Private members
- transient boolean pressed;
- transient boolean released;
- private int width;
- private int height;
- int state;
- int type;
- }
-
-