1 | /* Button.java -- AWT button widget
|
---|
2 | Copyright (C) 1999, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 |
|
---|
39 | package java.awt;
|
---|
40 |
|
---|
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.lang.reflect.Array;
|
---|
46 | import 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 | */
|
---|
54 | public 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
|
---|
64 | private 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 | */
|
---|
75 | private String actionCommand;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * @serial The label for this button.
|
---|
79 | */
|
---|
80 | private String label;
|
---|
81 |
|
---|
82 | // List of ActionListeners for this class.
|
---|
83 | private 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 | */
|
---|
97 | public
|
---|
98 | Button()
|
---|
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 | */
|
---|
114 | public
|
---|
115 | Button(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 | */
|
---|
135 | public String
|
---|
136 | getLabel()
|
---|
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 | */
|
---|
148 | public synchronized void
|
---|
149 | setLabel(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 | */
|
---|
166 | public String
|
---|
167 | getActionCommand()
|
---|
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 | */
|
---|
179 | public void
|
---|
180 | setActionCommand(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 | */
|
---|
193 | public synchronized void
|
---|
194 | addActionListener(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 | */
|
---|
207 | public synchronized void
|
---|
208 | removeActionListener(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 | */
|
---|
241 | public void
|
---|
242 | addNotify()
|
---|
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 | */
|
---|
262 | protected void
|
---|
263 | processEvent(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 | */
|
---|
279 | protected void
|
---|
280 | processActionEvent(ActionEvent event)
|
---|
281 | {
|
---|
282 | if (action_listeners != null)
|
---|
283 | action_listeners.actionPerformed(event);
|
---|
284 | }
|
---|
285 |
|
---|
286 | void
|
---|
287 | dispatchEventImpl(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 | */
|
---|
305 | protected String
|
---|
306 | paramString()
|
---|
307 | {
|
---|
308 | return ("label=" + getLabel() + ",actionCommand=" + getActionCommand()
|
---|
309 | + "," + super.paramString());
|
---|
310 | }
|
---|
311 |
|
---|
312 | } // class Button
|
---|
313 |
|
---|