source: trunk/src/gcc/libjava/java/awt/Button.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 7.3 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.util.EventListener;
46
47/**
48 * This class provides a button widget for the AWT.
49 *
50 * @author Aaron M. Renn ([email protected])
51 * @author Tom Tromey <[email protected]>
52 */
53public class Button extends Component implements java.io.Serializable
54{
55
56/*
57 * Static Variables
58 */
59
60// FIXME: Need readObject/writeObject for serialization
61
62// Serialization version constant
63private static final long serialVersionUID = -8774683716313001058L;
64
65/*************************************************************************/
66
67/*
68 * Instance Variables
69 */
70
71/**
72 * @serial The action command name for this button.
73 */
74private String actionCommand;
75
76/**
77 * @serial The label for this button.
78 */
79private String label;
80
81// List of ActionListeners for this class.
82private transient ActionListener action_listeners;
83
84/*************************************************************************/
85
86/*
87 * Constructors
88 */
89
90/**
91 * Initializes a new instance of <code>Button</code> with no label.
92 */
93public
94Button()
95{
96 this(null);
97}
98
99/*************************************************************************/
100
101/**
102 * Initializes a new instance of <code>Button</code> with the specified
103 * label. The action command name is also initialized to this value.
104 *
105 * @param label The label to display on the button.
106 */
107public
108Button(String label)
109{
110 this.label = label;
111 actionCommand = label;
112}
113
114/*************************************************************************/
115
116/*
117 * Instance Variables
118 */
119
120/**
121 * Returns the label for this button.
122 *
123 * @return The label for this button.
124 */
125public String
126getLabel()
127{
128 return(label);
129}
130
131/*************************************************************************/
132
133/**
134 * Sets the label for this button to the specified value.
135 *
136 * @param label The new label for this button.
137 */
138public synchronized void
139setLabel(String label)
140{
141 this.label = label;
142 if (peer != null)
143 {
144 ButtonPeer bp = (ButtonPeer) peer;
145 bp.setLabel (label);
146 }
147}
148
149/*************************************************************************/
150
151/**
152 * Returns the action command name for this button.
153 *
154 * @return The action command name for this button.
155 */
156public String
157getActionCommand()
158{
159 return(actionCommand);
160}
161
162/*************************************************************************/
163
164/**
165 * Sets the action command name for this button to the specified value.
166 *
167 * @param actionCommand The new action command name.
168 */
169public void
170setActionCommand(String actionCommand)
171{
172 this.actionCommand = actionCommand == null ? label : actionCommand;
173}
174
175/*************************************************************************/
176
177/**
178 * Adds a new entry to the list of listeners that will receive
179 * action events from this button.
180 *
181 * @param listener The listener to add.
182 */
183public synchronized void
184addActionListener(ActionListener listener)
185{
186 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
187}
188
189/*************************************************************************/
190
191/**
192 * Removes the specified listener from the list of listeners that will
193 * receive action events from this button.
194 *
195 * @param listener The listener to remove.
196 */
197public synchronized void
198removeActionListener(ActionListener listener)
199{
200 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
201}
202
203public EventListener[]
204getListeners(Class listenerType)
205{
206 if (listenerType == ActionListener.class)
207 return getListenersImpl(listenerType, action_listeners);
208 return super.getListeners(listenerType);
209}
210
211/*************************************************************************/
212
213/**
214 * Notifies this button that it should create its native peer object.
215 */
216public void
217addNotify()
218{
219 if (peer == null)
220 peer = getToolkit ().createButton (this);
221 super.addNotify();
222}
223
224/*************************************************************************/
225
226/**
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.
234 *
235 * @param event The event to process.
236 */
237protected void
238processEvent(AWTEvent event)
239{
240 if (event instanceof ActionEvent)
241 processActionEvent((ActionEvent)event);
242 else
243 super.processEvent(event);
244}
245
246/*************************************************************************/
247
248/**
249 * This method dispatches an action event for this button to any
250 * registered listeners.
251 *
252 * @param event The event to process.
253 */
254protected void
255processActionEvent(ActionEvent event)
256{
257 if (action_listeners != null)
258 action_listeners.actionPerformed(event);
259}
260
261void
262dispatchEventImpl(AWTEvent e)
263{
264 super.dispatchEventImpl(e);
265
266 if (e.id <= ActionEvent.ACTION_LAST
267 && e.id >= ActionEvent.ACTION_FIRST
268 && (action_listeners != null
269 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
270 processEvent(e);
271}
272
273/*************************************************************************/
274
275/**
276 * Returns a debugging string for this button.
277 *
278 * @return A debugging string for this button.
279 */
280protected String
281paramString()
282{
283 return ("label=" + getLabel() + ",actionCommand=" + getActionCommand()
284 + "," + super.paramString());
285}
286
287} // class Button
288
Note: See TracBrowser for help on using the repository browser.