JRadioButtonMenuItem.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* JRadioButtonMenuItem.java --
  2. Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import javax.accessibility.Accessible;
  33. import javax.accessibility.AccessibleContext;
  34. import javax.accessibility.AccessibleRole;
  35. /**
  36. * This class represents JRadioButtonMenuItem. Its behaviour is very similar
  37. * to JRadioButton. Just like JRadioButton, user can check and uncheck this
  38. * menu item by clicking on it. JRadioButtonMenuItem uses ToggleButtonModel
  39. * to keep track of its selection. If the JRadioButtonMenuItem is included in
  40. * the button group, then only one JRadioButtonMenuItem can be selected at
  41. * one time.
  42. */
  43. public class JRadioButtonMenuItem extends JMenuItem implements Accessible
  44. {
  45. private static final long serialVersionUID = 8482658191548521743L;
  46. /** name for the UI delegate for this radio button menu item. */
  47. private static final String uiClassID = "RadioButtonMenuItemUI";
  48. /**
  49. * Creates a new JRadioButtonMenuItem object.
  50. */
  51. public JRadioButtonMenuItem()
  52. {
  53. this(null, null);
  54. }
  55. /**
  56. * Creates a new JRadioButtonMenuItem with specified icon
  57. *
  58. * @param icon Icon to be used for this menu item
  59. */
  60. public JRadioButtonMenuItem(Icon icon)
  61. {
  62. this(null, icon);
  63. }
  64. /**
  65. * Creates a new JRadioButtonMenuItem with specified label
  66. *
  67. * @param text Label for this menu item
  68. */
  69. public JRadioButtonMenuItem(String text)
  70. {
  71. this(text, null);
  72. }
  73. /**
  74. * Creates a new JRadioButtonMenuItem using specified action
  75. *
  76. * @param action Action for this menu item
  77. */
  78. public JRadioButtonMenuItem(Action action)
  79. {
  80. this();
  81. setAction(action);
  82. }
  83. /**
  84. * Creates a new JRadioButtonMenuItem with specified label and icon
  85. *
  86. * @param text Label for this menu item
  87. * @param icon Icon for this menu item
  88. */
  89. public JRadioButtonMenuItem(String text, Icon icon)
  90. {
  91. this(text, icon, false);
  92. }
  93. /**
  94. * Creates a new JRadioButtonMenuItem with specified label
  95. * and marked selected if 'selected' is true.
  96. *
  97. * @param text Text for this menu item
  98. * @param selected Selected state of this menu item
  99. */
  100. public JRadioButtonMenuItem(String text, boolean selected)
  101. {
  102. this(text, null, selected);
  103. }
  104. /**
  105. * Creates a new JRadioButtonMenuItem with specified icon
  106. * and given selected state
  107. *
  108. * @param icon Icon for this menu item
  109. * @param selected Selected state for this menu item
  110. */
  111. public JRadioButtonMenuItem(Icon icon, boolean selected)
  112. {
  113. this(null, icon, selected);
  114. }
  115. /**
  116. * Creates a new JRadioButtonMenuItem with specified label,
  117. * icon and selected state.
  118. *
  119. * @param text Label for this menu item
  120. * @param icon Icon to be use for this menu item
  121. * @param selected selected state of this menu item
  122. */
  123. public JRadioButtonMenuItem(String text, Icon icon, boolean selected)
  124. {
  125. super(text, icon);
  126. setModel(new JToggleButton.ToggleButtonModel());
  127. model.setSelected(selected);
  128. setFocusable(false);
  129. }
  130. /**
  131. * This method returns a name to identify which look and feel class will be
  132. * the UI delegate for the menuItem.
  133. *
  134. * @return The Look and Feel classID. "JRadioButtonMenuItemUI"
  135. */
  136. public String getUIClassID()
  137. {
  138. return uiClassID;
  139. }
  140. /**
  141. * This method overrides JComponent.requestFocus with an empty
  142. * implementation, since JRadioButtonMenuItems should not
  143. * receve focus in general.
  144. */
  145. public void requestFocus()
  146. {
  147. // Should do nothing here
  148. }
  149. /**
  150. * Returns a string describing the attributes for the
  151. * <code>JRadioButtonMenuItem</code> component, for use in debugging. The
  152. * return value is guaranteed to be non-<code>null</code>, but the format of
  153. * the string may vary between implementations.
  154. *
  155. * @return A string describing the attributes of the
  156. * <code>JRadioButtonMenuItem</code>.
  157. */
  158. protected String paramString()
  159. {
  160. // calling super seems to be sufficient here...
  161. return super.paramString();
  162. }
  163. /**
  164. * Returns the object that provides accessibility features for this
  165. * <code>JRadioButtonMenuItem</code> component.
  166. *
  167. * @return The accessible context (an instance of
  168. * {@link AccessibleJRadioButtonMenuItem}).
  169. */
  170. public AccessibleContext getAccessibleContext()
  171. {
  172. if (accessibleContext == null)
  173. accessibleContext = new AccessibleJRadioButtonMenuItem();
  174. return accessibleContext;
  175. }
  176. /**
  177. * Provides the accessibility features for the
  178. * <code>JRadioButtonMenuItem</code> component.
  179. *
  180. * @see JRadioButtonMenuItem#getAccessibleContext()
  181. */
  182. protected class AccessibleJRadioButtonMenuItem extends AccessibleJMenuItem
  183. {
  184. private static final long serialVersionUID = 4381471510145292179L;
  185. /**
  186. * Creates a new <code>AccessibleJRadioButtonMenuItem</code> instance.
  187. */
  188. protected AccessibleJRadioButtonMenuItem()
  189. {
  190. // Nothing to do here.
  191. }
  192. /**
  193. * Returns the accessible role for the <code>JRadioButtonMenuItem</code>
  194. * component.
  195. *
  196. * @return {@link AccessibleRole#RADIO_BUTTON}.
  197. */
  198. public AccessibleRole getAccessibleRole()
  199. {
  200. return AccessibleRole.RADIO_BUTTON;
  201. }
  202. }
  203. }