001    /* JButton.java --
002       Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.swing;
039    
040    import javax.accessibility.Accessible;
041    import javax.accessibility.AccessibleContext;
042    import javax.accessibility.AccessibleRole;
043    import javax.swing.plaf.ButtonUI;
044    
045    
046    /**
047     * A general purpose push button. <code>JButton</code>s can display a label,
048     * an {@link Icon} or both.
049     *
050     * @author Ronald Veldema (rveldema@cs.vu.nl)
051     */
052    public class JButton extends AbstractButton
053      implements Accessible
054    {
055    
056      /**
057       * Accessibility support for JButtons.
058       */
059      protected class AccessibleJButton
060        extends AbstractButton.AccessibleAbstractButton
061      {
062        /**
063         * Returns the accessible role that this component represents.
064         * This is {@link AccessibleRole#PUSH_BUTTON} for <code>JButton</code>s.
065         *
066         * @return the accessible role that this component represents
067         */
068        public AccessibleRole getAccessibleRole()
069        {
070          return AccessibleRole.PUSH_BUTTON;
071        }
072      }
073    
074      private static final long serialVersionUID = -1907255238954382202L;
075    
076      /**
077       * Indicates if this button is capable to become the default button.
078       */
079      private boolean defaultCapable;
080    
081      /**
082       * Creates a new button with an empty string for the button text and no
083       * icon.
084       */
085      public JButton()
086      {
087        this(null, null);
088      }
089    
090      /**
091       * Creates a new button from the specified action.
092       * 
093       * @param a  the action (<code>null</code> permitted).
094       * 
095       * @see AbstractButton#setAction(Action)
096       */
097      public JButton(Action a)
098      {
099        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    }