home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / JFC.bin / JCheckBoxMenuItem.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  285 lines

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