home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / StateCheckBox.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  12.1 KB  |  425 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Canvas;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.Color;
  8. import java.awt.Event;
  9.  
  10.  
  11. /**
  12.  * A two or three state checkbox that doesn't have an associated text label.
  13.  * A two-state checkbox behaves just like the standard checkbox. In three-state
  14.  * mode clicking the checkbox can switch the checkbox to a third state that 
  15.  * appears both gray and checked.
  16.  * <p>
  17.  * The third state is often used to indicate that an attribute of the current
  18.  * selection varies across the selected items. For example, a checkbox to
  19.  * indicate "bold text" might use the third state if the currently selected
  20.  * text included both bold and plain characters.
  21.  * <p>
  22.  * It is also used to indicate default action or state.
  23.  * <p>
  24.  * @version 1.0, Nov 26, 1996
  25.  *
  26.  * @author  Symantec
  27.  *
  28.  */
  29. public class StateCheckBox
  30.     extends Canvas
  31. {
  32.     //--------------------------------------------------
  33.     // constants
  34.     //--------------------------------------------------
  35.  
  36.     private static final int WIDTH            = 14;
  37.     private static final int HEIGHT           = 14;
  38.     /**
  39.      * This style constant indicates that this is a two-state checkbox.
  40.      */
  41.     public static final int TWO_STATE         = 0;
  42.     /**
  43.      * This style constant indicates that this is a three-state checkbox.
  44.      */
  45.     public static final int THREE_STATE       = 1;
  46.     /**
  47.      * This state constant indicates that the current state is "unchecked".
  48.      */
  49.     public static final int STATE_UNCHECKED   = 0;
  50.     /**
  51.      * This state constant indicates that the current state is "checked".
  52.      */
  53.     public static final int STATE_CHECKED     = 1;
  54.     /**
  55.      * This state constant indicates that the current state is "default".
  56.      * Default is the third state of a three-state checkbox.
  57.      */
  58.     public static final int STATE_DEFAULT     = 2;
  59.  
  60.  
  61.     //--------------------------------------------------
  62.     // class variables
  63.     //--------------------------------------------------
  64.  
  65.  
  66.     //--------------------------------------------------
  67.     // member variables
  68.     //--------------------------------------------------
  69.  
  70.     private boolean enabled;
  71.     private boolean pressed;
  72.     private boolean released;
  73.     private int width;
  74.     private int height;
  75.     private int state;
  76.     private int type;
  77.  
  78.  
  79.     //--------------------------------------------------
  80.     // constructors
  81.     //--------------------------------------------------
  82.  
  83.     /**
  84.      * Constructs a two-state StateCheckBox that is currently unchecked.
  85.      */
  86.     public StateCheckBox()
  87.     {
  88.         this.type   = TWO_STATE;
  89.         this.state  = STATE_UNCHECKED;
  90.  
  91.         width       = WIDTH;
  92.         height      = HEIGHT;
  93.  
  94.         state       = 0;
  95.  
  96.         pressed     = false;
  97.         released    = true;
  98.         enabled     = true;
  99.     }
  100.  
  101.  
  102.     //--------------------------------------------------
  103.     // accessor methods
  104.     //--------------------------------------------------
  105.  
  106.     /**
  107.      * Sets the current style to two-state or three-state.
  108.      * @param type new StateCheckBox type; either TWO_STATE or THREE_STATE
  109.      * @see #getStyle
  110.      */
  111.     public void setStyle(int type)
  112.     {
  113.         this.type = type;
  114.         invalidate();
  115.     }
  116.  
  117.     /**
  118.      * Gets the current style, two-state or three-state.
  119.      * @return the current StateCheckBox type; either TWO_STATE or THREE_STATE
  120.      * @see #setStyle
  121.      */
  122.     public int getStyle()
  123.     {
  124.         return type;
  125.     }
  126.  
  127.     /**
  128.      * Gets the current checkbox state; unchecked, checked, or default.
  129.      * @return the current state; STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
  130.      * @see #getState
  131.      */
  132.     public int getState()
  133.     {
  134.         return state;
  135.     }
  136.  
  137.     /**
  138.      * Sets the current checkbox state to unchecked, checked, or default.
  139.      * @param state the new state; STATE_UNCHECKED, STATE_CHECKED, or STATE_DEFAULT
  140.      * @see #getState
  141.      */
  142.     public void setState(int state)
  143.     {
  144.         if (this.state != state)
  145.         {
  146.             this.state = state;
  147.             invalidate();
  148.         }
  149.     }
  150.  
  151.  
  152.  
  153.     //--------------------------------------------------
  154.     // event methods
  155.     //--------------------------------------------------
  156.  
  157.     /**
  158.      * Processes MOUSE_DOWN events.
  159.      * This is a standard Java AWT method which gets called by the AWT
  160.      * method handleEvent() in response to receiving a MOUSE_DOWN
  161.      * event. These events occur when the mouse button is pressed while
  162.      * inside this component.
  163.      *
  164.      * This method notes that the mouse is pressed inside the box and repaints it.
  165.      *
  166.      * @param e the event
  167.      * @param x the component-relative horizontal coordinate of the mouse
  168.      * @param y the component-relative vertical coordinate of the mouse
  169.      *
  170.      * @return always true since the event was handled
  171.      *
  172.      * @see #mouseUp
  173.      * @see java.awt.Component#handleEvent
  174.      */
  175.     public boolean mouseDown(Event e, int x, int y)
  176.     {
  177.         pressed  = true;
  178.         released = false;
  179.  
  180.         repaint();
  181.  
  182.         return true;
  183.     }
  184.  
  185.  
  186.     /**
  187.      * Processes MOUSE_UP events.
  188.      * This is a standard Java AWT method which gets called by the AWT
  189.      * method handleEvent() in response to receiving a MOUSE_UP
  190.      * event. These events occur when the mouse button is released while
  191.      * inside this component.
  192.      *
  193.      * If the mouse was pressed inside the box then the component is repainted
  194.      * and an event is posted.
  195.      *
  196.      * @param e the event
  197.      * @param x the component-relative horizontal coordinate of the mouse
  198.      * @param y the component-relative vertical coordinate of the mouse
  199.      *
  200.      * @return always true since the event was handled
  201.      *
  202.      * @see #mouseDown
  203.      * @see java.awt.Component#handleEvent
  204.      */
  205.     public boolean mouseUp(Event e, int x, int y)
  206.     {
  207.         if (pressed)
  208.         {
  209.             pressed = false;
  210.             state = (state + 1) % (type == TWO_STATE ? 2 : 3);
  211.             repaint();
  212.             postEvent(new Event(this, Event.ACTION_EVENT, null));
  213.         }
  214.  
  215.         released = true;
  216.  
  217.         return true;
  218.     }
  219.  
  220.     /**
  221.      * Processes MOUSE_ENTER events.
  222.      * This is a standard Java AWT method which gets called by the AWT
  223.      * method handleEvent() in response to receiving a MOUSE_ENTER
  224.      * event. These events occur when the mouse first moves over this
  225.      * component.
  226.      *
  227.      * @param e the event
  228.      * @param x the component-relative horizontal coordinate of the mouse
  229.      * @param y the component-relative vertical coordinate of the mouse
  230.      *
  231.      * @return always true since the event was handled
  232.      *
  233.      * @see #mouseExit
  234.      * @see java.awt.Component#handleEvent
  235.      */
  236.     public boolean mouseEnter(Event e, int x, int y)
  237.     {
  238.         if (!released)
  239.         {
  240.             mouseDown(e, x, y);
  241.         }
  242.  
  243.         return true;
  244.     }
  245.  
  246.     /**
  247.      * Processes MOUSE_EXIT events.
  248.      * This is a standard Java AWT method which gets called by the AWT
  249.      * method handleEvent() in response to receiving a MOUSE_EXIT
  250.      * event. These events occur when the mouse first leaves this
  251.      * component.
  252.      *
  253.      * @param e the event
  254.      * @param x the component-relative horizontal coordinate of the mouse
  255.      * @param y the component-relative vertical coordinate of the mouse
  256.      *
  257.      * @return always true since the event was handled
  258.      *
  259.      * @see #mouseEnter
  260.      * @see java.awt.Component#handleEvent
  261.      */
  262.     public boolean mouseExit(Event e, int x, int y)
  263.     {
  264.         if (pressed)
  265.         {
  266.             pressed = false;
  267.             repaint();
  268.         }
  269.  
  270.         return true;
  271.     }
  272.  
  273.  
  274.     //--------------------------------------------------
  275.     // class methods
  276.     //--------------------------------------------------
  277.  
  278.  
  279.     //--------------------------------------------------
  280.     // member methods
  281.     //--------------------------------------------------
  282.  
  283.     /**
  284.      * Moves and/or resizes this component.
  285.      * This is a standard Java AWT method which gets called to move and/or
  286.      * resize this component. Components that are in containers with layout
  287.      * managers should not call this method, but rely on the layout manager
  288.      * instead.
  289.      *
  290.      * It is overriden here to note the requested width and height.
  291.      *
  292.      * @param x horizontal position in the parent's coordinate space
  293.      * @param y vertical position in the parent's coordinate space
  294.      * @param width the new width
  295.      * @param height the new height
  296.      */
  297.     public void reshape(int x, int y, int width, int height)
  298.     {
  299.         this.width  = WIDTH;
  300.         this.height = HEIGHT;
  301.  
  302.         super.reshape(x, y, width, height);
  303.     }
  304.  
  305.     /**
  306.      * Returns the recommended dimensions to properly display this component.
  307.      * This is a standard Java AWT method which gets called to determine
  308.      * the recommended size of this component.
  309.      *
  310.      * If this component has been reshaped, then the width and height of that
  311.      * request are returned.
  312.      *
  313.      * @see java.awt.Component#minimumSize
  314.      */
  315.     public Dimension preferredSize()
  316.     {
  317.         return new Dimension(width, height);
  318.     }
  319.  
  320.     /**
  321.      * Enables this component so that it will respond to user input.
  322.      * This is a standard Java AWT method which gets called to enable
  323.      * this component. Once enabled this component will respond to user input.
  324.      *
  325.      * @see #disable
  326.      */
  327.     public synchronized void enable()
  328.     {
  329.         if (!enabled)
  330.         {
  331.             enabled = true;
  332.  
  333.             repaint();
  334.         }
  335.  
  336.         super.enable();
  337.     }
  338.  
  339.  
  340.     /**
  341.      * Disables this component so that it doesn't respond to user input.
  342.      * This is a standard Java AWT method which gets called to disable
  343.      * this component. Once disabled this component will not respond to user 
  344.      * input.
  345.      *
  346.      * @see #enable
  347.      */
  348.     public synchronized void disable()
  349.     {
  350.         if (enabled)
  351.         {
  352.             enabled = false;
  353.  
  354.             repaint();
  355.         }
  356.  
  357.         super.disable();
  358.     }
  359.  
  360.     /**
  361.      * Paints this component using the given graphics context.
  362.      * This is a standard Java AWT method which typically gets called
  363.      * by the AWT to handle painting this component. It paints this component
  364.      * using the given graphics context. The graphics context clipping region
  365.      * is set to the bounding rectangle of this component and its <0,0>
  366.      * coordinate is this component's top-left corner.
  367.      *
  368.      * @param g the graphics context used for painting
  369.      * @see java.awt.Component#repaint
  370.      * @see java.awt.Component#update
  371.      */
  372.     public void paint(Graphics g)
  373.     {
  374.         g.clipRect(0, 0, width, height);
  375.  
  376.         g.setColor(Color.white);
  377.         g.fillRect(0, 0, width, height);
  378.  
  379.         int w = width - 1, h = height - 1;
  380.  
  381.         if (pressed)
  382.         {
  383.             g.setColor(Color.lightGray);
  384.             g.fillRect(2, 2, w - 4, h - 4);
  385.         }
  386.  
  387.         g.setColor(Color.gray);
  388.         g.drawLine(0, h - 1, 0, 0);
  389.         g.drawLine(0, 0, w - 1, 0);
  390.  
  391.         g.setColor(Color.lightGray);
  392.         g.drawLine(1, h - 2, w - 2, h - 2);
  393.         g.drawLine(w - 2, h - 2, w - 2, 1);
  394.  
  395.         g.setColor(Color.black);
  396.         g.drawLine(1, h - 2, 1, 1);
  397.         g.drawLine(1, 1, w - 2, 1);
  398.  
  399.         switch (state)
  400.         {
  401.             case STATE_DEFAULT :
  402.             {
  403.                 g.setColor(Color.lightGray);
  404.                 g.fillRect(2, 2, w - 4, h - 4);
  405.                 g.setColor(Color.black);
  406.                 // fall through
  407.             }
  408.             case STATE_CHECKED :
  409.             {
  410.                 for (int i = 0; i < 3; ++i)
  411.                 {
  412.                     g.drawLine(3, 5 + i, 5, 7 + i);
  413.                     g.drawLine(5, 7 + i, 9, 3 + i);
  414.                 }
  415.                 break;
  416.             }
  417.             default :
  418.             {
  419.                 break;
  420.             }
  421.         }
  422.     }
  423. }
  424.  
  425.