Merge from the pain train
[official-gcc.git] / libjava / java / awt / Button.java
blobeb0fe3b7a2032870ee40b088b4e62901f9d04c38
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)
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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. */
39 package java.awt;
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;
53 /**
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
64 * Static Variables
67 // FIXME: Need readObject/writeObject for serialization
69 // Serialization version constant
70 private static final long serialVersionUID = -8774683716313001058L;
72 /*************************************************************************/
75 * Instance Variables
78 /**
79 * @serial The action command name for this button.
81 private String actionCommand;
83 /**
84 * @serial The label for this button.
86 private String label;
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()
101 // Do nothing here.
104 /* (non-Javadoc)
105 * @see javax.accessibility.AccessibleAction#getAccessibleActionCount()
107 public int getAccessibleActionCount()
109 // Only 1 action possible
110 return 1;
113 /* (non-Javadoc)
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.
122 if (i == 0)
123 return "click";
124 return null;
127 /* (non-Javadoc)
128 * @see javax.accessibility.AccessibleAction#doAccessibleAction(int)
130 public boolean doAccessibleAction(int i)
132 if (i != 0)
133 return false;
134 processActionEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand));
135 return true;
138 public String getAccessibleName()
140 return label;
143 public AccessibleAction getAccessibleAction()
145 return this;
148 public AccessibleValue getAccessibleValue()
150 return this;
153 /* (non-Javadoc)
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);
162 /* (non-Javadoc)
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.
169 return false;
172 /* (non-Javadoc)
173 * @see javax.accessibility.AccessibleValue#getMinimumAccessibleValue()
175 public Number getMinimumAccessibleValue()
177 return new Integer(0);
180 /* (non-Javadoc)
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 /*************************************************************************/
197 * Constructors
201 * Initializes a new instance of <code>Button</code> with no label.
203 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
204 * returns true
206 public
207 Button()
209 this("");
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()
221 * returns true
223 public
224 Button(String label)
226 this.label = label;
227 actionCommand = label;
229 if (GraphicsEnvironment.isHeadless ())
230 throw new HeadlessException ();
233 /*************************************************************************/
236 * Instance Variables
240 * Returns the label for this button.
242 * @return The label for this button.
244 public String
245 getLabel()
247 return(label);
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)
260 this.label = label;
261 actionCommand = label;
262 if (peer != null)
264 ButtonPeer bp = (ButtonPeer) peer;
265 bp.setLabel (label);
269 /*************************************************************************/
272 * Returns the action command name for this button.
274 * @return The action command name for this button.
276 public String
277 getActionCommand()
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.
289 public void
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
328 * @since 1.4
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.
349 * @since 1.3
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.
363 public void
364 addNotify()
366 if (peer == null)
367 peer = getToolkit ().createButton (this);
368 super.addNotify();
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.
384 protected void
385 processEvent(AWTEvent event)
387 if (event instanceof ActionEvent)
388 processActionEvent((ActionEvent)event);
389 else
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.
401 protected void
402 processActionEvent(ActionEvent event)
404 if (action_listeners != null)
405 action_listeners.actionPerformed(event);
408 void
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))
415 processEvent(e);
416 else
417 super.dispatchEventImpl(e);
420 /*************************************************************************/
423 * Returns a debugging string for this button.
425 * @return A debugging string for this button.
427 protected String
428 paramString()
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++;
463 } // class Button