Source for javax.swing.JButton

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