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

  1. /*
  2.  * @(#)JCheckBoxMenuItem.java    1.25 98/02/12
  3.  * 
  4.  * Copyright (c) 1997 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.  */
  20. package java.awt.swing;
  21.  
  22. import java.util.EventListener;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import java.awt.image.*;
  26. import java.awt.swing.plaf.*;
  27. import java.awt.accessibility.*;
  28.  
  29.  
  30. /**
  31.  * A menu item that can be selected or deselected. If selected, the menu
  32.  * item typically appears with a checkmark next to it. If unselected or
  33.  * deselected, the menu item appears without a checkmark. Like a regular
  34.  * menu item, a checkbox menu item can have either text or a graphic
  35.  * icon associated with it, or both.
  36.  * <p>
  37.  * Either <code>getSelected</code>/<code>setSelected</code> or 
  38.  * <code>getState</code>/<code>setState</code> can be used
  39.  * to determine/specify the menu item's selection state. (The
  40.  * Swing-standard methods are <code>getSelected</code> and
  41.  * <code>setSelected</code>. These methods work for all menus and buttons.
  42.  * The <code>getState</code> and <code>setState</code> methods exist for
  43.  * compatibility with other component sets.)
  44.  * <p>
  45.  * Warning: serialized objects of this class will not be compatible with
  46.  * future swing releases.  The current serialization support is appropriate 
  47.  * for short term storage or RMI between Swing1.0 applications.  It will
  48.  * not be possible to load serialized Swing1.0 objects with future releases
  49.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  50.  * baseline for the serialized form of Swing objects.
  51.  *
  52.  * @beaninfo
  53.  *   attribute: isContainer false
  54.  *
  55.  * @version 1.25 02/12/98
  56.  * @author Georges Saab
  57.  * @author David Karlton
  58.  */
  59. public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  60.         Accessible {
  61.  
  62.     /**
  63.      * Creates an initially unselected checkboxMenuItem with no set text or icon.
  64.      */
  65.     public JCheckBoxMenuItem() {
  66.         this(null, null);
  67.     }
  68.  
  69.     /**
  70.      * Creates an initially unselected checkboxMenuItem with an icon.
  71.      *
  72.      * @param icon the icon of the CheckBoxMenuItem.
  73.      */
  74.     public JCheckBoxMenuItem(Icon icon) {
  75.         this(null, icon);
  76.     }
  77.  
  78.     /**
  79.      * Creates an initially unselected checkboxMenuItem with text.
  80.      *
  81.      * @param text the text of the CheckBoxMenuItem
  82.      */
  83.     public JCheckBoxMenuItem(String text) {
  84.         this(text, null);
  85.     }
  86.     
  87.     /**
  88.      * Creates an initially unselected checkboxMenuItem with the specified text and icon.
  89.      *
  90.      * @param text the text of the CheckBoxMenuItem
  91.      * @param icon the icon of the CheckBoxMenuItem
  92.      */
  93.     public JCheckBoxMenuItem(String text, Icon icon) {
  94.         setModel(new JToggleButton.ToggleButtonModel());
  95.         init(text, icon);
  96.         setBorderPainted(false);
  97.         setFocusPainted(false);
  98.         setHorizontalTextPosition(RIGHT);
  99.         setHorizontalAlignment(LEFT);
  100.         // setArmedPainted(false);
  101.         updateUI();
  102.     }
  103.  
  104.     /**
  105.      * Creates a checkboxMenuItem with the specified text and selection state.
  106.      *
  107.      * @param text the text of the CheckBoxMenuItem.
  108.      * @param b the selected state of the checkboxmenuitem
  109.      */
  110.     public JCheckBoxMenuItem(String text, boolean b) {
  111.         this(text);
  112.         setSelected(b);
  113.     }
  114.  
  115.     /**
  116.      * Creates a checkboxMenuItem with the specified text, icon, and selection state.
  117.      *
  118.      * @param text the text of the CheckBoxMenuItem
  119.      * @param icon the icon of the CheckBoxMenuItem
  120.      * @param b the selected state of the checkboxmenuitem
  121.      */
  122.     public JCheckBoxMenuItem(String text, Icon icon, boolean b) {
  123.         this(text, icon);
  124.         setSelected(b);
  125.     }
  126.  
  127.     protected void init(String text, Icon icon) {
  128.         setLayout(new OverlayLayout(this));
  129.  
  130.         if(text != null) {
  131.             setText(text);
  132.             
  133.             if(icon != null) {
  134.                 setVerticalTextPosition(BOTTOM);
  135.             } 
  136.         }
  137.         
  138.         if(icon != null) {
  139.             setIcon(icon);
  140.         }
  141.         
  142.         // Listen for Focus events
  143.         addFocusListener(
  144.             new FocusListener() {
  145.             public void focusGained(FocusEvent event) {}
  146.             public void focusLost(FocusEvent event) {
  147.                 // When focus is lost, repaint if 
  148.                 // we focus information is painted
  149.                 if(isFocusPainted()) {
  150.                     repaint();
  151.                 }
  152.             }
  153.         }
  154.         );
  155.     }
  156.     
  157.     /**
  158.      * Sets L&F object that renders this component.
  159.      *
  160.      * @param ui the new L&F object CheckBoxMenuItemUI
  161.      * @see UIDefaults#getUI
  162.      * @beaninfo
  163.      * description: The menu item's UI delegate
  164.      *       bound: true
  165.      *      expert: true
  166.      *      hidden: true
  167.      */
  168.     public void setUI(CheckBoxMenuItemUI ui) {
  169.         super.setUI(ui);
  170.     }
  171.     
  172.     /**
  173.      * Notification from the UIFactory that the L&F
  174.      * has changed. 
  175.      *
  176.      * @see JComponent#updateUI
  177.      */
  178.     public void updateUI() {
  179.         setUI((CheckBoxMenuItemUI)UIManager.getUI(this));
  180.     }
  181.  
  182.  
  183.     /**
  184.      * Returns the name of the L&F class
  185.      * that renders this component.
  186.      *
  187.      * @return "CheckBoxMenuItemUI"
  188.      * @see JComponent#getUIClassID
  189.      * @see UIDefaults#getUI
  190.      */
  191.     public String getUIClassID() {
  192.         return "CheckBoxMenuItemUI";
  193.     }
  194.         
  195.      /**
  196.       * Returns the selected-state of the item. This method
  197.       * exists for AWT compatibility only.  New code should
  198.       * use isSelected() instead.
  199.       *
  200.       * @return true  if the item is selected
  201.       */
  202.     public boolean getState() {
  203.     return isSelected();
  204.     }
  205.         
  206.     /**
  207.      * Sets the selected-state of the item. This method
  208.      * exists for AWT compatibility only.  New code should
  209.      * use setSelected() instead.
  210.      *
  211.      * @param b  a boolean value indicating the item's
  212.      *           selected-state, where true=selected
  213.      * @beaninfo
  214.      * description: The selection state of the Checkbox menu item
  215.      *      hidden: true
  216.      */
  217.     public synchronized void setState(boolean b) {
  218.     setSelected(b);
  219.     }
  220.         
  221.         
  222.     /**
  223.      * Returns an array (length 1) containing the checkbox menu item 
  224.      * label or null if the checkbox is not selected.
  225.      *
  226.      * @return an array containing 1 Object -- the text of the menu item
  227.      *         -- if the item is selected, otherwise null 
  228.      */
  229.     public synchronized Object[] getSelectedObjects() {
  230.         if (isSelected() == false)
  231.             return null;
  232.         Object[] selectedObjects = new Object[1];
  233.         selectedObjects[0] = getText();
  234.         return selectedObjects;
  235.     }
  236.  
  237.     /*
  238.      * Override Component.requestFocus() to prevent grabbing the focus.
  239.      */
  240.     public void requestFocus() {}
  241.  
  242.  
  243. /////////////////
  244. // Accessibility support
  245. ////////////////
  246.  
  247.     /**
  248.      * Get the AccessibleContext associated with this JComponent
  249.      *
  250.      * @return the AccessibleContext of this JComponent
  251.      */
  252.     public AccessibleContext getAccessibleContext() {
  253.         if (accessibleContext == null) {
  254.             accessibleContext = new AccessibleJCheckBoxMenuItem();
  255.         }
  256.         return accessibleContext;
  257.     }
  258.  
  259.     /**
  260.      * The class used to obtain the accessible role for this object.
  261.      * <p>
  262.      * Warning: serialized objects of this class will not be compatible with
  263.      * future swing releases.  The current serialization support is appropriate
  264.      * for short term storage or RMI between Swing1.0 applications.  It will
  265.      * not be possible to load serialized Swing1.0 objects with future releases
  266.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  267.      * baseline for the serialized form of Swing objects.
  268.      */
  269.     protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem {
  270.         /**
  271.          * Get the role of this object.
  272.          *
  273.          * @return an instance of AccessibleRole describing the role of the 
  274.          * object
  275.          */
  276.         public AccessibleRole getAccessibleRole() {
  277.             return AccessibleRole.CHECK_BOX;
  278.         }
  279.     } // inner class AccessibleJCheckBoxMenuItem
  280. }
  281.