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 gnu.java.lang.CPStringBuilder;
041
042 import javax.accessibility.Accessible;
043 import javax.accessibility.AccessibleContext;
044 import javax.accessibility.AccessibleRole;
045 import javax.swing.plaf.ButtonUI;
046
047
048 /**
049 * A general purpose push button. <code>JButton</code>s can display a label,
050 * an {@link Icon} or both.
051 *
052 * @author Ronald Veldema (rveldema@cs.vu.nl)
053 */
054 public class JButton extends AbstractButton
055 implements Accessible
056 {
057
058 /**
059 * Accessibility support for JButtons.
060 */
061 protected class AccessibleJButton
062 extends AbstractButton.AccessibleAbstractButton
063 {
064 /**
065 * Returns the accessible role that this component represents.
066 * This is {@link AccessibleRole#PUSH_BUTTON} for <code>JButton</code>s.
067 *
068 * @return the accessible role that this component represents
069 */
070 public AccessibleRole getAccessibleRole()
071 {
072 return AccessibleRole.PUSH_BUTTON;
073 }
074 }
075
076 private static final long serialVersionUID = -1907255238954382202L;
077
078 /**
079 * Indicates if this button is capable to become the default button.
080 */
081 private boolean defaultCapable;
082
083 /**
084 * Creates a new button with an empty string for the button text and no
085 * icon.
086 */
087 public JButton()
088 {
089 this(null, null);
090 }
091
092 /**
093 * Creates a new button from the specified action.
094 *
095 * @param a the action (<code>null</code> permitted).
096 *
097 * @see AbstractButton#setAction(Action)
098 */
099 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 }