home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / CheckboxMenuItem.java < prev    next >
Text File  |  1997-10-27  |  6KB  |  227 lines

  1. /*
  2.  * @(#)CheckboxMenuItem.java    1.25 97/03/03
  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 produces a checkbox that represents a choice in a menu.
  33.  *
  34.  * @version 1.25, 03/03/97
  35.  * @author     Sami Shaio
  36.  */
  37. public class CheckboxMenuItem extends MenuItem implements ItemSelectable {
  38.  
  39.     boolean state = false;
  40.  
  41.     transient ItemListener itemListener;
  42.  
  43.     private static final String base = "chkmenuitem";
  44.     private static int nameCounter = 0;
  45.  
  46.     /*
  47.      * JDK 1.1 serialVersionUID 
  48.      */
  49.      private static final long serialVersionUID = 6190621106981774043L;
  50.  
  51.     /**
  52.      * Creates a checkbox menu item with an empty label, initially set
  53.      * to off (false state).
  54.      */
  55.     public CheckboxMenuItem() {
  56.     this("", false);
  57.     }
  58.  
  59.     /**
  60.      * Creates the checkbox item with the specified label, initially
  61.      * set to off (false state).
  62.      * @param label the button label
  63.      */
  64.     public CheckboxMenuItem(String label) {
  65.     this(label, false);
  66.     }
  67.  
  68.     /**
  69.      * Creates a checkbox menu item with the specified label and state.
  70.      * @param label the button label
  71.      * @param state the initial state of the menu item:  true
  72.      * indicates "on"; false indicates "off".
  73.      */
  74.     public CheckboxMenuItem(String label, boolean state) {
  75.         super(label);
  76.     this.name = base + nameCounter++;
  77.         this.state = state;
  78.     }
  79.  
  80.     /**
  81.      * Creates the peer of the checkbox item.  This peer allows us to
  82.      * change the look of the checkbox item without changing its 
  83.      * functionality.
  84.      */
  85.     public void addNotify() {
  86.     peer = Toolkit.getDefaultToolkit().createCheckboxMenuItem(this);
  87.     super.addNotify();
  88.     }
  89.  
  90.     /**
  91.      * Returns the state of this MenuItem. This method is only valid for a 
  92.      * Checkbox.
  93.      */
  94.     public boolean getState() {
  95.     return state;
  96.     }
  97.  
  98.     /**
  99.      * Sets the state of this MenuItem if it is a Checkbox.
  100.      * @param t the specified state of the checkbox
  101.      */
  102.     public synchronized void setState(boolean b) {
  103.     state = b;
  104.     CheckboxMenuItemPeer peer = (CheckboxMenuItemPeer)this.peer;
  105.     if (peer != null) {
  106.         peer.setState(b);
  107.     }
  108.     }
  109.  
  110.     /**
  111.      * Returns the an array (length 1) containing the checkbox menu item
  112.      * label or null if the checkbox is not selected.
  113.      * @see ItemSelectable
  114.      */
  115.     public synchronized Object[] getSelectedObjects() {
  116.         if (state) {
  117.             Object[] items = new Object[1];
  118.             items[0] = label;
  119.             return items;
  120.         }
  121.         return null;
  122.     }
  123.  
  124.     /**
  125.      * Adds the specified item listener to recieve item events from
  126.      * this checkbox menu item.
  127.      * @param l the item listener
  128.      */ 
  129.     public synchronized void addItemListener(ItemListener l) {
  130.         itemListener = AWTEventMulticaster.add(itemListener, l);
  131.         newEventsOnly = true;
  132.     }
  133.  
  134.     /**
  135.      * Removes the specified item listener so it no longer receives
  136.      * item events from this checkbox menu item.
  137.      * @param l the item listener
  138.      */ 
  139.     public synchronized void removeItemListener(ItemListener l) {
  140.         itemListener = AWTEventMulticaster.remove(itemListener, l);
  141.     }
  142.  
  143.     // REMIND: remove when filtering is done at lower level
  144.     boolean eventEnabled(AWTEvent e) {
  145.         if (e.id == ItemEvent.ITEM_STATE_CHANGED) {
  146.             if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  147.                 itemListener != null) {
  148.                 return true;
  149.             } 
  150.             return false;
  151.         }
  152.         return super.eventEnabled(e);
  153.     }        
  154.  
  155.     /**
  156.      * Processes events on this checkbox menu item. If the event is
  157.      * an ItemEvent, it invokes the handleItemEvent method.
  158.      * NOTE: Checkbox menu items currently only support action and
  159.      * item events.
  160.      * @param e the event
  161.      */
  162.     protected void processEvent(AWTEvent e) {
  163.         if (e instanceof ItemEvent) {
  164.             processItemEvent((ItemEvent)e);
  165.         return;
  166.         }
  167.     super.processEvent(e);
  168.     }
  169.  
  170.     /** 
  171.      * Processes item events occurring on this checkbox menu item by
  172.      * dispatching them to any registered ItemListener objects.
  173.      * NOTE: This method will not be called unless item events
  174.      * are enabled for this checkbox menu item; this happens when one of the
  175.      * following occurs:
  176.      * a) An ItemListener object is registered via addItemListener()
  177.      * b) Item events are enabled via enableEvents()
  178.      * @see MenuItem#enableEvents
  179.      * @param e the item event
  180.      */  
  181.     protected void processItemEvent(ItemEvent e) {
  182.         if (itemListener != null) {
  183.             itemListener.itemStateChanged(e);
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Returns the parameter String of this button.
  189.      */
  190.     public String paramString() {
  191.     return super.paramString() + ",state=" + state;
  192.     }
  193.  
  194.     /* Serialization support. 
  195.      */
  196.  
  197.     private int checkboxMenuItemSerializedDataVersion = 1;
  198.  
  199.  
  200.     private void writeObject(ObjectOutputStream s)
  201.       throws java.io.IOException 
  202.     {
  203.       s.defaultWriteObject();
  204.  
  205.       AWTEventMulticaster.save(s, itemListenerK, itemListener);
  206.       s.writeObject(null);
  207.     }
  208.  
  209.  
  210.     private void readObject(ObjectInputStream s)
  211.       throws ClassNotFoundException, IOException 
  212.     {
  213.       s.defaultReadObject();
  214.  
  215.       Object keyOrNull;
  216.       while(null != (keyOrNull = s.readObject())) {
  217.     String key = ((String)keyOrNull).intern();
  218.  
  219.     if (itemListenerK == key) 
  220.       addItemListener((ItemListener)(s.readObject()));
  221.  
  222.     else // skip value for unrecognized key
  223.       s.readObject();
  224.       }
  225.     }
  226. }
  227.