home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Checkbox.java < prev    next >
Text File  |  1996-05-03  |  5KB  |  190 lines

  1. /*
  2.  * @(#)Checkbox.java    1.14 96/04/04 Sami Shaio
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.awt.peer.CheckboxPeer;
  22.  
  23. /**
  24.  * A Checkbox object is a graphical user interface element that has a boolean 
  25.  * state.
  26.  *
  27.  * @version    1.14 04 Apr 1996
  28.  * @author     Sami Shaio
  29.  */
  30. public class Checkbox extends Component {
  31.     /**
  32.      * The label of the Checkbox.
  33.      */
  34.     String label;
  35.  
  36.     /**
  37.      * The state of the Checkbox.
  38.      */
  39.     boolean state;
  40.  
  41.     /**
  42.      * The check box group.
  43.      */
  44.     CheckboxGroup group;
  45.  
  46.     /**
  47.      * Helper function for setState and CheckboxGroup.setCurrent
  48.      * Should remain package-private.
  49.      */
  50.     void setStateInternal(boolean state) {
  51.     this.state = state;
  52.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  53.     if (peer != null) {
  54.         peer.setState(state);
  55.     }
  56.     }
  57.  
  58.     /**
  59.      * Constructs a Checkbox with no label, no Checkbox group, and initialized  
  60.      * to a false state.
  61.      */
  62.     public Checkbox() {
  63.     }
  64.  
  65.     /**
  66.      * Constructs a Checkbox with the specified label, no Checkbox group, and 
  67.      * initialized to a false state.
  68.      * @param label the label on the Checkbox
  69.      */
  70.     public Checkbox(String label) {
  71.     this.label = label;
  72.     }
  73.  
  74.     /**
  75.      * Constructs a Checkbox with the specified label, specified Checkbox 
  76.      * group, and specified boolean state.  If the specified CheckboxGroup
  77.      * is not equal to null, then this Checkbox becomes a Checkbox button.  
  78.      * If the Checkbox becomes a button, this simply means that only 
  79.      * one Checkbox in a CheckboxGroup may be set at a time.
  80.      * @param label the label on the Checkbox
  81.      * @param group the CheckboxGroup this Checkbox is in
  82.      * @param state is the initial state of this Checkbox
  83.      */
  84.     public Checkbox(String label, CheckboxGroup group, boolean state) {
  85.     this.label = label;
  86.     this.state = state;
  87.     this.group = group;
  88.     if (state && (group != null)) {
  89.         group.setCurrent(this);
  90.     }
  91.     }
  92.  
  93.     /**
  94.      * Creates the peer of the Checkbox. The peer allows you to change the
  95.      * look of the Checkbox without changing its functionality.
  96.      */
  97.     public synchronized void addNotify() {
  98.     peer = getToolkit().createCheckbox(this);
  99.     super.addNotify();
  100.     }
  101.  
  102.     /**
  103.      * Gets the label of the button.
  104.      * @see #setLabel
  105.      */
  106.     public String getLabel() {
  107.     return label;
  108.     }
  109.  
  110.     /**
  111.      * Sets the button with the specified label.
  112.      * @param label the label of the button
  113.      * @see #getLabel
  114.      */
  115.     public void setLabel(String label) {
  116.     this.label = label;
  117.  
  118.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  119.     if (peer != null) {
  120.         peer.setLabel(label);
  121.     }
  122.     }
  123.  
  124.     /** 
  125.      * Returns the boolean state of the Checkbox. 
  126.      * @see #setState
  127.      */
  128.     public boolean getState() {
  129.     return state;
  130.     }
  131.     
  132.     /** 
  133.      * Sets the Checkbox to the specifed boolean state.
  134.      * @param state the boolean state 
  135.      * @see #getState
  136.      */
  137.     public void setState(boolean state) {
  138.     CheckboxGroup group = this.group;
  139.     if (group != null) {
  140.         if (state) {
  141.         group.setCurrent(this);
  142.         } else if (group.getCurrent() == this) {
  143.         state = true;
  144.         }
  145.     }
  146.     setStateInternal(state);
  147.     }
  148.  
  149.     /**
  150.      * Returns the checkbox group.
  151.      * @see #setCheckboxGroup
  152.      */
  153.     public CheckboxGroup getCheckboxGroup() {
  154.     return group;
  155.     }
  156.  
  157.     /**
  158.      * Sets the CheckboxGroup to the specified group.
  159.      * @param g the new CheckboxGroup
  160.      * @see #getCheckboxGroup
  161.      */
  162.     public void setCheckboxGroup(CheckboxGroup g) {
  163.     CheckboxGroup group = this.group;
  164.     if (group != null) {
  165.         group.setCurrent(null);
  166.     }
  167.     this.group = g;
  168.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  169.     if (peer != null) {
  170.         peer.setCheckboxGroup(g);
  171.     }
  172.     }
  173.  
  174.     /**
  175.      * Returns the parameter String of this Checkbox.
  176.      */
  177.     protected String paramString() {
  178.     String str = super.paramString();
  179.     String label = this.label;
  180.     if (label != null) {
  181.         str += ",label=" + label;
  182.     }
  183.     return str + ",state=" + state;
  184.     }
  185.  
  186.     boolean tabbable() {
  187.     return true;
  188.     }
  189. }
  190.