jdk/src/share/classes/java/awt/Button.java
author mcherkas
Wed, 16 Jan 2013 17:26:41 +0400
changeset 15318 607db339afcc
parent 5506 202f599c92aa
child 20451 4cedf4e1560a
permissions -rw-r--r--
8005492: Reduce number of warnings in sun/awt/* classes Reviewed-by: art, anthony
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
     2
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1188
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.peer.ButtonPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class creates a labeled button. The application can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * some action to happen when the button is pushed. This image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * depicts three views of a "<code>Quit</code>" button as it appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * under the Solaris operating system:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <img src="doc-files/Button-1.gif" alt="The following context describes the graphic"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ALIGN=center HSPACE=10 VSPACE=7>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The first view shows the button as it appears normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The second view shows the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * when it has input focus. Its outline is darkened to let the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * user know that it is an active object. The third view shows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * button when the user clicks the mouse over the button, and thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * requests that an action be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The gesture of clicking on a button with the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * is associated with one instance of <code>ActionEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * which is sent out when the mouse is both pressed and released
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * over the button. If an application is interested in knowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * when the button has been pressed but not released, as a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * gesture, it can specialize <code>processMouseEvent</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * or it can register itself as a listener for mouse events by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * calling <code>addMouseListener</code>. Both of these methods are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * defined by <code>Component</code>, the abstract superclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * all components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * When a button is pressed and released, AWT sends an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * of <code>ActionEvent</code> to the button, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>processEvent</code> on the button. The button's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>processEvent</code> method receives all events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * for the button; it passes an action event along by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * calling its own <code>processActionEvent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The latter method passes the action event on to any action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * listeners that have registered an interest in action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * events generated by this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * If an application wants to perform some action based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * a button being pressed and released, it should implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <code>ActionListener</code> and register the new listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * to receive events from this button, by calling the button's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>addActionListener</code> method. The application can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * make use of the button's action command as a messaging protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see         java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see         java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see         java.awt.Component#processMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see         java.awt.Component#addMouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @since       JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
public class Button extends Component implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The button's label.  This value may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see #getLabel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @see #setLabel(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    String label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * The action to be performed once a button has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * pressed.  This value may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see #getActionCommand()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    String actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    transient ActionListener actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private static final String base = "button";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final long serialVersionUID = -8774683716313001058L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Constructs a button with an empty string for its label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public Button() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Constructs a button with the specified label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param label  a string label for the button, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *               <code>null</code> for no label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Button(String label) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Construct a name for this component.  Called by getName() when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        synchronized (Button.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Creates the peer of the button.  The button's peer allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * application to change the look of the button without changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @see     java.awt.Toolkit#createButton(java.awt.Button)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see     java.awt.Component#getToolkit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        synchronized(getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                peer = getToolkit().createButton(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Gets the label of this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return    the button's label, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *                if the button has no label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see       java.awt.Button#setLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public String getLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Sets the button's label to be the specified string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @param     label   the new label, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                if the button has no label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see       java.awt.Button#getLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void setLabel(String label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        boolean testvalid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            if (label != this.label && (this.label == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                        !this.label.equals(label))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                ButtonPeer peer = (ButtonPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    peer.setLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                testvalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // This could change the preferred size of the Component.
1183
80d6aafba03a 6682046: Mixing code does not always recalculate shapes correctly when resizing components
anthony
parents: 438
diff changeset
   216
        if (testvalid) {
80d6aafba03a 6682046: Mixing code does not always recalculate shapes correctly when resizing components
anthony
parents: 438
diff changeset
   217
            invalidateIfValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Sets the command name for the action event fired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * by this button. By default this action command is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * set to match the label of the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param     command  a string used to set the button's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *                  action command.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *            If the string is <code>null</code> then the action command
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *            is set to match the label of the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @see       java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void setActionCommand(String command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        actionCommand = command;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * Returns the command name of the action event fired by this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * If the command name is <code>null</code> (default) then this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * returns the label of the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public String getActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        return (actionCommand == null? label : actionCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Adds the specified action listener to receive action events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * this button. Action events occur when a user presses or releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * the mouse over this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param         l the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @see           #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @see           #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see           java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @since         JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public synchronized void addActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        actionListener = AWTEventMulticaster.add(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Removes the specified action listener so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * receives action events from this button. Action events occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * when a user presses or releases the mouse over this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param           l     the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @see             #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @see             #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see             java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @since           JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public synchronized void removeActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        actionListener = AWTEventMulticaster.remove(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Returns an array of all the action listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * registered on this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return all of this button's <code>ActionListener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *         or an empty array if no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see             #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see             #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see             java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public synchronized ActionListener[] getActionListeners() {
15318
607db339afcc 8005492: Reduce number of warnings in sun/awt/* classes
mcherkas
parents: 5506
diff changeset
   303
        return getListeners(ActionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * as <code><em>Foo</em>Listener</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * upon this <code>Button</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * You can specify the <code>listenerType</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * For example, you can query a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <code>Button</code> <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * for its action listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <pre>ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *          should specify an interface that descends from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *          <code><em>Foo</em>Listener</code>s on this button,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @exception ClassCastException if <code>listenerType</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          doesn't specify a class or interface that implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          <code>java.util.EventListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @see #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if  (listenerType == ActionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            l = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return super.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (e.id == ActionEvent.ACTION_PERFORMED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                actionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Processes events on this button. If an event is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * an instance of <code>ActionEvent</code>, this method invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * the <code>processActionEvent</code> method. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * it invokes <code>processEvent</code> on the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param        e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see          java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see          java.awt.Button#processActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @since        JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (e instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            processActionEvent((ActionEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Processes action events occurring on this button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * by dispatching them to any registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <code>ActionListener</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * This method is not called unless action events are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * enabled for this button. Action events are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * when one of the following occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <li>An <code>ActionListener</code> object is registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * via <code>addActionListener</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <li>Action events are enabled via <code>enableEvents</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>Note that if the event parameter is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param       e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see         java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @see         java.awt.Button#addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see         java.awt.Component#enableEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected void processActionEvent(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        ActionListener listener = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            listener.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Returns a string representing the state of this <code>Button</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * implementations. The returned string may be empty but may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @return     the parameter string of this button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return super.paramString() + ",label=" + label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /* Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Button Serial Data Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private int buttonSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Writes default serializable fields to stream.  Writes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * a list of serializable <code>ActionListeners</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * as optional data.  The non-serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <code>ActionListeners</code> are detected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * no attempt is made to serialize them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @serialData <code>null</code> terminated sequence of 0 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *   more pairs: the pair consists of a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *   and an <code>Object</code>; the <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *   indicates the type of object and is one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *   <code>actionListenerK</code> indicating an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *     <code>ActionListener</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param s the <code>ObjectOutputStream</code> to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @see java.awt.Component#actionListenerK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see #readObject(ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
      AWTEventMulticaster.save(s, actionListenerK, actionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
      s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Reads the <code>ObjectInputStream</code> and if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * it isn't <code>null</code> adds a listener to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * receive action events fired by the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Unrecognized keys or values will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param s the <code>ObjectInputStream</code> to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *   <code>GraphicsEnvironment.isHeadless</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *   <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @see #removeActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @see #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @see #writeObject(ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
      throws ClassNotFoundException, IOException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
      s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
      while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (actionListenerK == key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
          addActionListener((ActionListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        else // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
          s.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Gets the <code>AccessibleContext</code> associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * this <code>Button</code>. For buttons, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <code>AccessibleContext</code> takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * <code>AccessibleAWTButton</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * A new <code>AccessibleAWTButton</code> instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return an <code>AccessibleAWTButton</code> that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         <code>AccessibleContext</code> of this <code>Button</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *  description: The AccessibleContext associated with this Button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            accessibleContext = new AccessibleAWTButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <code>Button</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Java Accessibility API appropriate to button user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    protected class AccessibleAWTButton extends AccessibleAWTComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        implements AccessibleAction, AccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        private static final long serialVersionUID = -5932203980244017102L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            if (accessibleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                return accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if (getLabel() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    return super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    return getLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         * Get the AccessibleAction associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * AccessibleAction interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        public AccessibleAction getAccessibleAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * Get the AccessibleValue associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * AccessibleValue interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * Returns the number of Actions available in this object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * default behavior of a button is to have one action - toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * @return 1, the number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * Return a description of the specified action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // [[[PENDING:  WDW -- need to provide a localized string]]]
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   600
                return "click";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * Perform the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @param i zero-based index of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * @return true if the the action was performed; else false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                // Simulate a button click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        new ActionEvent(Button.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                        ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                        Button.this.getActionCommand()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * Get the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * @return An Integer of 0 if this isn't selected or an Integer of 1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * this is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * @see javax.swing.AbstractButton#isSelected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        public Number getCurrentAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * Set the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * @return True if the value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * Get the minimum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        public Number getMinimumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * Get the maximum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            return AccessibleRole.PUSH_BUTTON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    } // inner class AccessibleAWTButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
}