1 /* 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.awt.*; 28 import java.awt.event.*; 29 import java.beans.JavaBean; 30 import java.beans.BeanProperty; 31 32 import javax.swing.plaf.*; 33 import javax.accessibility.*; 34 35 import java.io.ObjectOutputStream; 36 import java.io.IOException; 37 38 /** 39 * An implementation of a two-state button. 40 * The <code>JRadioButton</code> and <code>JCheckBox</code> classes 41 * are subclasses of this class. 42 * For information on using them see 43 * <a 44 href="http://docs.oracle.com/javase/tutorial/uiswing/components/button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a>, 45 * a section in <em>The Java Tutorial</em>. 46 * <p> 47 * Buttons can be configured, and to some degree controlled, by 48 * <code><a href="Action.html">Action</a></code>s. Using an 49 * <code>Action</code> with a button has many benefits beyond directly 50 * configuring a button. Refer to <a href="Action.html#buttonActions"> 51 * Swing Components Supporting <code>Action</code></a> for more 52 * details, and you can find more information in <a 53 * href="http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html">How 54 * to Use Actions</a>, a section in <em>The Java Tutorial</em>. 55 * <p> 56 * <strong>Warning:</strong> Swing is not thread safe. For more 57 * information see <a 58 * href="package-summary.html#threading">Swing's Threading 59 * Policy</a>. 60 * <p> 61 * <strong>Warning:</strong> 62 * Serialized objects of this class will not be compatible with 63 * future Swing releases. The current serialization support is 64 * appropriate for short term storage or RMI between applications running 65 * the same version of Swing. As of 1.4, support for long term storage 66 * of all JavaBeans™ 67 * has been added to the <code>java.beans</code> package. 68 * Please see {@link java.beans.XMLEncoder}. 69 * 70 * @see JRadioButton 71 * @see JCheckBox 72 * @author Jeff Dinkins 73 * @since 1.2 74 */ 75 @JavaBean(defaultProperty = "UIClassID", description = "An implementation of a two-state button.") 76 @SwingContainer(false) 77 @SuppressWarnings("serial") // Same-version serialization only 78 public class JToggleButton extends AbstractButton implements Accessible { 79 80 /** 81 * @see #getUIClassID 82 * @see #readObject 83 */ 84 private static final String uiClassID = "ToggleButtonUI"; 85 86 /** 87 * Creates an initially unselected toggle button 88 * without setting the text or image. 89 */ 90 public JToggleButton () { 91 this(null, null, false); 92 } 93 94 /** 95 * Creates an initially unselected toggle button 96 * with the specified image but no text. 97 * 98 * @param icon the image that the button should display 99 */ 100 public JToggleButton(Icon icon) { 101 this(null, icon, false); 102 } 103 104 /** 105 * Creates a toggle button with the specified image 106 * and selection state, but no text. 107 * 108 * @param icon the image that the button should display 109 * @param selected if true, the button is initially selected; 110 * otherwise, the button is initially unselected 111 */ 112 public JToggleButton(Icon icon, boolean selected) { 113 this(null, icon, selected); 114 } 115 116 /** 117 * Creates an unselected toggle button with the specified text. 118 * 119 * @param text the string displayed on the toggle button 120 */ 121 public JToggleButton (String text) { 122 this(text, null, false); 123 } 124 125 /** 126 * Creates a toggle button with the specified text 127 * and selection state. 128 * 129 * @param text the string displayed on the toggle button 130 * @param selected if true, the button is initially selected; 131 * otherwise, the button is initially unselected 132 */ 133 public JToggleButton (String text, boolean selected) { 134 this(text, null, selected); 135 } 136 137 /** 138 * Creates a toggle button where properties are taken from the 139 * Action supplied. 140 * 141 * @param a an instance of an {@code Action} 142 * @since 1.3 143 */ 144 public JToggleButton(Action a) { 145 this(); 146 setAction(a); 147 } 148 149 /** 150 * Creates a toggle button that has the specified text and image, 151 * and that is initially unselected. 152 * 153 * @param text the string displayed on the button 154 * @param icon the image that the button should display 155 */ 156 public JToggleButton(String text, Icon icon) { 157 this(text, icon, false); 158 } 159 160 /** 161 * Creates a toggle button with the specified text, image, and 162 * selection state. 163 * 164 * @param text the text of the toggle button 165 * @param icon the image that the button should display 166 * @param selected if true, the button is initially selected; 167 * otherwise, the button is initially unselected 168 */ 169 public JToggleButton (String text, Icon icon, boolean selected) { 170 // Create the model 171 setModel(new ToggleButtonModel()); 172 173 model.setSelected(selected); 174 175 // initialize 176 init(text, icon); 177 } 178 179 /** 180 * Resets the UI property to a value from the current look and feel. 181 * 182 * @see JComponent#updateUI 183 */ 184 public void updateUI() { 185 setUI((ButtonUI)UIManager.getUI(this)); 186 } 187 188 /** 189 * Returns a string that specifies the name of the l&f class 190 * that renders this component. 191 * 192 * @return String "ToggleButtonUI" 193 * @see JComponent#getUIClassID 194 * @see UIDefaults#getUI 195 */ 196 @BeanProperty(bound = false, description 197 = "A string that specifies the name of the L&F class") 198 public String getUIClassID() { 199 return uiClassID; 200 } 201 202 203 /** 204 * Overriden to return true, JToggleButton supports 205 * the selected state. 206 */ 207 boolean shouldUpdateSelectedStateFromAction() { 208 return true; 209 } 210 211 // ********************************************************************* 212 213 /** 214 * The ToggleButton model 215 * <p> 216 * <strong>Warning:</strong> 217 * Serialized objects of this class will not be compatible with 218 * future Swing releases. The current serialization support is 219 * appropriate for short term storage or RMI between applications running 220 * the same version of Swing. As of 1.4, support for long term storage 221 * of all JavaBeans™ 222 * has been added to the <code>java.beans</code> package. 223 * Please see {@link java.beans.XMLEncoder}. 224 */ 225 @SuppressWarnings("serial") // Same-version serialization only 226 public static class ToggleButtonModel extends DefaultButtonModel { 227 228 /** 229 * Creates a new ToggleButton Model 230 */ 231 public ToggleButtonModel () { 232 } 233 234 /** 235 * Checks if the button is selected. 236 */ 237 public boolean isSelected() { 238 // if(getGroup() != null) { 239 // return getGroup().isSelected(this); 240 // } else { 241 return (stateMask & SELECTED) != 0; 242 // } 243 } 244 245 246 /** 247 * Sets the selected state of the button. 248 * @param b true selects the toggle button, 249 * false deselects the toggle button. 250 */ 251 public void setSelected(boolean b) { 252 ButtonGroup group = getGroup(); 253 if (group != null) { 254 // use the group model instead 255 group.setSelected(this, b); 256 b = group.isSelected(this); 257 } 258 259 if (isSelected() == b) { 260 return; 261 } 262 263 if (b) { 264 stateMask |= SELECTED; 265 } else { 266 stateMask &= ~SELECTED; 267 } 268 269 // Send ChangeEvent 270 fireStateChanged(); 271 272 // Send ItemEvent 273 fireItemStateChanged( 274 new ItemEvent(this, 275 ItemEvent.ITEM_STATE_CHANGED, 276 this, 277 this.isSelected() ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); 278 279 } 280 281 /** 282 * Sets the pressed state of the toggle button. 283 */ 284 public void setPressed(boolean b) { 285 if ((isPressed() == b) || !isEnabled()) { 286 return; 287 } 288 289 if (b == false && isArmed()) { 290 setSelected(!this.isSelected()); 291 } 292 293 if (b) { 294 stateMask |= PRESSED; 295 } else { 296 stateMask &= ~PRESSED; 297 } 298 299 fireStateChanged(); 300 301 if(!isPressed() && isArmed()) { 302 int modifiers = 0; 303 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 304 if (currentEvent instanceof InputEvent) { 305 modifiers = ((InputEvent)currentEvent).getModifiers(); 306 } else if (currentEvent instanceof ActionEvent) { 307 modifiers = ((ActionEvent)currentEvent).getModifiers(); 308 } 309 fireActionPerformed( 310 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 311 getActionCommand(), 312 EventQueue.getMostRecentEventTime(), 313 modifiers)); 314 } 315 316 } 317 } 318 319 320 /** 321 * See readObject() and writeObject() in JComponent for more 322 * information about serialization in Swing. 323 */ 324 private void writeObject(ObjectOutputStream s) throws IOException { 325 s.defaultWriteObject(); 326 if (getUIClassID().equals(uiClassID)) { 327 byte count = JComponent.getWriteObjCounter(this); 328 JComponent.setWriteObjCounter(this, --count); 329 if (count == 0 && ui != null) { 330 ui.installUI(this); 331 } 332 } 333 } 334 335 336 /** 337 * Returns a string representation of this JToggleButton. This method 338 * is intended to be used only for debugging purposes, and the 339 * content and format of the returned string may vary between 340 * implementations. The returned string may be empty but may not 341 * be <code>null</code>. 342 * 343 * @return a string representation of this JToggleButton. 344 */ 345 protected String paramString() { 346 return super.paramString(); 347 } 348 349 350 ///////////////// 351 // Accessibility support 352 //////////////// 353 354 /** 355 * Gets the AccessibleContext associated with this JToggleButton. 356 * For toggle buttons, the AccessibleContext takes the form of an 357 * AccessibleJToggleButton. 358 * A new AccessibleJToggleButton instance is created if necessary. 359 * 360 * @return an AccessibleJToggleButton that serves as the 361 * AccessibleContext of this JToggleButton 362 */ 363 @BeanProperty(bound = false, expert = true, description 364 = "The AccessibleContext associated with this ToggleButton.") 365 public AccessibleContext getAccessibleContext() { 366 if (accessibleContext == null) { 367 accessibleContext = new AccessibleJToggleButton(); 368 } 369 return accessibleContext; 370 } 371 372 /** 373 * This class implements accessibility support for the 374 * <code>JToggleButton</code> class. It provides an implementation of the 375 * Java Accessibility API appropriate to toggle button user-interface 376 * elements. 377 * <p> 378 * <strong>Warning:</strong> 379 * Serialized objects of this class will not be compatible with 380 * future Swing releases. The current serialization support is 381 * appropriate for short term storage or RMI between applications running 382 * the same version of Swing. As of 1.4, support for long term storage 383 * of all JavaBeans™ 384 * has been added to the <code>java.beans</code> package. 385 * Please see {@link java.beans.XMLEncoder}. 386 */ 387 @SuppressWarnings("serial") // Same-version serialization only 388 protected class AccessibleJToggleButton extends AccessibleAbstractButton 389 implements ItemListener { 390 391 /** 392 * Constructs {@code AccessibleJToggleButton} 393 */ 394 public AccessibleJToggleButton() { 395 super(); 396 JToggleButton.this.addItemListener(this); 397 } 398 399 /** 400 * Fire accessible property change events when the state of the 401 * toggle button changes. 402 */ 403 public void itemStateChanged(ItemEvent e) { 404 JToggleButton tb = (JToggleButton) e.getSource(); 405 if (JToggleButton.this.accessibleContext != null) { 406 if (tb.isSelected()) { 407 JToggleButton.this.accessibleContext.firePropertyChange( 408 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 409 null, AccessibleState.CHECKED); 410 } else { 411 JToggleButton.this.accessibleContext.firePropertyChange( 412 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 413 AccessibleState.CHECKED, null); 414 } 415 } 416 } 417 418 /** 419 * Get the role of this object. 420 * 421 * @return an instance of AccessibleRole describing the role of the 422 * object 423 */ 424 public AccessibleRole getAccessibleRole() { 425 return AccessibleRole.TOGGLE_BUTTON; 426 } 427 } // inner class AccessibleJToggleButton 428 }