libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / swing / JButton.java
blob705a863810552a243e286161bb902c6049019237
1 /* JButton.java --
2 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package javax.swing;
40 import gnu.java.lang.CPStringBuilder;
42 import javax.accessibility.Accessible;
43 import javax.accessibility.AccessibleContext;
44 import javax.accessibility.AccessibleRole;
45 import javax.swing.plaf.ButtonUI;
48 /**
49 * A general purpose push button. <code>JButton</code>s can display a label,
50 * an {@link Icon} or both.
52 * @author Ronald Veldema (rveldema@cs.vu.nl)
54 public class JButton extends AbstractButton
55 implements Accessible
58 /**
59 * Accessibility support for JButtons.
61 protected class AccessibleJButton
62 extends AbstractButton.AccessibleAbstractButton
64 /**
65 * Returns the accessible role that this component represents.
66 * This is {@link AccessibleRole#PUSH_BUTTON} for <code>JButton</code>s.
68 * @return the accessible role that this component represents
70 public AccessibleRole getAccessibleRole()
72 return AccessibleRole.PUSH_BUTTON;
76 private static final long serialVersionUID = -1907255238954382202L;
78 /**
79 * Indicates if this button is capable to become the default button.
81 private boolean defaultCapable;
83 /**
84 * Creates a new button with an empty string for the button text and no
85 * icon.
87 public JButton()
89 this(null, null);
92 /**
93 * Creates a new button from the specified action.
95 * @param a the action (<code>null</code> permitted).
97 * @see AbstractButton#setAction(Action)
99 public JButton(Action a)
101 this();
102 setAction(a);
106 * Creates a new button with the specified icon (and an empty string for
107 * the button text).
109 * @param icon the icon (<code>null</code> permitted).
111 public JButton(Icon icon)
113 this(null, icon);
117 * Creates a new button with the specified text and no icon.
119 * @param text the button text (<code>null</code> permitted, will be
120 * substituted by an empty string).
122 public JButton(String text)
124 this(text, null);
128 * Creates a new button with the specified text and icon.
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).
134 public JButton(String text, Icon icon)
136 super();
137 setModel(new DefaultButtonModel());
138 init(text, icon);
139 defaultCapable = true;
142 protected void configurePropertiesFromAction(Action a)
144 super.configurePropertiesFromAction(a);
148 * Returns the object that provides accessibility features for this
149 * <code>JButton</code> component.
151 * @return The accessible context (an instance of {@link AccessibleJButton}).
153 public AccessibleContext getAccessibleContext()
155 if (accessibleContext == null)
156 accessibleContext = new AccessibleJButton();
157 return accessibleContext;
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>.
165 * @return <code>"ButtonUI"</code>.
167 public String getUIClassID()
169 // Returns a string that specifies the name of the L&F class that renders
170 // this component.
171 return "ButtonUI";
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).
180 * @return <code>true</code> if this button is the default button in
181 * its <code>JRootPane</code>
183 * @see #isDefaultCapable()
184 * @see #setDefaultCapable(boolean)
185 * @see JRootPane#getDefaultButton()
186 * @see JRootPane#setDefaultButton(JButton)
188 public boolean isDefaultButton()
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;
201 * Returns <code>true</code> if this button can act as the default button.
202 * This is <code>true</code> by default.
204 * @return <code>true</code> if this button can act as the default button
206 * @see #setDefaultCapable(boolean)
207 * @see #isDefaultButton()
208 * @see JRootPane#getDefaultButton()
209 * @see JRootPane#setDefaultButton(JButton)
211 public boolean isDefaultCapable()
213 // Returns whether or not this button is capable of being the default
214 // button on the RootPane.
215 return defaultCapable;
219 * Returns an implementation-dependent string describing the attributes of
220 * this <code>JButton</code>.
222 * @return A string describing the attributes of this <code>JButton</code>
223 * (never <code>null</code>).
225 protected String paramString()
227 String superParam = super.paramString();
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);
234 return superParam + sb.toString();
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.
243 public void removeNotify()
245 JRootPane root = SwingUtilities.getRootPane(this);
246 if (root != null && root.getDefaultButton() == this)
247 root.setDefaultButton(null);
248 super.removeNotify();
252 * Sets the <code>defaultCapable</code> property which indicates if
253 * this button may become the default button in its <code>JRootPane</code>.
255 * @param defaultCapable <code>true</code> if this button can become the
256 * default button in its JRootPane, <code>false</code> otherwise
258 * @see #setDefaultCapable(boolean)
259 * @see #isDefaultButton()
260 * @see JRootPane#getDefaultButton()
261 * @see JRootPane#setDefaultButton(JButton)
263 public void setDefaultCapable(boolean defaultCapable)
265 this.defaultCapable = defaultCapable;
269 * Sets this button's UI delegate to the default (obtained from the
270 * {@link UIManager}) for the current look and feel.
272 public void updateUI()
274 setUI((ButtonUI) UIManager.getUI(this));