001: /*
002: * @(#)Button.java 1.57 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.awt;
029:
030: import sun.awt.peer.ButtonPeer;
031: import sun.awt.PeerBasedToolkit;
032: import java.awt.event.*;
033: import java.io.ObjectOutputStream;
034: import java.io.ObjectInputStream;
035: import java.io.IOException;
036: import java.util.EventListener;
037:
038: /**
039: * This class creates a labeled button. The application can cause
040: * some action to happen when the button is pushed. This image
041: * depicts three views of a "<code>Quit</code>" button as it appears
042: * under the Solaris operating system:
043: * <p>
044: * <img src="images-awt/Button-1.gif"
045: * ALIGN=center HSPACE=10 VSPACE=7>
046: * <p>
047: * The first view shows the button as it appears normally.
048: * The second view shows the button
049: * when it has input focus. Its outline is darkened to let the
050: * user know that it is an active object. The third view shows the
051: * button when the user clicks the mouse over the button, and thus
052: * requests that an action be performed.
053: * <p>
054: * The gesture of clicking on a button with the mouse
055: * is associated with one instance of <code>ActionEvent</code>,
056: * which is sent out when the mouse is both pressed and released
057: * over the button. If an application is interested in knowing
058: * when the button has been pressed but not released, as a separate
059: * gesture, it can specialize <code>processMouseEvent</code>,
060: * or it can register itself as a listener for mouse events by
061: * calling <code>addMouseListener</code>. Both of these methods are
062: * defined by <code>Component</code>, the abstract superclass of
063: * all components.
064: * <p>
065: * When a button is pressed and released, AWT sends an instance
066: * of <code>ActionEvent</code> to the button, by calling
067: * <code>processEvent</code> on the button. The button's
068: * <code>processEvent</code> method receives all events
069: * for the button; it passes an action event along by
070: * calling its own <code>processActionEvent</code> method.
071: * The latter method passes the action event on to any action
072: * listeners that have registered an interest in action
073: * events generated by this button.
074: * <p>
075: * If an application wants to perform some action based on
076: * a button being pressed and released, it should implement
077: * <code>ActionListener</code> and register the new listener
078: * to receive events from this button, by calling the button's
079: * <code>addActionListener</code> method. The application can
080: * make use of the button's action command as a messaging protocol.
081: *
082: * @version 1.39 06/12/97
083: * @author Sami Shaio
084: * @see java.awt.event.ActionEvent
085: * @see java.awt.event.ActionListener
086: * @see java.awt.Component#processMouseEvent
087: * @see java.awt.Component#addMouseListener
088: * @since JDK1.0
089: */
090: public class Button extends Component {
091: String label;
092: String actionCommand;
093: transient ActionListener actionListener;
094: private static final String base = "button";
095: private static int nameCounter = 0;
096: /*
097: * JDK 1.1 serialVersionUID
098: */
099: private static final long serialVersionUID = -8774683716313001058L;
100:
101: /**
102: * Constructs a Button with no label.
103: * @since JDK1.0
104: */
105: public Button() {
106: this ("");
107: }
108:
109: /**
110: * Constructs a Button with the specified label.
111: * @param label A string label for the button.
112: * @since JDK1.0
113: */
114: public Button(String label) {
115: this .label = label;
116: }
117:
118: /**
119: * Construct a name for this component. Called by getName() when the
120: * name is null.
121: */
122: String constructComponentName() {
123: return base + nameCounter++;
124: }
125:
126: /**
127: * Creates the peer of the button. The button's peer allows the
128: * application to change the look of the button without changing
129: * its functionality.
130: * @see java.awt.Toolkit#createButton(java.awt.Button)
131: * @see java.awt.Component#getToolkit()
132: * @since JDK1.0
133: */
134: public void addNotify() {
135: synchronized (getTreeLock()) {
136: if (peer == null)
137: peer = ((PeerBasedToolkit) getToolkit())
138: .createButton(this );
139: super .addNotify();
140: }
141: }
142:
143: /**
144: * Gets the label of this button.
145: * @return the button's label, or <code>null</code>
146: * if the button has no label.
147: * @see java.awt.Button#setLabel
148: * @since JDK1.0
149: */
150: public String getLabel() {
151: return label;
152: }
153:
154: /**
155: * Sets the button's label to be the specified string.
156: * @param label the new label, or <code>null</code>
157: * if the button has no label.
158: * @see java.awt.Button#getLabel
159: * @since JDK1.0
160: */
161: public synchronized void setLabel(String label) {
162: this .label = label;
163: ButtonPeer peer = (ButtonPeer) this .peer;
164: if (peer != null) {
165: peer.setLabel(label);
166: }
167: }
168:
169: /**
170: * Sets the command name for the action event fired
171: * by this button. By default this action command is
172: * set to match the label of the button.
173: * @param command A string used to set the button's
174: * action command.
175: * @see java.awt.event.ActionEvent
176: * @since JDK1.1
177: */
178: public void setActionCommand(String command) {
179: actionCommand = command;
180: }
181:
182: /**
183: * Returns the command name of the action event fired by this button.
184: */
185: public String getActionCommand() {
186: return (actionCommand == null ? label : actionCommand);
187: }
188:
189: /**
190: * Adds the specified action listener to receive action events from
191: * this button. Action events occur when a user presses or releases
192: * the mouse over this button.
193: * @param l the action listener.
194: * @see java.awt.event.ActionListener
195: * @see java.awt.Button#removeActionListener
196: * @since JDK1.1
197: */
198: public synchronized void addActionListener(ActionListener l) {
199: actionListener = AWTEventMulticaster.add(actionListener, l);
200: newEventsOnly = true;
201: }
202:
203: /**
204: * Removes the specified action listener so that it no longer
205: * receives action events from this button. Action events occur
206: * when a user presses or releases the mouse over this button.
207: * @param l the action listener.
208: * @see java.awt.event.ActionListener
209: * @see java.awt.Button#addActionListener
210: * @since JDK1.1
211: */
212: public synchronized void removeActionListener(ActionListener l) {
213: actionListener = AWTEventMulticaster.remove(actionListener, l);
214: }
215:
216: /**
217: * Returns an array of all the action listeners
218: * registered on this button.
219: *
220: * @return all of this button's <code>ActionListener</code>s
221: * or an empty array if no action
222: * listeners are currently registered
223: *
224: * @see #addActionListener
225: * @see #removeActionListener
226: * @see java.awt.event.ActionListener
227: * @since 1.4
228: */
229: public synchronized ActionListener[] getActionListeners() {
230: return (ActionListener[]) AWTEventMulticaster.getListeners(
231: (EventListener) actionListener, ActionListener.class);
232: }
233:
234: // NOTE: remove when filtering is done at lower level
235: boolean eventEnabled(AWTEvent e) {
236: if (e.id == ActionEvent.ACTION_PERFORMED) {
237: if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0
238: || actionListener != null) {
239: return true;
240: }
241: return false;
242: }
243: return super .eventEnabled(e);
244: }
245:
246: /**
247: * Processes events on this button. If an event is
248: * an instance of <code>ActionEvent</code>, this method invokes
249: * the <code>processActionEvent</code> method. Otherwise,
250: * it invokes <code>processEvent</code> on the superclass.
251: * @param e the event.
252: * @see java.awt.event.ActionEvent
253: * @see java.awt.Button#processActionEvent
254: * @since JDK1.1
255: */
256: protected void processEvent(AWTEvent e) {
257: if (e instanceof ActionEvent) {
258: processActionEvent((ActionEvent) e);
259: return;
260: }
261: super .processEvent(e);
262: }
263:
264: /**
265: * Processes action events occurring on this button
266: * by dispatching them to any registered
267: * <code>ActionListener</code> objects.
268: * <p>
269: * This method is not called unless action events are
270: * enabled for this button. Action events are enabled
271: * when one of the following occurs:
272: * <p><ul>
273: * <li>An <code>ActionListener</code> object is registered
274: * via <code>addActionListener</code>.
275: * <li>Action events are enabled via <code>enableEvents</code>.
276: * </ul>
277: * @param e the action event.
278: * @see java.awt.event.ActionListener
279: * @see java.awt.Button#addActionListener
280: * @see java.awt.Component#enableEvents
281: * @since JDK1.1
282: */
283: protected void processActionEvent(ActionEvent e) {
284: if (actionListener != null) {
285: actionListener.actionPerformed(e);
286: }
287: }
288:
289: /**
290: * Returns the parameter string representing the state of this
291: * button. This string is useful for debugging.
292: * @return the parameter string of this button.
293: * @since JDK1.0
294: */
295: protected String paramString() {
296: return super .paramString() + ",label=" + label;
297: }
298:
299: /* Serialization support.
300: */
301:
302: private int buttonSerializedDataVersion = 1;
303:
304: private void writeObject(ObjectOutputStream s) throws IOException {
305: s.defaultWriteObject();
306: AWTEventMulticaster.save(s, actionListenerK, actionListener);
307: s.writeObject(null);
308: }
309:
310: private void readObject(ObjectInputStream s)
311: throws ClassNotFoundException, IOException {
312: s.defaultReadObject();
313: Object keyOrNull;
314: while (null != (keyOrNull = s.readObject())) {
315: String key = ((String) keyOrNull).intern();
316: if (actionListenerK == key)
317: addActionListener((ActionListener) (s.readObject()));
318: else
319: // skip value for unrecognized key
320: s.readObject();
321: }
322: }
323: }
|