Source for javax.swing.JRadioButton

   1: /* JRadioButton.java -- 
   2:    Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import javax.accessibility.AccessibleContext;
  42: import javax.accessibility.AccessibleRole;
  43: import javax.swing.plaf.ButtonUI;
  44: 
  45: /**
  46:  * The <code>JRadioButton</code> component provides a visually selectable
  47:  * button with mutually exclusive behaviour within a <code>ButtonGroup</code>.
  48:  * A series of radio buttons can be used to provide options to the user,
  49:  * where the user can only select one of the available options.  The state
  50:  * of the button is provided by the superclass, <code>JToggleButton</code>.
  51:  * <code>JRadioButton</code> adds the additional behaviour, that if two
  52:  * or more radio buttons are grouped together, the selection of one implies
  53:  * the deselection of the other buttons within the group.
  54:  * <p>
  55:  *
  56:  * Buttons are grouped by adding each instance to a <code>ButtonGroup</code>.
  57:  * The existence of such a grouping is not reflected visually, so other means
  58:  * should be used to denote this.  For instance, the grouped buttons can be placed
  59:  * within the same panel, possibly with an appropriate border to denote
  60:  * the connection between the components.
  61:  *
  62:  * @author Michael Koch  (konqueror@gmx.de)
  63:  * @author Graydon Hoare  (graydon@redhat.com)
  64:  * @author Andrew John Hughes  (gnu_andrew@member.fsf.org)
  65:  * @see JToggleButton
  66:  * @see ButtonGroup
  67:  * @since 1.2
  68:  */
  69: public class JRadioButton extends JToggleButton
  70: {
  71:   /**
  72:    * Compatible with Sun's JDK.
  73:    */
  74:   private static final long serialVersionUID = 7751949583255506856L;
  75: 
  76:   /**
  77:    * This class provides accessibility support for the toggle button.
  78:    */
  79:   protected class AccessibleJRadioButton
  80:     extends AccessibleJToggleButton
  81:   {
  82:     private static final long serialVersionUID = 4850967637026120674L;
  83: 
  84:     /**
  85:      * Constructor for the accessible toggle button.
  86:      */
  87:     protected AccessibleJRadioButton()
  88:     {
  89:         /* Call the superclass to register for events */
  90:         super();
  91:     }
  92: 
  93:     /**
  94:      * Returns the accessible role for the toggle button.
  95:      *
  96:      * @return An instance of <code>AccessibleRole</code>, describing
  97:      *         the role of the toggle button.
  98:      */
  99:     public AccessibleRole getAccessibleRole()
 100:     {
 101:       return AccessibleRole.RADIO_BUTTON;
 102:     }
 103: 
 104:   }
 105: 
 106:   /**
 107:    * Constructs an unselected radio button with no text or icon.
 108:    */ 
 109:   public JRadioButton()
 110:   {
 111:     this(null, null, false);
 112:   }
 113:     
 114:   /**
 115:    * Constructs a radio button using the labelling, state
 116:    * and icon specified by the supplied action.
 117:    *
 118:    * @param a the action to use to define the properties of the button.
 119:    */
 120:   public JRadioButton(Action a)
 121:   {
 122:     this();
 123:     setAction(a);
 124:   }
 125: 
 126:   /**
 127:    * Constructs an unselected radio button with the supplied icon
 128:    * and no text.
 129:    *
 130:    * @param icon the icon to use.
 131:    */
 132:   public JRadioButton(Icon icon)
 133:   { 
 134:     this(null, icon, false);
 135:   }    
 136:   
 137:   /**
 138:    * Constructs a radio button with the supplied icon and state.
 139:    *
 140:    * @param icon the icon to use.
 141:    * @param selected if true, the radio button is initially in the
 142:    *        selected state.  Otherwise, the button is unselected.
 143:    */
 144:   public JRadioButton(Icon icon, boolean selected)
 145:   { 
 146:     this(null, icon, selected);
 147:   }    
 148:   
 149:   /**
 150:    * Constructs an unselected radio button using the supplied text
 151:    * and no icon.
 152:    *
 153:    * @param text the text to use.
 154:    */
 155:   public JRadioButton(String text)
 156:   {
 157:     this(text, null, false);
 158:   }
 159: 
 160:   /**
 161:    * Constructs a radio button with the supplied text and state.
 162:    *
 163:    * @param text the text to use.
 164:    * @param selected if true, the radio button is initially in the
 165:    *        selected state.  Otherwise, the button is unselected.
 166:    */
 167:   public JRadioButton(String text, boolean selected)
 168:   {
 169:     this(text, null, selected);
 170:   }
 171:       
 172:   /**
 173:    * Constructs an unselected radio button with the supplied text
 174:    * and icon.
 175:    *
 176:    * @param text the text to use.
 177:    * @param icon the icon to use.
 178:    */
 179:   public JRadioButton(String text, Icon icon)
 180:   {
 181:     this(text, icon, false);
 182:   }
 183:   
 184:   /**
 185:    * Constructs a radio button with the supplied text, icon and state.
 186:    *
 187:    * @param text the text to use.
 188:    * @param icon the icon to use.
 189:    * @param selected if true, the radio button is initially in the
 190:    *        selected state.  Otherwise, the button is unselected.
 191:    */
 192:   public JRadioButton(String text, Icon icon, boolean selected)
 193:   {
 194:     super(text, icon, selected);
 195:     setBorderPainted(false);
 196:     setHorizontalAlignment(LEADING);
 197:   }
 198:       
 199:   /**
 200:    * Returns the accessible context for this <code>JRadioButton</code>,
 201:    * in the form of an instance of <code>AccessibleJRadioButton</code>.
 202:    * The context is created, if necessary.
 203:    *
 204:    * @return the associated context
 205:    */
 206:   public AccessibleContext getAccessibleContext()
 207:   {
 208:     /* Create the context if this is the first request */
 209:     if (accessibleContext == null)
 210:       {
 211:         /* Create the context */
 212:     accessibleContext = new AccessibleJRadioButton();
 213:       }
 214:     return accessibleContext;
 215:   }
 216:   
 217:   /**
 218:    * Returns a string specifying the name of the Look and Feel UI class
 219:    * that renders this component.
 220:    *
 221:    * @return the Look and Feel UI class for <code>JRadioButton</code>s
 222:    *         as a <code>String</code>.
 223:    */  
 224:   public String getUIClassID()
 225:   {
 226:     return "RadioButtonUI";
 227:   }
 228:   
 229:   /**
 230:    * Returns a string representation of this component for debugging use.
 231:    * Users should not depend on anything as regards the content or formatting
 232:    * of this string, except for the fact that the returned string may never be
 233:    * null (only empty).
 234:    *
 235:    * @return the component in <code>String</code> form for debugging.
 236:    */  
 237:   protected  String paramString()
 238:   {
 239:     return super.paramString();
 240:   }
 241:   
 242:   /**
 243:    * This method resets the radio button's UI delegate to the default UI for
 244:    * the current look and feel.
 245:    */
 246:   public void updateUI()
 247:   {
 248:     /* 
 249:        I can't see any difference between this and the superclass one,
 250:        but Sun reimplements it... there is no RadioButtonUI class for it
 251:        to be cast to.
 252:     */
 253:     setUI((ButtonUI) UIManager.getUI(this));
 254:   }
 255: 
 256: }
 257: 
 258: