home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / CheckboxMenuItem.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  9.0 KB  |  289 lines

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