home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 12.1 KB | 425 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.Event;
-
-
- /**
- * 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 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 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;
- /**
- * This style constant indicates that this is a two-state checkbox.
- */
- public static final int TWO_STATE = 0;
- /**
- * This style constant indicates that this is a three-state checkbox.
- */
- public static final int THREE_STATE = 1;
- /**
- * This state constant indicates that the current state is "unchecked".
- */
- public static final int STATE_UNCHECKED = 0;
- /**
- * This state constant indicates that the current state is "checked".
- */
- public static final int STATE_CHECKED = 1;
- /**
- * This state constant indicates that the current state is "default".
- * Default is the third state of a three-state checkbox.
- */
- public static final int STATE_DEFAULT = 2;
-
-
- //--------------------------------------------------
- // class variables
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member variables
- //--------------------------------------------------
-
- private boolean enabled;
- private boolean pressed;
- private boolean released;
- private int width;
- private int height;
- private int state;
- private int type;
-
-
- //--------------------------------------------------
- // 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;
- enabled = true;
- }
-
-
- //--------------------------------------------------
- // accessor methods
- //--------------------------------------------------
-
- /**
- * Sets the current style to two-state or three-state.
- * @param type new StateCheckBox type; either TWO_STATE or THREE_STATE
- * @see #getStyle
- */
- public void setStyle(int type)
- {
- this.type = type;
- invalidate();
- }
-
- /**
- * Gets the current style, two-state or three-state.
- * @return the current StateCheckBox type; either TWO_STATE or THREE_STATE
- * @see #setStyle
- */
- public int getStyle()
- {
- return type;
- }
-
- /**
- * Gets the current checkbox state; unchecked, checked, or default.
- * @return the current state; STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
- * @see #getState
- */
- public int getState()
- {
- return state;
- }
-
- /**
- * Sets the current checkbox state to unchecked, checked, or default.
- * @param state the new state; STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
- * @see #getState
- */
- public void setState(int state)
- {
- if (this.state != state)
- {
- this.state = state;
- invalidate();
- }
- }
-
-
-
- //--------------------------------------------------
- // event methods
- //--------------------------------------------------
-
- /**
- * 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 boolean mouseDown(Event e, int x, int y)
- {
- pressed = true;
- released = false;
-
- repaint();
-
- return true;
- }
-
-
- /**
- * 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 boolean mouseUp(Event e, int x, int y)
- {
- if (pressed)
- {
- pressed = false;
- state = (state + 1) % (type == TWO_STATE ? 2 : 3);
- repaint();
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- }
-
- released = true;
-
- return 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 boolean mouseEnter(Event e, int x, int y)
- {
- if (!released)
- {
- mouseDown(e, x, y);
- }
-
- return true;
- }
-
- /**
- * 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 boolean mouseExit(Event e, int x, int y)
- {
- if (pressed)
- {
- pressed = false;
- repaint();
- }
-
- return true;
- }
-
-
- //--------------------------------------------------
- // class methods
- //--------------------------------------------------
-
-
- //--------------------------------------------------
- // member methods
- //--------------------------------------------------
-
- /**
- * 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);
- }
-
- /**
- * Enables this component so that it will respond to user input.
- * This is a standard Java AWT method which gets called to enable
- * this component. Once enabled this component will respond to user input.
- *
- * @see #disable
- */
- public synchronized void enable()
- {
- if (!enabled)
- {
- enabled = true;
-
- repaint();
- }
-
- super.enable();
- }
-
-
- /**
- * Disables this component so that it doesn't respond to user input.
- * This is a standard Java AWT method which gets called to disable
- * this component. Once disabled this component will not respond to user
- * input.
- *
- * @see #enable
- */
- public synchronized void disable()
- {
- if (enabled)
- {
- enabled = false;
-
- repaint();
- }
-
- super.disable();
- }
-
- /**
- * 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;
- }
- }
- }
- }
-
-