home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Checkbox.java < prev    next >
Text File  |  1997-10-01  |  14KB  |  428 lines

  1. /*
  2.  * @(#)Checkbox.java    1.37 97/06/12
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22. package java.awt;
  23.  
  24. import java.awt.peer.CheckboxPeer;
  25. import java.awt.event.*;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * A check box is a graphical component that can be in either an 
  33.  * "on" (<code>true</code>) or "off" (<code>false</code>) state.
  34.  * Clicking on a check box changes its state from 
  35.  * "on" to "off," or from "off" to "on." 
  36.  * <p>
  37.  * The following code example creates a set of check boxes in
  38.  * a grid layout: 
  39.  * <p>
  40.  * <hr><blockquote><pre>
  41.  * setLayout(new GridLayout(3, 1));
  42.  * add(new Checkbox("one", null, true));
  43.  * add(new Checkbox("two"));
  44.  * add(new Checkbox("three"));
  45.  * </pre></blockquote><hr>
  46.  * <p>
  47.  * This image depicts the check boxes and grid layout
  48.  * created by this code example:
  49.  * <p>
  50.  * <img src="images-awt/Checkbox-1.gif"
  51.  * ALIGN=center HSPACE=10 VSPACE=7>
  52.  * <p>
  53.  * The button labeled <code>one</code> is in the "on" state, and the 
  54.  * other two are in the "off" state. In this example, which uses the
  55.  * <code>GridLayout</code> class, the states of the three check 
  56.  * boxes are set independently.
  57.  * <p>
  58.  * Alternatively, several check boxes can be grouped together under 
  59.  * the control of a single object, using the 
  60.  * <code>CheckboxGroup</code> class. 
  61.  * In a check box group, at most one button can be in the "on" 
  62.  * state at any given time. Clicking on a check box to turn it on
  63.  * forces any other check box in the same group that is on 
  64.  * into the "off" state.
  65.  *
  66.  * @version    1.37 06/12/97
  67.  * @author     Sami Shaio
  68.  * @see         java.awt.GridLayout
  69.  * @see         java.awt.CheckboxGroup
  70.  * @since       JDK1.0
  71.  */
  72. public class Checkbox extends Component implements ItemSelectable {
  73.     /**
  74.      * The label of the Checkbox.
  75.      */
  76.     String label;
  77.  
  78.     /**
  79.      * The state of the Checkbox.
  80.      */
  81.     boolean state;
  82.  
  83.     /**
  84.      * The check box group.
  85.      */
  86.     CheckboxGroup group;
  87.  
  88.     transient ItemListener itemListener;
  89.  
  90.     private static final String base = "checkbox";
  91.     private static int nameCounter = 0;
  92.  
  93.     /*
  94.      * JDK 1.1 serialVersionUID 
  95.      */
  96.     private static final long serialVersionUID = 7270714317450821763L;
  97.  
  98.     /**
  99.      * Helper function for setState and CheckboxGroup.setSelectedCheckbox
  100.      * Should remain package-private.
  101.      */
  102.     synchronized void setStateInternal(boolean state) {
  103.     this.state = state;
  104.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  105.     if (peer != null) {
  106.         peer.setState(state);
  107.     }
  108.     }
  109.  
  110.     /**
  111.      * Creates a check box with no label. The state of this 
  112.      * check box is set to "off," and it is not part of any 
  113.      * check box group. 
  114.      * @since     JDK1.0
  115.      */
  116.     public Checkbox() {
  117.         this("", false, null);
  118.     }
  119.  
  120.     /**
  121.      * Creates a check box with the specified label.  The state 
  122.      * of this check box is set to "off," and it is not part of  
  123.      * any check box group. 
  124.      * @param     label   a string label for this check box, 
  125.      *                        or <code>null</code> for no label.
  126.      * @since     JDK1.0
  127.      */
  128.     public Checkbox(String label) {
  129.     this(label, false, null);
  130.     }
  131.  
  132.     /**
  133.      * Creates a check box with the specified label.  The state 
  134.      * of this check box is as specified by the <code>state</code> 
  135.      * argument, and it is not part of any check box group. 
  136.      * @param     label   a string label for this check box, 
  137.      *                        or <code>null</code> for no label.
  138.      * @param     state    the initial state of this check box.
  139.      * @since     JDK1.0
  140.      */
  141.     public Checkbox(String label, boolean state) {
  142.         this(label, state, null);
  143.     }
  144.  
  145.     /**
  146.      * Creates a check box with the specified label, in the specified 
  147.      * check box group, and set to the specified state. 
  148.  
  149.      * @param     label   a string label for this check box, 
  150.      *                        or <code>null</code> for no label.
  151.      * @param     state   the initial state of this check box.
  152.      * @param     group   a check box group for this check box, 
  153.      *                           or <code>null</code> for no group.
  154.      * @since     JDK1.1
  155.      */
  156.     public Checkbox(String label, boolean state, CheckboxGroup group) {
  157.     this.name = base + nameCounter++;        
  158.     this.label = label;
  159.     this.state = state;
  160.     this.group = group;
  161.     if (state && (group != null)) {
  162.         group.setSelectedCheckbox(this);
  163.     }
  164.     }
  165.  
  166.     /**
  167.      * Constructs a Checkbox with the specified label, set to the
  168.      * specified state, and in the specified check box group.
  169.      */
  170.     public Checkbox(String label, CheckboxGroup group, boolean state) {
  171.         this(label, state, group);
  172.     }
  173.  
  174.     /**
  175.      * Creates the peer of the Checkbox. The peer allows you to change the
  176.      * look of the Checkbox without changing its functionality.
  177.      * @see     java.awt.Toolkit#createCheckbox(java.awt.Checkbox)
  178.      * @see     java.awt.Component#getToolkit()
  179.      * @since   JDK1.0
  180.      */
  181.     public void addNotify() {
  182.     peer = getToolkit().createCheckbox(this);
  183.     super.addNotify();
  184.     }
  185.  
  186.     /**
  187.      * Gets the label of this check box.
  188.      * @return   the label of this check box, or <code>null</code> 
  189.      *                  if this check box has no label.
  190.      * @see      java.awt.Checkbox#setLabel
  191.      * @since    JDK1.0
  192.      */
  193.     public String getLabel() {
  194.     return label;
  195.     }
  196.  
  197.     /**
  198.      * Sets this check box's label to be the string argument. 
  199.      * @param    label   a string to set as the new label, or 
  200.      *                        <code>null</code> for no label.
  201.      * @see      java.awt.Checkbox#getLabel 
  202.      * @since    JDK1.0
  203.      */
  204.     public synchronized void setLabel(String label) {
  205.     this.label = label;
  206.     CheckboxPeer peer = (CheckboxPeer)this.peer;
  207.     if (peer != null) {
  208.         peer.setLabel(label);
  209.     }
  210.     }
  211.  
  212.     /** 
  213.      * Determines whether this check box is in the "on" or "off" state.
  214.      * The boolean value <code>true</code> indicates the "on" state,  
  215.      * and <code>false</code> indicates the "off" state.
  216.      * @return    the state of this check box, as a boolean value. 
  217.      * @see       java.awt.Checkbox#setState  
  218.      * @since     JDK1.0
  219.      */
  220.     public boolean getState() {
  221.     return state;
  222.     }
  223.     
  224.     /** 
  225.      * Sets the state of this check box to the specified state. 
  226.      * The boolean value <code>true</code> indicates the "on" state,  
  227.      * and <code>false</code> indicates the "off" state.
  228.      * @param     state   the boolean state of the check box.
  229.      * @see       java.awt.Checkbox#getState  
  230.      * @since     JDK1.0
  231.      */
  232.     public void setState(boolean state) {
  233.     /* Cannot hold check box lock when calling group.setSelectedCheckbox. */
  234.         CheckboxGroup group = this.group;
  235.     if (group != null) {
  236.         if (state) {
  237.         group.setSelectedCheckbox(this);
  238.         } else if (group.getSelectedCheckbox() == this) {
  239.         state = true;
  240.         }
  241.     }
  242.     setStateInternal(state);
  243.     }
  244.  
  245.     /**
  246.      * Returns the an array (length 1) containing the checkbox
  247.      * label or null if the checkbox is not selected.
  248.      * @see ItemSelectable
  249.      */
  250.     public Object[] getSelectedObjects() {
  251.         if (state) {
  252.             Object[] items = new Object[1];
  253.             items[0] = label;
  254.             return items;
  255.         }
  256.         return null;
  257.     }
  258.  
  259.     /**
  260.      * Determines this check box's group. 
  261.      * @return     this check box's group, or <code>null</code> 
  262.      *               if the check box is not part of a check box group.
  263.      * @see        java.awt.Checkbox#setCheckboxGroup 
  264.      * @since      JDK1.0
  265.      */
  266.     public CheckboxGroup getCheckboxGroup() {
  267.     return group;
  268.     }
  269.  
  270.     /**
  271.      * Sets this check box's group to be the specified check box group. 
  272.      * If this check box is already in a different check box group, 
  273.      * it is first taken out of that group. 
  274.      * @param     g   the new check box group, or <code>null</code> 
  275.      *                to remove this check box from any check box group.
  276.      * @see       java.awt.Checkbox#getCheckboxGroup  
  277.      * @since     JDK1.0
  278.      */
  279.     public void setCheckboxGroup(CheckboxGroup g) {
  280.         CheckboxGroup group = this.group;
  281.     if (group != null) {
  282.         group.setSelectedCheckbox(null);
  283.     }
  284.     /* Locking check box above could cause deadlock with
  285.      * CheckboxGroup's setSelectedCheckbox method.
  286.      */
  287.     synchronized (this) {
  288.         this.group = g;
  289.         CheckboxPeer peer = (CheckboxPeer)this.peer;
  290.         if (peer != null) {
  291.         peer.setCheckboxGroup(g);
  292.         }
  293.     }
  294.     }
  295.  
  296.     /**
  297.      * Adds the specified item listener to receive item events from
  298.      * this check box.
  299.      * @param         l    the item listener.
  300.      * @see           java.awt.event.ItemEvent
  301.      * @see           java.awt.event.ItemListener
  302.      * @see           java.awt.Checkbox#removeItemListener
  303.      * @since         JDK1.1
  304.      */ 
  305.     public synchronized void addItemListener(ItemListener l) {
  306.         itemListener = AWTEventMulticaster.add(itemListener, l);
  307.         newEventsOnly = true;
  308.     }
  309.  
  310.     /**
  311.      * Removes the specified item listener so that the item listener 
  312.      * no longer receives item events from this check box. 
  313.      * @param         l    the item listener.
  314.      * @see           java.awt.event.ItemEvent
  315.      * @see           java.awt.event.ItemListener
  316.      * @see           java.awt.Checkbox#addItemListener
  317.      * @since         JDK1.1
  318.      */ 
  319.     public synchronized void removeItemListener(ItemListener l) {
  320.         itemListener = AWTEventMulticaster.remove(itemListener, l);
  321.     }
  322.  
  323.     // REMIND: remove when filtering is done at lower level
  324.     boolean eventEnabled(AWTEvent e) {
  325.         if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
  326.             if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  327.                 itemListener != null) {
  328.                 return true;
  329.             }             
  330.             return false;
  331.         }
  332.         return super.eventEnabled(e);
  333.     }          
  334.  
  335.     /**
  336.      * Processes events on this check box. 
  337.      * If the event is an instance of <code>ItemEvent</code>,
  338.      * this method invokes the <code>processItemEvent</code> method. 
  339.      * Otherwise, it calls its superclass's <code>processEvent</code> method.
  340.      * @param         e the event.
  341.      * @see           java.awt.event.ItemEvent
  342.      * @see           java.awt.Checkbox#processItemEvent
  343.      * @since         JDK1.1
  344.      */
  345.     protected void processEvent(AWTEvent e) {
  346.         if (e instanceof ItemEvent) {
  347.             processItemEvent((ItemEvent)e);
  348.             return;
  349.         }
  350.     super.processEvent(e);
  351.     }
  352.  
  353.     /** 
  354.      * Processes item events occurring on this check box by
  355.      * dispatching them to any registered 
  356.      * <code>ItemListener</code> objects. 
  357.      * <p>
  358.      * This method is not called unless item events are 
  359.      * enabled for this component. Item events are enabled 
  360.      * when one of the following occurs:
  361.      * <p><ul>
  362.      * <li>An <code>ItemListener</code> object is registered 
  363.      * via <code>addItemListener</code>.
  364.      * <li>Item events are enabled via <code>enableEvents</code>.
  365.      * </ul>
  366.      * @param       e the item event.
  367.      * @see         java.awt.event.ItemEvent
  368.      * @see         java.awt.event.ItemListener
  369.      * @see         java.awt.Checkbox#addItemListener
  370.      * @see         java.awt.Component#enableEvents
  371.      * @since       JDK1.1
  372.      */  
  373.     protected void processItemEvent(ItemEvent e) {
  374.         if (itemListener != null) {
  375.             itemListener.itemStateChanged(e);
  376.         }
  377.     }
  378.  
  379.     /**
  380.      * Returns the parameter string representing the state of 
  381.      * this check box. This string is useful for debugging. 
  382.      * @return    the parameter string of this check box.
  383.      * @since     JDK1.0
  384.      */
  385.     protected String paramString() {
  386.     String str = super.paramString();
  387.     String label = this.label;
  388.     if (label != null) {
  389.         str += ",label=" + label;
  390.     }
  391.     return str + ",state=" + state;
  392.     }
  393.  
  394.  
  395.     /* Serialization support. 
  396.      */
  397.  
  398.     private int checkboxSerializedDataVersion = 1;
  399.  
  400.  
  401.     private void writeObject(ObjectOutputStream s)
  402.       throws java.io.IOException 
  403.     {
  404.       s.defaultWriteObject();
  405.  
  406.       AWTEventMulticaster.save(s, itemListenerK, itemListener);
  407.       s.writeObject(null);
  408.     }
  409.  
  410.  
  411.     private void readObject(ObjectInputStream s)
  412.       throws ClassNotFoundException, IOException 
  413.     {
  414.       s.defaultReadObject();
  415.  
  416.       Object keyOrNull;
  417.       while(null != (keyOrNull = s.readObject())) {
  418.     String key = ((String)keyOrNull).intern();
  419.  
  420.     if (itemListenerK == key) 
  421.       addItemListener((ItemListener)(s.readObject()));
  422.  
  423.     else // skip value for unrecognized key
  424.       s.readObject();
  425.       }
  426.     }
  427. }
  428.