| 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.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 | */
|
|---|
| 53 | public 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
|
|---|
| 63 | private 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 | */
|
|---|
| 74 | private String actionCommand;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * @serial The label for this button.
|
|---|
| 78 | */
|
|---|
| 79 | private String label;
|
|---|
| 80 |
|
|---|
| 81 | // List of ActionListeners for this class.
|
|---|
| 82 | private 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 | */
|
|---|
| 93 | public
|
|---|
| 94 | Button()
|
|---|
| 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 | */
|
|---|
| 107 | public
|
|---|
| 108 | Button(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 | */
|
|---|
| 125 | public String
|
|---|
| 126 | getLabel()
|
|---|
| 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 | */
|
|---|
| 138 | public synchronized void
|
|---|
| 139 | setLabel(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 | */
|
|---|
| 156 | public String
|
|---|
| 157 | getActionCommand()
|
|---|
| 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 | */
|
|---|
| 169 | public void
|
|---|
| 170 | setActionCommand(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 | */
|
|---|
| 183 | public synchronized void
|
|---|
| 184 | addActionListener(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 | */
|
|---|
| 197 | public synchronized void
|
|---|
| 198 | removeActionListener(ActionListener listener)
|
|---|
| 199 | {
|
|---|
| 200 | action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | public EventListener[]
|
|---|
| 204 | getListeners(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 | */
|
|---|
| 216 | public void
|
|---|
| 217 | addNotify()
|
|---|
| 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 | */
|
|---|
| 237 | protected void
|
|---|
| 238 | processEvent(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 | */
|
|---|
| 254 | protected void
|
|---|
| 255 | processActionEvent(ActionEvent event)
|
|---|
| 256 | {
|
|---|
| 257 | if (action_listeners != null)
|
|---|
| 258 | action_listeners.actionPerformed(event);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | void
|
|---|
| 262 | dispatchEventImpl(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 | */
|
|---|
| 280 | protected String
|
|---|
| 281 | paramString()
|
|---|
| 282 | {
|
|---|
| 283 | return ("label=" + getLabel() + ",actionCommand=" + getActionCommand()
|
|---|
| 284 | + "," + super.paramString());
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | } // class Button
|
|---|
| 288 |
|
|---|