1 /* Button.java -- AWT button widget
2 Copyright (C) 1999, 2002, 2004, 2005 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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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. */
41 import java
.awt
.event
.ActionEvent
;
42 import java
.awt
.event
.ActionListener
;
43 import java
.awt
.peer
.ButtonPeer
;
44 import java
.lang
.reflect
.Array
;
45 import java
.util
.EventListener
;
47 import javax
.accessibility
.Accessible
;
48 import javax
.accessibility
.AccessibleAction
;
49 import javax
.accessibility
.AccessibleContext
;
50 import javax
.accessibility
.AccessibleRole
;
51 import javax
.accessibility
.AccessibleValue
;
54 * This class provides a button widget for the AWT.
56 * @author Aaron M. Renn (arenn@urbanophile.com)
57 * @author Tom Tromey (tromey@cygnus.com)
59 public class Button
extends Component
60 implements java
.io
.Serializable
, Accessible
67 // FIXME: Need readObject/writeObject for serialization
69 // Serialization version constant
70 private static final long serialVersionUID
= -8774683716313001058L;
72 /*************************************************************************/
79 * @serial The action command name for this button.
81 private String actionCommand
;
84 * @serial The label for this button.
88 // List of ActionListeners for this class.
89 private transient ActionListener action_listeners
;
92 * The number used to generate the name returned by getName.
94 private static transient long next_button_number
;
96 protected class AccessibleAWTButton
extends AccessibleAWTComponent
97 implements AccessibleAction
, AccessibleValue
99 protected AccessibleAWTButton()
105 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
107 public int getAccessibleActionCount()
109 // Only 1 action possible
114 * @see javax.accessibility.AccessibleAction#getAccessibleActionDescription(int)
116 public String
getAccessibleActionDescription(int i
)
118 // JDK 1.4.2 returns the string "click" for action 0. However, the API
119 // docs don't say what the string to be returned is, beyond being a
120 // description of the action. So we return the same thing for
121 // compatibility with 1.4.2.
128 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
130 public boolean doAccessibleAction(int i
)
134 processActionEvent(new ActionEvent(this, ActionEvent
.ACTION_PERFORMED
, actionCommand
));
138 public String
getAccessibleName()
143 public AccessibleAction
getAccessibleAction()
148 public AccessibleValue
getAccessibleValue()
154 * @see javax.accessibility.AccessibleValue#getCurrentAccessibleValue()
156 public Number
getCurrentAccessibleValue()
158 // Docs say return 1 if selected, but buttons can't be selected, right?
159 return new Integer(0);
163 * @see javax.accessibility.AccessibleValue#setCurrentAccessibleValue(java.lang.Number)
165 public boolean setCurrentAccessibleValue(Number number
)
167 // Since there's no selection with buttons, we're ignoring this.
168 // TODO someone who knows shoulw check this.
173 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
175 public Number
getMinimumAccessibleValue()
177 return new Integer(0);
181 * @see javax.accessibility.AccessibleValue#getMaximumAccessibleValue()
183 public Number
getMaximumAccessibleValue()
185 return new Integer(0);
188 public AccessibleRole
getAccessibleRole()
190 return AccessibleRole
.PUSH_BUTTON
;
194 /*************************************************************************/
201 * Initializes a new instance of <code>Button</code> with no label.
203 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
212 /*************************************************************************/
215 * Initializes a new instance of <code>Button</code> with the specified
216 * label. The action command name is also initialized to this value.
218 * @param label The label to display on the button.
220 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
227 actionCommand
= label
;
229 if (GraphicsEnvironment
.isHeadless ())
230 throw new HeadlessException ();
233 /*************************************************************************/
240 * Returns the label for this button.
242 * @return The label for this button.
250 /*************************************************************************/
253 * Sets the label for this button to the specified value.
255 * @param label The new label for this button.
257 public synchronized void
258 setLabel(String label
)
261 actionCommand
= label
;
264 ButtonPeer bp
= (ButtonPeer
) peer
;
269 /*************************************************************************/
272 * Returns the action command name for this button.
274 * @return The action command name for this button.
279 return(actionCommand
);
282 /*************************************************************************/
285 * Sets the action command name for this button to the specified value.
287 * @param actionCommand The new action command name.
290 setActionCommand(String actionCommand
)
292 this.actionCommand
= actionCommand
== null ? label
: actionCommand
;
295 /*************************************************************************/
298 * Adds a new entry to the list of listeners that will receive
299 * action events from this button.
301 * @param listener The listener to add.
303 public synchronized void
304 addActionListener(ActionListener listener
)
306 action_listeners
= AWTEventMulticaster
.add(action_listeners
, listener
);
309 /*************************************************************************/
312 * Removes the specified listener from the list of listeners that will
313 * receive action events from this button.
315 * @param listener The listener to remove.
317 public synchronized void
318 removeActionListener(ActionListener listener
)
320 action_listeners
= AWTEventMulticaster
.remove(action_listeners
, listener
);
324 * Returns all added <code>ActionListener</code> objects.
326 * @return an array of listeners
330 public synchronized ActionListener
[] getActionListeners()
332 return (ActionListener
[])
333 AWTEventMulticaster
.getListeners(action_listeners
,
334 ActionListener
.class);
338 * Returns all registered EventListers of the given listenerType.
339 * listenerType must be a subclass of EventListener, or a
340 * ClassClassException is thrown.
342 * @param listenerType the listener type to return
344 * @return an array of listeners
346 * @exception ClassCastException If listenerType doesn't specify a class or
347 * interface that implements @see java.util.EventListener.
351 public EventListener
[] getListeners(Class listenerType
)
353 if (listenerType
== ActionListener
.class)
354 return getActionListeners();
355 return (EventListener
[]) Array
.newInstance(listenerType
, 0);
358 /*************************************************************************/
361 * Notifies this button that it should create its native peer object.
367 peer
= getToolkit ().createButton (this);
371 /*************************************************************************/
374 * Processes an event for this button. If the specified event is an
375 * instance of <code>ActionEvent</code>, then the
376 * <code>processActionEvent()</code> method is called to dispatch it
377 * to any registered listeners. Otherwise, the superclass method
378 * will be invoked. Note that this method will not be called at all
379 * unless <code>ActionEvent</code>'s are enabled. This will be done
380 * implicitly if any listeners are added.
382 * @param event The event to process.
385 processEvent(AWTEvent event
)
387 if (event
instanceof ActionEvent
)
388 processActionEvent((ActionEvent
)event
);
390 super.processEvent(event
);
393 /*************************************************************************/
396 * This method dispatches an action event for this button to any
397 * registered listeners.
399 * @param event The event to process.
402 processActionEvent(ActionEvent event
)
404 if (action_listeners
!= null)
405 action_listeners
.actionPerformed(event
);
409 dispatchEventImpl(AWTEvent e
)
411 if (e
.id
<= ActionEvent
.ACTION_LAST
412 && e
.id
>= ActionEvent
.ACTION_FIRST
413 && (action_listeners
!= null
414 || (eventMask
& AWTEvent
.ACTION_EVENT_MASK
) != 0))
417 super.dispatchEventImpl(e
);
420 /*************************************************************************/
423 * Returns a debugging string for this button.
425 * @return A debugging string for this button.
430 return getName () + "," + getX () + "," + getY () + ","
431 + getWidth () + "x" + getHeight () + ",label=" + getLabel ();
435 * Gets the AccessibleContext associated with this <code>Button</code>.
436 * The context is created, if necessary.
438 * @return the associated context
440 public AccessibleContext
getAccessibleContext()
442 /* Create the context if this is the first request */
443 if (accessibleContext
== null)
444 accessibleContext
= new AccessibleAWTButton();
445 return accessibleContext
;
449 * Generate a unique name for this button.
451 * @return A unique name for this button.
453 String
generateName ()
455 return "button" + getUniqueLong ();
458 private static synchronized long getUniqueLong ()
460 return next_button_number
++;