source: trunk/gcc/libjava/java/awt/Button.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 8.1 KB
Line 
1/* Button.java -- AWT button widget
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.event.ActionEvent;
42import java.awt.event.ActionListener;
43import java.awt.peer.ButtonPeer;
44import java.awt.peer.ComponentPeer;
45import java.lang.reflect.Array;
46import java.util.EventListener;
47
48/**
49 * This class provides a button widget for the AWT.
50 *
51 * @author Aaron M. Renn (arenn@urbanophile.com)
52 * @author Tom Tromey <tromey@cygnus.com>
53 */
54public class Button extends Component implements java.io.Serializable
55{
56
57/*
58 * Static Variables
59 */
60
61// FIXME: Need readObject/writeObject for serialization
62
63// Serialization version constant
64private static final long serialVersionUID = -8774683716313001058L;
65
66/*************************************************************************/
67
68/*
69 * Instance Variables
70 */
71
72/**
73 * @serial The action command name for this button.
74 */
75private String actionCommand;
76
77/**
78 * @serial The label for this button.
79 */
80private String label;
81
82// List of ActionListeners for this class.
83private transient ActionListener action_listeners;
84
85/*************************************************************************/
86
87/*
88 * Constructors
89 */
90
91/**
92 * Initializes a new instance of <code>Button</code> with no label.
93 *
94 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
95 * returns true
96 */
97public
98Button()
99{
100 this(null);
101}
102
103/*************************************************************************/
104
105/**
106 * Initializes a new instance of <code>Button</code> with the specified
107 * label. The action command name is also initialized to this value.
108 *
109 * @param label The label to display on the button.
110 *
111 * @exception HeadlessException If GraphicsEnvironment.isHeadless()
112 * returns true
113 */
114public
115Button(String label)
116{
117 this.label = label;
118 actionCommand = label;
119
120 if (GraphicsEnvironment.isHeadless ())
121 throw new HeadlessException ();
122}
123
124/*************************************************************************/
125
126/*
127 * Instance Variables
128 */
129
130/**
131 * Returns the label for this button.
132 *
133 * @return The label for this button.
134 */
135public String
136getLabel()
137{
138 return(label);
139}
140
141/*************************************************************************/
142
143/**
144 * Sets the label for this button to the specified value.
145 *
146 * @param label The new label for this button.
147 */
148public synchronized void
149setLabel(String label)
150{
151 this.label = label;
152 if (peer != null)
153 {
154 ButtonPeer bp = (ButtonPeer) peer;
155 bp.setLabel (label);
156 }
157}
158
159/*************************************************************************/
160
161/**
162 * Returns the action command name for this button.
163 *
164 * @return The action command name for this button.
165 */
166public String
167getActionCommand()
168{
169 return(actionCommand);
170}
171
172/*************************************************************************/
173
174/**
175 * Sets the action command name for this button to the specified value.
176 *
177 * @param actionCommand The new action command name.
178 */
179public void
180setActionCommand(String actionCommand)
181{
182 this.actionCommand = actionCommand == null ? label : actionCommand;
183}
184
185/*************************************************************************/
186
187/**
188 * Adds a new entry to the list of listeners that will receive
189 * action events from this button.
190 *
191 * @param listener The listener to add.
192 */
193public synchronized void
194addActionListener(ActionListener listener)
195{
196 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
197}
198
199/*************************************************************************/
200
201/**
202 * Removes the specified listener from the list of listeners that will
203 * receive action events from this button.
204 *
205 * @param listener The listener to remove.
206 */
207public synchronized void
208removeActionListener(ActionListener listener)
209{
210 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
211}
212
213 public synchronized ActionListener[] getActionListeners()
214 {
215 return (ActionListener[])
216 AWTEventMulticaster.getListeners(action_listeners,
217 ActionListener.class);
218 }
219
220/** Returns all registered EventListers of the given listenerType.
221 * listenerType must be a subclass of EventListener, or a
222 * ClassClassException is thrown.
223 *
224 * @exception ClassCastException If listenerType doesn't specify a class or
225 * interface that implements @see java.util.EventListener.
226 *
227 * @since 1.3
228 */
229 public EventListener[] getListeners(Class listenerType)
230 {
231 if (listenerType == ActionListener.class)
232 return getActionListeners();
233 return (EventListener[]) Array.newInstance(listenerType, 0);
234 }
235
236/*************************************************************************/
237
238/**
239 * Notifies this button that it should create its native peer object.
240 */
241public void
242addNotify()
243{
244 if (peer == null)
245 peer = getToolkit ().createButton (this);
246 super.addNotify();
247}
248
249/*************************************************************************/
250
251/**
252 * Processes an event for this button. If the specified event is an
253 * instance of <code>ActionEvent</code>, then the
254 * <code>processActionEvent()</code> method is called to dispatch it
255 * to any registered listeners. Otherwise, the superclass method
256 * will be invoked. Note that this method will not be called at all
257 * unless <code>ActionEvent</code>'s are enabled. This will be done
258 * implicitly if any listeners are added.
259 *
260 * @param event The event to process.
261 */
262protected void
263processEvent(AWTEvent event)
264{
265 if (event instanceof ActionEvent)
266 processActionEvent((ActionEvent)event);
267 else
268 super.processEvent(event);
269}
270
271/*************************************************************************/
272
273/**
274 * This method dispatches an action event for this button to any
275 * registered listeners.
276 *
277 * @param event The event to process.
278 */
279protected void
280processActionEvent(ActionEvent event)
281{
282 if (action_listeners != null)
283 action_listeners.actionPerformed(event);
284}
285
286void
287dispatchEventImpl(AWTEvent e)
288{
289 if (e.id <= ActionEvent.ACTION_LAST
290 && e.id >= ActionEvent.ACTION_FIRST
291 && (action_listeners != null
292 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
293 processEvent(e);
294 else
295 super.dispatchEventImpl(e);
296}
297
298/*************************************************************************/
299
300/**
301 * Returns a debugging string for this button.
302 *
303 * @return A debugging string for this button.
304 */
305protected String
306paramString()
307{
308 return ("label=" + getLabel() + ",actionCommand=" + getActionCommand()
309 + "," + super.paramString());
310}
311
312} // class Button
313
Note: See TracBrowser for help on using the repository browser.