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 / JRadioButtonMenuItem.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.7 KB  |  215 lines

  1. /*
  2.  * @(#)JRadioButtonMenuItem.java    1.17 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.  * An implementation of a RadioButtonMenuItem. A RadioButtonMenuItem is
  31.  * a menu item that is part of a group of menu items in which only one
  32.  * item in the group can be selected. The selected item displays its
  33.  * selected state. Selecting it causes any other selected item to
  34.  * switch to the unselected state.
  35.  * <p>
  36.  * Warning: serialized objects of this class will not be compatible with
  37.  * future swing releases.  The current serialization support is appropriate
  38.  * for short term storage or RMI between Swing1.0 applications.  It will
  39.  * not be possible to load serialized Swing1.0 objects with future releases
  40.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  41.  * baseline for the serialized form of Swing objects.
  42.  *
  43.  * @beaninfo
  44.  *   attribute: isContainer false
  45.  *
  46.  * @version 1.17 02/12/98
  47.  * @author Georges Saab
  48.  * @author David Karlton
  49.  */
  50. public class JRadioButtonMenuItem extends JMenuItem implements Accessible {
  51.     /**
  52.      * Creates a JRadioButtonMenuItem with no set text or icon.
  53.      */
  54.     public JRadioButtonMenuItem() {
  55.         this(null, null);
  56.     }
  57.  
  58.     /**
  59.      * Creates a JRadioButtonMenuItem with an icon.
  60.      *
  61.      * @param icon the Icon to display on the RadioButtonMenuItem.
  62.      */
  63.     public JRadioButtonMenuItem(Icon icon) {
  64.         this(null, icon);
  65.     }
  66.  
  67.     /**
  68.      * Creates a JRadioButtonMenuItem with text.
  69.      *
  70.      * @param text the text of the RadioButtonMenuItem.
  71.      */
  72.     public JRadioButtonMenuItem(String text) {
  73.         this(text, null);
  74.     }
  75.     
  76.     /**
  77.      * Creates a JRadioButtonMenuItem with the specified text
  78.      * and Icon.
  79.      *
  80.      * @param text the text of the RadioButtonMenuItem
  81.      * @param icon the icon to display on the RadioButtonMenuItem
  82.      */
  83.     public JRadioButtonMenuItem(String text, Icon icon) {
  84.         setModel(new JToggleButton.ToggleButtonModel());
  85.         init(text, icon);
  86.         setBorderPainted(false);
  87.         setFocusPainted(false);
  88.         setHorizontalTextPosition(JButton.RIGHT);
  89.         setHorizontalAlignment(JButton.LEFT);
  90.         // setArmedPainted(false);
  91.         updateUI();
  92.     }
  93.  
  94.     /**
  95.      * Initialize the JRadioButtonMenuItem with the specified text
  96.      * and Icon.
  97.      *
  98.      * @param text the text to display
  99.      * @param icon the icon to display
  100.      */
  101.     protected void init(String text, Icon icon) {
  102.         setLayout(new OverlayLayout(this));
  103.  
  104.         if(text != null) {
  105.             setText(text);
  106.             
  107.             if(icon != null) {
  108.                 setVerticalTextPosition(BOTTOM);
  109.             } 
  110.         }
  111.         
  112.         if(icon != null) {
  113.             setIcon(icon);
  114.         }
  115.         
  116.         // Listen for Focus events
  117.         addFocusListener(
  118.             new FocusListener() {
  119.             public void focusGained(FocusEvent event) {}
  120.             public void focusLost(FocusEvent event) {
  121.                 // When focus is lost, repaint if 
  122.                 // we focus information is painted
  123.                 if(isFocusPainted()) {
  124.                     repaint();
  125.                 }
  126.             }
  127.         }
  128.         );
  129.     }
  130.     
  131.     /**
  132.      * Sets the L&F object that renders this component.
  133.      *
  134.      * @param ui the RadioButtonMenuItemUI L&F object
  135.      * @see UIDefaults#getUI
  136.      * @beaninfo
  137.      * description: The menu item's UI delegate
  138.      *       bound: true
  139.      *      expert: true
  140.      *      hidden: true
  141.      */
  142.     public void setUI(RadioButtonMenuItemUI ui) {
  143.         super.setUI(ui);
  144.     }
  145.     
  146.     /**
  147.      * Notification from the UIFactory that the L&F has changed. 
  148.      * Called to replace the UI with the latest version from the 
  149.      * UIFactory.
  150.      *
  151.      * @see JComponent#updateUI
  152.      */
  153.     public void updateUI() {
  154.         setUI((RadioButtonMenuItemUI)UIManager.getUI(this));
  155.     }
  156.  
  157.  
  158.     /**
  159.      * Returns the name of the L&F class that renders this component.
  160.      *
  161.      * @return "RadioButtonMenuItemUI"
  162.      * @see JComponent#getUIClassID
  163.      * @see UIDefaults#getUI
  164.      */
  165.     public String getUIClassID() {
  166.         return "RadioButtonMenuItemUI";
  167.     }
  168.  
  169.  
  170.     /*
  171.      * Override Component.requestFocus() to not grab focus.
  172.      */
  173.     public void requestFocus() {}
  174.  
  175.  
  176. /////////////////                                                 
  177. // Accessibility support
  178. ////////////////
  179.  
  180.     /**
  181.      * Get the AccessibleContext associated with this JComponent
  182.      *
  183.      * @return the AccessibleContext of this JComponent
  184.      */
  185.     public AccessibleContext getAccessibleContext() {
  186.         if (accessibleContext == null) {
  187.             accessibleContext = new AccessibleJRadioButtonMenuItem();
  188.         }
  189.         return accessibleContext;
  190.     }
  191.  
  192.     /**
  193.      * The class used to obtain the accessible role for this object.
  194.      * <p>
  195.      * Warning: serialized objects of this class will not be compatible with
  196.      * future swing releases.  The current serialization support is appropriate
  197.      * for short term storage or RMI between Swing1.0 applications.  It will
  198.      * not be possible to load serialized Swing1.0 objects with future releases
  199.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  200.      * baseline for the serialized form of Swing objects.
  201.      */
  202.     protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem {
  203.         /**
  204.          * Get the role of this object.
  205.          *
  206.          * @return an instance of AccessibleRole describing the role of the 
  207.          * object
  208.          */
  209.         public AccessibleRole getAccessibleRole() {
  210.             return AccessibleRole.RADIO_BUTTON;
  211.         }
  212.     } // inner class AccessibleJRadioButtonMenuItem
  213. }
  214.  
  215.