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 / JToggleButton.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.4 KB  |  294 lines

  1. /*
  2.  * @(#)JToggleButton.java    1.26 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 disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19. package java.awt.swing;
  20.  
  21. import java.awt.*;
  22. import java.awt.event.*;
  23.  
  24. import java.awt.swing.event.*;
  25. import java.awt.swing.plaf.*;
  26. import java.awt.accessibility.*;
  27.  
  28. /**
  29.  * An implementation of a two-state button.  
  30.  * The <code>JRadioButton</code> and <code>JCheckbox</code> classes
  31.  * are subclasses of this class.
  32.  * <p>
  33.  * Warning: serialized objects of this class will not be compatible with
  34.  * future swing releases.  The current serialization support is appropriate
  35.  * for short term storage or RMI between Swing1.0 applications.  It will
  36.  * not be possible to load serialized Swing1.0 objects with future releases
  37.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  38.  * baseline for the serialized form of Swing objects.
  39.  *
  40.  * @beaninfo
  41.  *   attribute: isContainer false
  42.  * @see JRadioButton
  43.  * @see JCheckbox
  44.  * @version 1.26 02/12/98
  45.  * @author Jeff Dinkins
  46.  */
  47. public class JToggleButton extends AbstractButton implements Accessible {
  48.  
  49.     /**
  50.      * Creates an initially unselected toggle button
  51.      * without setting the text or image.
  52.      */
  53.     public JToggleButton () {
  54.     this(null, null, false);
  55.     }
  56.  
  57.     /**
  58.      * Creates an initially unselected toggle button
  59.      * with the specified image but no text.
  60.      *
  61.      * @param icon  the image that the button should display
  62.      */
  63.     public JToggleButton(Icon icon) {
  64.     this(null, icon, false);
  65.     }
  66.     
  67.     /**
  68.      * Creates a toggle button with the specified image 
  69.      * and selection state, but no text.
  70.      *
  71.      * @param icon  the image that the button should display
  72.      * @param selected  if true, the button is initially selected;
  73.      *                  otherwise, the button is initially unselected
  74.      */
  75.     public JToggleButton(Icon icon, boolean selected) {
  76.     this(null, icon, selected);
  77.     }
  78.     
  79.     /**
  80.      * Creates an unselected toggle button with the specified text.
  81.      *
  82.      * @param text  the string displayed on the toggle button
  83.      */
  84.     public JToggleButton (String text) {
  85.     this(text, null, false);
  86.     }
  87.  
  88.     /**
  89.      * Creates a toggle button with the specified text
  90.      * and selection state.
  91.      *
  92.      * @param text  the string displayed on the toggle button
  93.      * @param selected  if true, the button is initially selected;
  94.      *                  otherwise, the button is initially unselected
  95.      */
  96.     public JToggleButton (String text, boolean selected) {
  97.     this(text, null, selected);
  98.     }
  99.  
  100.     /**
  101.      * Creates a toggle button that has the specified text and image,
  102.      * and that is initially unselected.
  103.      *
  104.      * @param text the string displayed on the button
  105.      * @param icon  the image that the button should display
  106.      */
  107.     public JToggleButton(String text, Icon icon) {
  108.     this(text, icon, false);
  109.     }
  110.  
  111.     /**
  112.      * Creates a toggle button with the specified text, image, and
  113.      * selection state.
  114.      *
  115.      * @param text the text of the toggle button.
  116.      * @param selected  if true, the button is initially selected;
  117.      *                  otherwise, the button is initially unselected
  118.      */
  119.     public JToggleButton (String text, Icon icon, boolean selected) {
  120.     // Create the model
  121.     setModel(new ToggleButtonModel());
  122.  
  123.     model.setSelected(selected);
  124.  
  125.     // initialize
  126.     init(text, icon);
  127.     }
  128.  
  129.     /**
  130.      * Notification from the UIFactory that the L&F
  131.      * has changed. 
  132.      *
  133.      * @see JComponent#updateUI
  134.      */
  135.     public void updateUI() {
  136.         setUI((ButtonUI)UIManager.getUI(this));
  137.     }
  138.     
  139.     /**
  140.      * Returns a string that specifies the name of the l&f class
  141.      * that renders this component.
  142.      *
  143.      * @return String "ToggleButtonUI"
  144.      * @see JComponent#getUIClassID
  145.      * @see UIDefaults#getUI
  146.      * @beaninfo
  147.      *  description: A string that specifies the name of the L&F class
  148.      */
  149.     public String getUIClassID() {
  150.     return "ToggleButtonUI";
  151.     }
  152.  
  153.  
  154.  
  155.  
  156.     // *********************************************************************
  157.  
  158.     /**
  159.      * The ToggleButton model
  160.      * <p>
  161.      * Warning: serialized objects of this class will not be compatible with
  162.      * future swing releases.  The current serialization support is appropriate
  163.      * for short term storage or RMI between Swing1.0 applications.  It will
  164.      * not be possible to load serialized Swing1.0 objects with future releases
  165.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  166.      * baseline for the serialized form of Swing objects.
  167.      */
  168.     public static class ToggleButtonModel extends DefaultButtonModel {
  169.  
  170.     /**
  171.      * Creates a new ToggleButton Model
  172.      */
  173.     public ToggleButtonModel () {
  174.     }
  175.  
  176.     /**
  177.      * Checks if the button is selected.
  178.      */
  179.     public boolean isSelected() {
  180.         if(group != null) {
  181.         return group.isSelected(this);
  182.         } else {
  183.         return (stateMask & SELECTED) != 0;
  184.         }
  185.     }
  186.  
  187.  
  188.     /**
  189.      * Sets the selected state of the button.
  190.      * @param b true selects the toggle button,
  191.      *          false deselects the toggle button.
  192.      */
  193.     public void setSelected(boolean b) {
  194.         // if (this.isSelected() == b) {
  195.         // return;
  196.         // }
  197.         
  198.         if(group != null) {
  199.         // use the group model instead
  200.         group.setSelected(this, b);
  201.         } else {
  202.         if (b) {
  203.             stateMask |= SELECTED;
  204.         } else {
  205.             stateMask &= ~SELECTED;
  206.         }
  207.         }
  208.  
  209.         // Send ChangeEvent
  210.         fireStateChanged();
  211.  
  212.         // Send ItemEvent
  213.         fireItemStateChanged(
  214.             new ItemEvent(this,
  215.                   ItemEvent.ITEM_STATE_CHANGED,
  216.                   this,
  217.                   this.isSelected() ?  ItemEvent.SELECTED : ItemEvent.DESELECTED));
  218.     
  219.     }
  220.  
  221.     /**
  222.      * Sets the pressed state of the toggle button.
  223.      */ 
  224.     public void setPressed(boolean b) {
  225.         if ((isPressed() == b) || !isEnabled()) {
  226.         return;
  227.         }
  228.  
  229.         if (b == false && isArmed()) {
  230.         setSelected(!this.isSelected());
  231.         } 
  232.  
  233.         if (b) {
  234.         stateMask |= PRESSED;
  235.         } else {
  236.         stateMask &= ~PRESSED;
  237.         }
  238.  
  239.         fireStateChanged();
  240.  
  241.         if(!isPressed() && isArmed()) {
  242.         fireActionPerformed(
  243.             new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
  244.                     getActionCommand())
  245.             );
  246.         }
  247.  
  248.     }
  249.     }
  250.  
  251. /////////////////
  252. // Accessibility support
  253. ////////////////
  254.  
  255.     /**
  256.      * Get the AccessibleContext associated with this JComponent
  257.      *
  258.      * @return the AccessibleContext of this JComponent
  259.      * @beaninfo
  260.      *       expert: true
  261.      *  description: The AccessibleContext associated with this ToggleButton.
  262.      */
  263.     public AccessibleContext getAccessibleContext() {
  264.     if (accessibleContext == null) {
  265.         accessibleContext = new AccessibleJToggleButton();
  266.     }
  267.     return accessibleContext;
  268.     }
  269.  
  270.     /**
  271.      * The class used to obtain the accessible role for this object.
  272.      * <p>
  273.      * Warning: serialized objects of this class will not be compatible with
  274.      * future swing releases.  The current serialization support is appropriate
  275.      * for short term storage or RMI between Swing1.0 applications.  It will
  276.      * not be possible to load serialized Swing1.0 objects with future releases
  277.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  278.      * baseline for the serialized form of Swing objects.
  279.      */
  280.     protected class AccessibleJToggleButton extends AccessibleAbstractButton {
  281.  
  282.         /**
  283.          * Get the role of this object.
  284.          *
  285.          * @return an instance of AccessibleRole describing the role of the 
  286.      * object
  287.          */
  288.         public AccessibleRole getAccessibleRole() {
  289.             return AccessibleRole.TOGGLE_BUTTON;
  290.         }
  291.     } // inner class AccessibleJToggleButton
  292. }
  293.   
  294.