GNU Classpath (0.95) | |
Frames | No Frames |
1: /* JButton.java -- 2: Copyright (C) 2002, 2004, 2005, 2006, 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: package javax.swing; 39: 40: import javax.accessibility.Accessible; 41: import javax.accessibility.AccessibleContext; 42: import javax.accessibility.AccessibleRole; 43: import javax.swing.plaf.ButtonUI; 44: 45: 46: /** 47: * A general purpose push button. <code>JButton</code>s can display a label, 48: * an {@link Icon} or both. 49: * 50: * @author Ronald Veldema (rveldema@cs.vu.nl) 51: */ 52: public class JButton extends AbstractButton 53: implements Accessible 54: { 55: 56: /** 57: * Accessibility support for JButtons. 58: */ 59: protected class AccessibleJButton 60: extends AbstractButton.AccessibleAbstractButton 61: { 62: /** 63: * Returns the accessible role that this component represents. 64: * This is {@link AccessibleRole#PUSH_BUTTON} for <code>JButton</code>s. 65: * 66: * @return the accessible role that this component represents 67: */ 68: public AccessibleRole getAccessibleRole() 69: { 70: return AccessibleRole.PUSH_BUTTON; 71: } 72: } 73: 74: private static final long serialVersionUID = -1907255238954382202L; 75: 76: /** 77: * Indicates if this button is capable to become the default button. 78: */ 79: private boolean defaultCapable; 80: 81: /** 82: * Creates a new button with an empty string for the button text and no 83: * icon. 84: */ 85: public JButton() 86: { 87: this(null, null); 88: } 89: 90: /** 91: * Creates a new button from the specified action. 92: * 93: * @param a the action (<code>null</code> permitted). 94: * 95: * @see AbstractButton#setAction(Action) 96: */ 97: public JButton(Action a) 98: { 99: this(); 100: setAction(a); 101: } 102: 103: /** 104: * Creates a new button with the specified icon (and an empty string for 105: * the button text). 106: * 107: * @param icon the icon (<code>null</code> permitted). 108: */ 109: public JButton(Icon icon) 110: { 111: this(null, icon); 112: } 113: 114: /** 115: * Creates a new button with the specified text and no icon. 116: * 117: * @param text the button text (<code>null</code> permitted, will be 118: * substituted by an empty string). 119: */ 120: public JButton(String text) 121: { 122: this(text, null); 123: } 124: 125: /** 126: * Creates a new button with the specified text and icon. 127: * 128: * @param text the button text (<code>null</code> permitted, will be 129: * substituted by an empty string). 130: * @param icon the icon (<code>null</code> permitted). 131: */ 132: public JButton(String text, Icon icon) 133: { 134: super(); 135: setModel(new DefaultButtonModel()); 136: init(text, icon); 137: defaultCapable = true; 138: } 139: 140: protected void configurePropertiesFromAction(Action a) 141: { 142: super.configurePropertiesFromAction(a); 143: } 144: 145: /** 146: * Returns the object that provides accessibility features for this 147: * <code>JButton</code> component. 148: * 149: * @return The accessible context (an instance of {@link AccessibleJButton}). 150: */ 151: public AccessibleContext getAccessibleContext() 152: { 153: if (accessibleContext == null) 154: accessibleContext = new AccessibleJButton(); 155: return accessibleContext; 156: } 157: 158: /** 159: * Returns the suffix (<code>"ButtonUI"</code> in this case) used to 160: * determine the class name for a UI delegate that can provide the look and 161: * feel for a <code>JButton</code>. 162: * 163: * @return <code>"ButtonUI"</code>. 164: */ 165: public String getUIClassID() 166: { 167: // Returns a string that specifies the name of the L&F class that renders 168: // this component. 169: return "ButtonUI"; 170: } 171: 172: /** 173: * Returns <code>true</code> if this button is the default button in 174: * its <code>JRootPane</code>. The default button gets automatically 175: * activated when the user presses <code>ENTER</code> (or whatever 176: * key this is bound to in the current Look and Feel). 177: * 178: * @return <code>true</code> if this button is the default button in 179: * its <code>JRootPane</code> 180: * 181: * @see #isDefaultCapable() 182: * @see #setDefaultCapable(boolean) 183: * @see JRootPane#getDefaultButton() 184: * @see JRootPane#setDefaultButton(JButton) 185: */ 186: public boolean isDefaultButton() 187: { 188: // The default button is managed by the JRootPane, so the safest way 189: // to determine this property is to ask the root pane of this button, 190: // if it exists. 191: JRootPane rp = SwingUtilities.getRootPane(this); 192: boolean isDefault = false; 193: if (rp != null) 194: isDefault = rp.getDefaultButton() == this; 195: return isDefault; 196: } 197: 198: /** 199: * Returns <code>true</code> if this button can act as the default button. 200: * This is <code>true</code> by default. 201: * 202: * @return <code>true</code> if this button can act as the default button 203: * 204: * @see #setDefaultCapable(boolean) 205: * @see #isDefaultButton() 206: * @see JRootPane#getDefaultButton() 207: * @see JRootPane#setDefaultButton(JButton) 208: */ 209: public boolean isDefaultCapable() 210: { 211: // Returns whether or not this button is capable of being the default 212: // button on the RootPane. 213: return defaultCapable; 214: } 215: 216: /** 217: * Returns an implementation-dependent string describing the attributes of 218: * this <code>JButton</code>. 219: * 220: * @return A string describing the attributes of this <code>JButton</code> 221: * (never <code>null</code>). 222: */ 223: protected String paramString() 224: { 225: String superParam = super.paramString(); 226: 227: // 41 is the maximum number of chars which may be needed. 228: StringBuffer sb = new StringBuffer(41); 229: sb.append(",defaultButton=").append(isDefaultButton()); 230: sb.append(",defaultCapable=").append(defaultCapable); 231: 232: return superParam + sb.toString(); 233: } 234: 235: /** 236: * Overrides JComponent.removeNotify to check if this button is currently 237: * set as the default button on the RootPane, and if so, sets the RootPane's 238: * default button to null to ensure the RootPane doesn't hold onto an invalid 239: * button reference. 240: */ 241: public void removeNotify() 242: { 243: JRootPane root = SwingUtilities.getRootPane(this); 244: if (root != null && root.getDefaultButton() == this) 245: root.setDefaultButton(null); 246: super.removeNotify(); 247: } 248: 249: /** 250: * Sets the <code>defaultCapable</code> property which indicates if 251: * this button may become the default button in its <code>JRootPane</code>. 252: * 253: * @param defaultCapable <code>true</code> if this button can become the 254: * default button in its JRootPane, <code>false</code> otherwise 255: * 256: * @see #setDefaultCapable(boolean) 257: * @see #isDefaultButton() 258: * @see JRootPane#getDefaultButton() 259: * @see JRootPane#setDefaultButton(JButton) 260: */ 261: public void setDefaultCapable(boolean defaultCapable) 262: { 263: this.defaultCapable = defaultCapable; 264: } 265: 266: /** 267: * Sets this button's UI delegate to the default (obtained from the 268: * {@link UIManager}) for the current look and feel. 269: */ 270: public void updateUI() 271: { 272: setUI((ButtonUI) UIManager.getUI(this)); 273: } 274: }
GNU Classpath (0.95) |