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

  1. /*
  2.  * @(#)CheckboxMenuItem.java    1.27 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.CheckboxMenuItemPeer;
  25. import java.awt.event.*;
  26. import java.io.ObjectOutputStream;
  27. import java.io.ObjectInputStream;
  28. import java.io.IOException;
  29.  
  30.  
  31. /**
  32.  * This class represents a check box that can be included in a menu. 
  33.  * Clicking on the check box in the menu changes its state from 
  34.  * "on" to "off" or from "off" to "on." 
  35.  * <p>
  36.  * The following picture depicts a menu which contains an instance
  37.  * of <code>CheckBoxMenuItem</code>:
  38.  * <p>
  39.  * <img src="images-awt/MenuBar-1.gif" 
  40.  * ALIGN=center HSPACE=10 VSPACE=7>
  41.  * <p>
  42.  * The item labeled <code>Check</code> shows a check box menu item 
  43.  * in its "off" state. 
  44.  * <p>
  45.  * When a check box menu item is selected, AWT sends an item event to 
  46.  * the item. Since the event is an instance of <code>ItemEvent</code>, 
  47.  * the <code>processEvent</code> method examines the event and passes 
  48.  * it along to <code>processItemEvent</code>. The latter method redirects 
  49.  * the event to any <code>ItemListener</code> objects that have
  50.  * registered an interest in item events generated by this menu item. 
  51.  *
  52.  * @version 1.27, 06/12/97
  53.  * @author     Sami Shaio
  54.  * @see         java.awt.event.ItemEvent
  55.  * @see         java.awt.event.ItemListener
  56.  * @since       JDK1.0
  57.  */
  58. public class CheckboxMenuItem extends MenuItem implements ItemSelectable {
  59.  
  60.     boolean state = false;
  61.  
  62.     transient ItemListener itemListener;
  63.  
  64.     private static final String base = "chkmenuitem";
  65.     private static int nameCounter = 0;
  66.  
  67.     /*
  68.      * JDK 1.1 serialVersionUID 
  69.      */
  70.      private static final long serialVersionUID = 6190621106981774043L;
  71.  
  72.     /**
  73.      * Create a check box menu item with an empty label.
  74.      * The item's state is initially set to "off." 
  75.      * @since   JDK1.1
  76.      */
  77.     public CheckboxMenuItem() {
  78.     this("", false);
  79.     }
  80.  
  81.     /**
  82.      * Create a check box menu item with the specified label. 
  83.      * The item's state is initially set to "off." 
  84.  
  85.      * @param     label   a string label for the check box menu item, 
  86.      *                or <code>null</code> for an unlabeled menu item.
  87.      * @since     JDK1.0
  88.      */
  89.     public CheckboxMenuItem(String label) {
  90.     this(label, false);
  91.     }
  92.  
  93.     /**
  94.      * Create a check box menu item with the specified label and state.
  95.      * @param      label   the button label.
  96.      * @param      state   the initial state of the menu item, where
  97.      *                     <code>true</code> indicates "on" and 
  98.      *                     <code>false</code> indicates "off."
  99.      * @since      JDK1.1
  100.      */
  101.     public CheckboxMenuItem(String label, boolean state) {
  102.         super(label);
  103.     this.name = base + nameCounter++;
  104.         this.state = state;
  105.     }
  106.  
  107.     /**
  108.      * Creates the peer of the checkbox item.  This peer allows us to
  109.      * change the look of the checkbox item without changing its 
  110.      * functionality.
  111.      * Most applications do not call this method directly. 
  112.      * @see     java.awt.Toolkit#createCheckboxMenuItem(java.awt.CheckboxMenuItem)
  113.      * @see     java.awt.Component#getToolkit()
  114.      * @since   JDK1.0
  115.      */
  116.     public void addNotify() {
  117.     peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this);
  118.     super.addNotify();
  119.     }
  120.  
  121.     /**
  122.      * Determines whether the state of this check box menu item 
  123.      * is "on" or "off." 
  124.      * @return      the state of this check box menu item, where
  125.      *                     <code>true</code> indicates "on" and 
  126.      *                     <code>false</code> indicates "off."
  127.      * @see        java.awt.CheckboxMenuItem#setState
  128.      * @since      JDK1.0
  129.      */
  130.     public boolean getState() {
  131.     return state;
  132.     }
  133.  
  134.     /**
  135.      * Sets this check box menu item to the specifed state.
  136.      * The boolean value <code>true</code> indicates "on" while 
  137.      * <code>false</code> indicates "off." 
  138.      * @param      b   the boolean state of this 
  139.      *                      check box menu item.
  140.      * @see        java.awt.CheckboxMenuItem#getState
  141.      * @since      JDK1.0
  142.      */
  143.     public synchronized void setState(boolean b) {
  144.     state = b;
  145.     CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer;
  146.     if (peer != null) {
  147.         peer.setState(b);
  148.     }
  149.     }
  150.  
  151.     /**
  152.      * Returns the an array (length 1) containing the checkbox menu item
  153.      * label or null if the checkbox is not selected.
  154.      * @see ItemSelectable
  155.      */
  156.     public synchronized Object[] getSelectedObjects() {
  157.         if (state) {
  158.             Object[] items = new Object[1];
  159.             items[0] = label;
  160.             return items;
  161.         }
  162.         return null;
  163.     }
  164.  
  165.     /**
  166.      * Adds the specified item listener to receive item events from
  167.      * this check box menu item.
  168.      * @param         l the item listener.
  169.      * @see           java.awt.event.ItemEvent
  170.      * @see           java.awt.event.ItemListener
  171.      * @see           java.awt.Choice#removeItemListener
  172.      * @since         JDK1.1
  173.      */ 
  174.     public synchronized void addItemListener(ItemListener l) {
  175.         itemListener = AWTEventMulticaster.add(itemListener, l);
  176.         newEventsOnly = true;
  177.     }
  178.  
  179.     /**
  180.      * Removes the specified item listener so that it no longer receives
  181.      * item events from this check box menu item.
  182.      * @param         l the item listener.
  183.      * @see           java.awt.event.ItemEvent
  184.      * @see           java.awt.event.ItemListener
  185.      * @see           java.awt.Choice#addItemListener
  186.      * @since         JDK1.1
  187.      */ 
  188.     public synchronized void removeItemListener(ItemListener l) {
  189.         itemListener = AWTEventMulticaster.remove(itemListener, l);
  190.     }
  191.  
  192.     // REMIND: remove when filtering is done at lower level
  193.     boolean eventEnabled(AWTEvent e) {
  194.         if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
  195.             if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  196.                 itemListener != null) {
  197.                 return true;
  198.             } 
  199.             return false;
  200.         }
  201.         return super.eventEnabled(e);
  202.     }        
  203.  
  204.     /**
  205.      * Processes events on this check box menu item. 
  206.      * If the event is an instance of <code>ItemEvent</code>, 
  207.      * this method invokes the <code>processItemEvent</code> method.
  208.      * If the event is not an item event,
  209.      * it invokes <code>processEvent</code> on the superclass.
  210.      * <p>
  211.      * Check box menu items currently support only item events.
  212.      * @param        e the event
  213.      * @see          java.awt.event.ItemEvent
  214.      * @see          java.awt.CheckboxMenuItem#processItemEvent
  215.      * @since        JDK1.1
  216.      */
  217.     protected void processEvent(AWTEvent e) {
  218.         if (e instanceof ItemEvent) {
  219.             processItemEvent((ItemEvent)e);
  220.         return;
  221.         }
  222.     super.processEvent(e);
  223.     }
  224.  
  225.     /** 
  226.      * Processes item events occurring on this check box menu item by
  227.      * dispatching them to any registered <code>ItemListener</code> objects. 
  228.      * <p>
  229.      * This method is not called unless item events are 
  230.      * enabled for this menu item. Item events are enabled 
  231.      * when one of the following occurs:
  232.      * <p><ul>
  233.      * <li>An <code>ItemListener</code> object is registered 
  234.      * via <code>addItemListener</code>.
  235.      * <li>Item events are enabled via <code>enableEvents</code>.
  236.      * </ul>
  237.      * @param       e the item event.
  238.      * @see         java.awt.event.ItemEvent
  239.      * @see         java.awt.event.ItemListener
  240.      * @see         java.awt.CheckboxMenuItem#addItemListener
  241.      * @see         java.awt.MenuItem#enableEvents
  242.      * @since       JDK1.1
  243.      */  
  244.     protected void processItemEvent(ItemEvent e) {
  245.         if (itemListener != null) {
  246.             itemListener.itemStateChanged(e);
  247.         }
  248.     }
  249.  
  250.     /**
  251.      * Returns the parameter string representing the state of this check 
  252.      * box menu item. This string is useful for debugging. 
  253.      * @return     the parameter string of this check box menu item.
  254.      * @since      JDK1.0
  255.      */
  256.     public String paramString() {
  257.     return super.paramString() + ",state=" + state;
  258.     }
  259.  
  260.     /* Serialization support. 
  261.      */
  262.  
  263.     private int checkboxMenuItemSerializedDataVersion = 1;
  264.  
  265.  
  266.     private void writeObject(ObjectOutputStream s)
  267.       throws java.io.IOException 
  268.     {
  269.       s.defaultWriteObject();
  270.  
  271.       AWTEventMulticaster.save(s, itemListenerK, itemListener);
  272.       s.writeObject(null);
  273.     }
  274.  
  275.  
  276.     private void readObject(ObjectInputStream s)
  277.       throws ClassNotFoundException, IOException 
  278.     {
  279.       s.defaultReadObject();
  280.  
  281.       Object keyOrNull;
  282.       while(null != (keyOrNull = s.readObject())) {
  283.     String key = ((String)keyOrNull).intern();
  284.  
  285.     if (itemListenerK == key) 
  286.       addItemListener((ItemListener)(s.readObject()));
  287.  
  288.     else // skip value for unrecognized key
  289.       s.readObject();
  290.       }
  291.     }
  292. }
  293.