001/* JRadioButton.java -- 
002   Copyright (C) 2002, 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.swing;
040
041import javax.accessibility.AccessibleContext;
042import javax.accessibility.AccessibleRole;
043import javax.swing.plaf.ButtonUI;
044
045/**
046 * The <code>JRadioButton</code> component provides a visually selectable
047 * button with mutually exclusive behaviour within a <code>ButtonGroup</code>.
048 * A series of radio buttons can be used to provide options to the user,
049 * where the user can only select one of the available options.  The state
050 * of the button is provided by the superclass, <code>JToggleButton</code>.
051 * <code>JRadioButton</code> adds the additional behaviour, that if two
052 * or more radio buttons are grouped together, the selection of one implies
053 * the deselection of the other buttons within the group.
054 * <p>
055 *
056 * Buttons are grouped by adding each instance to a <code>ButtonGroup</code>.
057 * The existence of such a grouping is not reflected visually, so other means
058 * should be used to denote this.  For instance, the grouped buttons can be placed
059 * within the same panel, possibly with an appropriate border to denote
060 * the connection between the components.
061 *
062 * @author Michael Koch  (konqueror@gmx.de)
063 * @author Graydon Hoare  (graydon@redhat.com)
064 * @author Andrew John Hughes  (gnu_andrew@member.fsf.org)
065 * @see JToggleButton
066 * @see ButtonGroup
067 * @since 1.2
068 */
069public class JRadioButton extends JToggleButton
070{
071  /**
072   * Compatible with Sun's JDK.
073   */
074  private static final long serialVersionUID = 7751949583255506856L;
075
076  /**
077   * This class provides accessibility support for the toggle button.
078   */
079  protected class AccessibleJRadioButton
080    extends AccessibleJToggleButton
081  {
082    private static final long serialVersionUID = 4850967637026120674L;
083
084    /**
085     * Constructor for the accessible toggle button.
086     */
087    protected AccessibleJRadioButton()
088    {
089        /* Call the superclass to register for events */
090        super();
091    }
092
093    /**
094     * Returns the accessible role for the toggle button.
095     *
096     * @return An instance of <code>AccessibleRole</code>, describing
097     *         the role of the toggle button.
098     */
099    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
259