1 /* Button.java -- AWT button widget
2 Copyright (C) 1999, 2002 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.awt.peer.ComponentPeer;
45 import java.util.EventListener;
48 * This class provides a button widget for the AWT.
50 * @author Aaron M. Renn (arenn@urbanophile.com)
51 * @author Tom Tromey <tromey@cygnus.com>
53 public class Button extends Component implements java.io.Serializable
60 // FIXME: Need readObject/writeObject for serialization
62 // Serialization version constant
63 private static final long serialVersionUID = -8774683716313001058L;
65 /*************************************************************************/
72 * @serial The action command name for this button.
74 private String actionCommand;
77 * @serial The label for this button.
81 // List of ActionListeners for this class.
82 private transient ActionListener action_listeners;
84 /*************************************************************************/
91 * Initializes a new instance of <code>Button</code> with no label.
99 /*************************************************************************/
102 * Initializes a new instance of <code>Button</code> with the specified
103 * label. The action command name is also initialized to this value.
105 * @param label The label to display on the button.
111 actionCommand = label;
114 /*************************************************************************/
121 * Returns the label for this button.
123 * @return The label for this button.
131 /*************************************************************************/
134 * Sets the label for this button to the specified value.
136 * @param label The new label for this button.
138 public synchronized void
139 setLabel(String label)
144 ButtonPeer bp = (ButtonPeer) peer;
149 /*************************************************************************/
152 * Returns the action command name for this button.
154 * @return The action command name for this button.
159 return(actionCommand);
162 /*************************************************************************/
165 * Sets the action command name for this button to the specified value.
167 * @param actionCommand The new action command name.
170 setActionCommand(String actionCommand)
172 this.actionCommand = actionCommand == null ? label : actionCommand;
175 /*************************************************************************/
178 * Adds a new entry to the list of listeners that will receive
179 * action events from this button.
181 * @param listener The listener to add.
183 public synchronized void
184 addActionListener(ActionListener listener)
186 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
189 /*************************************************************************/
192 * Removes the specified listener from the list of listeners that will
193 * receive action events from this button.
195 * @param listener The listener to remove.
197 public synchronized void
198 removeActionListener(ActionListener listener)
200 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
203 public EventListener[]
204 getListeners(Class listenerType)
206 if (listenerType == ActionListener.class)
207 return getListenersImpl(listenerType, action_listeners);
208 return super.getListeners(listenerType);
211 /*************************************************************************/
214 * Notifies this button that it should create its native peer object.
220 peer = getToolkit ().createButton (this);
224 /*************************************************************************/
227 * Processes an event for this button. If the specified event is an
228 * instance of <code>ActionEvent</code>, then the
229 * <code>processActionEvent()</code> method is called to dispatch it
230 * to any registered listeners. Otherwise, the superclass method
231 * will be invoked. Note that this method will not be called at all
232 * unless <code>ActionEvent</code>'s are enabled. This will be done
233 * implicitly if any listeners are added.
235 * @param event The event to process.
238 processEvent(AWTEvent event)
240 if (event instanceof ActionEvent)
241 processActionEvent((ActionEvent)event);
243 super.processEvent(event);
246 /*************************************************************************/
249 * This method dispatches an action event for this button to any
250 * registered listeners.
252 * @param event The event to process.
255 processActionEvent(ActionEvent event)
257 if (action_listeners != null)
258 action_listeners.actionPerformed(event);
262 dispatchEventImpl(AWTEvent e)
264 super.dispatchEventImpl(e);
266 if (e.id <= ActionEvent.ACTION_LAST
267 && e.id >= ActionEvent.ACTION_FIRST
268 && (action_listeners != null
269 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
273 /*************************************************************************/
276 * Returns a debugging string for this button.
278 * @return A debugging string for this button.
283 return ("label=" + getLabel() + ",actionCommand=" + getActionCommand()
284 + "," + super.paramString());