src/java.desktop/share/classes/java/awt/Button.java
author serb
Mon, 09 Sep 2019 12:23:22 -0700
changeset 58325 d32a3b1ca84a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8225372: accessibility errors in tables in java.desktop files Reviewed-by: aivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58325
d32a3b1ca84a 8225372: accessibility errors in tables in java.desktop files
serb
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 2019, 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;
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30471
diff changeset
    29
import java.beans.BeanProperty;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.EventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class creates a labeled button. The application can cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * some action to happen when the button is pushed. This image
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    40
 * depicts three views of a "{@code Quit}" button as it appears
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * under the Solaris operating system:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
58325
d32a3b1ca84a 8225372: accessibility errors in tables in java.desktop files
serb
parents: 47216
diff changeset
    43
 * <img src="doc-files/Button-1.gif" alt="The following context describes the
d32a3b1ca84a 8225372: accessibility errors in tables in java.desktop files
serb
parents: 47216
diff changeset
    44
 * graphic" style="margin: 7px 10px;">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * The first view shows the button as it appears normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The second view shows the button
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * when it has input focus. Its outline is darkened to let the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * user know that it is an active object. The third view shows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * button when the user clicks the mouse over the button, and thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * requests that an action be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The gesture of clicking on a button with the mouse
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    54
 * is associated with one instance of {@code ActionEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * which is sent out when the mouse is both pressed and released
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * over the button. If an application is interested in knowing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * when the button has been pressed but not released, as a separate
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    58
 * gesture, it can specialize {@code processMouseEvent},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * or it can register itself as a listener for mouse events by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    60
 * calling {@code addMouseListener}. Both of these methods are
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    61
 * defined by {@code Component}, the abstract superclass of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * all components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * When a button is pressed and released, AWT sends an instance
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    65
 * of {@code ActionEvent} to the button, by calling
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    66
 * {@code processEvent} on the button. The button's
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    67
 * {@code processEvent} method receives all events
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * for the button; it passes an action event along by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    69
 * calling its own {@code processActionEvent} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The latter method passes the action event on to any action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * listeners that have registered an interest in action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * events generated by this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * If an application wants to perform some action based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * a button being pressed and released, it should implement
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    76
 * {@code ActionListener} and register the new listener
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * to receive events from this button, by calling the button's
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
    78
 * {@code addActionListener} method. The application can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * make use of the button's action command as a messaging protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see         java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see         java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see         java.awt.Component#processMouseEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see         java.awt.Component#addMouseListener
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    86
 * @since       1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public class Button extends Component implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * The button's label.  This value may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @see #getLabel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see #setLabel(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    String label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The action to be performed once a button has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * pressed.  This value may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see #getActionCommand()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see #setActionCommand(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    String actionCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    transient ActionListener actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final String base = "button";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static final long serialVersionUID = -8774683716313001058L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            initIDs();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Constructs a button with an empty string for its label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Button() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Constructs a button with the specified label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param label  a string label for the button, or
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   147
     *               {@code null} for no label
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public Button(String label) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.label = label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Construct a name for this component.  Called by getName() when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * name is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        synchronized (Button.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return base + nameCounter++;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Creates the peer of the button.  The button's peer allows the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * application to change the look of the button without changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * its functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
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)
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 28059
diff changeset
   177
                peer = getComponentFactory().createButton(this);
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   185
     * @return    the button's label, or {@code null}
2
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
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   196
     * @param     label   the new label, or {@code null}
2
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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   228
     *            If the string is {@code null} then the action command
2
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
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   231
     * @since     1.1
2
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.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   239
     * If the command name is {@code null} (default) then this method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * returns the label of the button.
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   241
     *
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   242
     * @return the action command name (or label) for this button
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public String getActionCommand() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return (actionCommand == null? label : actionCommand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Adds the specified action listener to receive action events from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * this button. Action events occur when a user presses or releases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the mouse over this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param         l the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see           #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @see           #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @see           java.awt.event.ActionListener
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   260
     * @since         1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public synchronized void addActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        actionListener = AWTEventMulticaster.add(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        newEventsOnly = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Removes the specified action listener so that it no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * receives action events from this button. Action events occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * when a user presses or releases the mouse over this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * If l is null, no exception is thrown and no action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * >AWT Threading Issues</a> for details on AWT's threading model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param           l     the action listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @see             #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see             #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @see             java.awt.event.ActionListener
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   282
     * @since           1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public synchronized void removeActionListener(ActionListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        actionListener = AWTEventMulticaster.remove(actionListener, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns an array of all the action listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * registered on this button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   295
     * @return all of this button's {@code ActionListener}s
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *         or an empty array if no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *         listeners are currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see             #addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see             #removeActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see             java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public synchronized ActionListener[] getActionListeners() {
15318
607db339afcc 8005492: Reduce number of warnings in sun/awt/* classes
mcherkas
parents: 5506
diff changeset
   305
        return getListeners(ActionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Returns an array of all the objects currently registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * as <code><em>Foo</em>Listener</code>s
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   311
     * upon this {@code Button}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <code><em>Foo</em>Listener</code>s are registered using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>add<em>Foo</em>Listener</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   316
     * You can specify the {@code listenerType} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * with a class literal, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <code><em>Foo</em>Listener.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * For example, you can query a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   320
     * {@code Button b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * for its action listeners with the following code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <pre>ActionListener[] als = (ActionListener[])(b.getListeners(ActionListener.class));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * If no such listeners exist, this method returns an empty array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param listenerType the type of listeners requested; this parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *          should specify an interface that descends from
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   329
     *          {@code java.util.EventListener}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return an array of all objects registered as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          <code><em>Foo</em>Listener</code>s on this button,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *          or an empty array if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          listeners have been added
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   334
     * @exception ClassCastException if {@code listenerType}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *          doesn't specify a class or interface that implements
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   336
     *          {@code java.util.EventListener}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @see #getActionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        EventListener l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if  (listenerType == ActionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            l = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return super.getListeners(listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return AWTEventMulticaster.getListeners(l, listenerType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // REMIND: remove when filtering is done at lower level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    boolean eventEnabled(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (e.id == ActionEvent.ACTION_PERFORMED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                actionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return super.eventEnabled(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Processes events on this button. If an event is
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   365
     * an instance of {@code ActionEvent}, this method invokes
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   366
     * the {@code processActionEvent} method. Otherwise,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   367
     * it invokes {@code processEvent} on the superclass.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   368
     * <p>Note that if the event parameter is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @param        e the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @see          java.awt.event.ActionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @see          java.awt.Button#processActionEvent
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   375
     * @since        1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    protected void processEvent(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (e instanceof ActionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            processActionEvent((ActionEvent)e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        super.processEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Processes action events occurring on this button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * by dispatching them to any registered
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   388
     * {@code ActionListener} objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * This method is not called unless action events are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * enabled for this button. Action events are enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * when one of the following occurs:
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 20451
diff changeset
   393
     * <ul>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   394
     * <li>An {@code ActionListener} object is registered
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   395
     * via {@code addActionListener}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   396
     * <li>Action events are enabled via {@code enableEvents}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * </ul>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   398
     * <p>Note that if the event parameter is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the behavior is unspecified and may result in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param       e the action event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see         java.awt.event.ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see         java.awt.Button#addActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see         java.awt.Component#enableEvents
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   406
     * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    protected void processActionEvent(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        ActionListener listener = actionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (listener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            listener.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   416
     * Returns a string representing the state of this {@code Button}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * implementations. The returned string may be empty but may not be
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   420
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return     the parameter string of this button
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return super.paramString() + ",label=" + label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /* Serialization support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Button Serial Data Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private int buttonSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Writes default serializable fields to stream.  Writes
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   440
     * a list of serializable {@code ActionListeners}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * as optional data.  The non-serializable
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   442
     * {@code ActionListeners} are detected and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * no attempt is made to serialize them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   445
     * @serialData {@code null} terminated sequence of 0 or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   446
     *   more pairs: the pair consists of a {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   447
     *   and an {@code Object}; the {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *   indicates the type of object and is one of the following:
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   449
     *   {@code actionListenerK} indicating an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   450
     *     {@code ActionListener} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   452
     * @param s the {@code ObjectOutputStream} to write
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see java.awt.Component#actionListenerK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @see #readObject(ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
      throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
      s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
      AWTEventMulticaster.save(s, actionListenerK, actionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
      s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   467
     * Reads the {@code ObjectInputStream} and if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   468
     * it isn't {@code null} adds a listener to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * receive action events fired by the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Unrecognized keys or values will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   472
     * @param s the {@code ObjectInputStream} to read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @exception HeadlessException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   474
     *   {@code GraphicsEnvironment.isHeadless} returns
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   475
     *   {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see #removeActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @see #addActionListener(ActionListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @see #writeObject(ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      throws ClassNotFoundException, IOException, HeadlessException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
      GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
      Object keyOrNull;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
      while(null != (keyOrNull = s.readObject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        String key = ((String)keyOrNull).intern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (actionListenerK == key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
          addActionListener((ActionListener)(s.readObject()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        else // skip value for unrecognized key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
          s.readObject();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   506
     * Gets the {@code AccessibleContext} associated with
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   507
     * this {@code Button}. For buttons, the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   508
     * {@code AccessibleContext} takes the form of an
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   509
     * {@code AccessibleAWTButton}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   510
     * A new {@code AccessibleAWTButton} instance is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   513
     * @return an {@code AccessibleAWTButton} that serves as the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   514
     *         {@code AccessibleContext} of this {@code Button}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30471
diff changeset
   517
    @BeanProperty(expert = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 30471
diff changeset
   518
            = "The AccessibleContext associated with this Button.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            accessibleContext = new AccessibleAWTButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * This class implements accessibility support for the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 33253
diff changeset
   528
     * {@code Button} class.  It provides an implementation of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Java Accessibility API appropriate to button user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    protected class AccessibleAWTButton extends AccessibleAWTComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        implements AccessibleAction, AccessibleValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        private static final long serialVersionUID = -5932203980244017102L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (accessibleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                return accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                if (getLabel() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    return super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    return getLabel();
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * Get the AccessibleAction associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * AccessibleAction interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        public AccessibleAction getAccessibleAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * Get the AccessibleValue associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
         * AccessibleValue interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * Returns the number of Actions available in this object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         * default behavior of a button is to have one action - toggle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
         * the button.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         * @return 1, the number of Actions in this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        public int getAccessibleActionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         * Return a description of the specified action of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         * @param i zero-based index of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        public String getAccessibleActionDescription(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // [[[PENDING:  WDW -- need to provide a localized string]]]
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   601
                return "click";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                return null;
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * Perform the specified Action on the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * @param i zero-based index of actions
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   611
         * @return true if the action was performed; else false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        public boolean doAccessibleAction(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                // Simulate a button click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                Toolkit.getEventQueue().postEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                        new ActionEvent(Button.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                        ActionEvent.ACTION_PERFORMED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                        Button.this.getActionCommand()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                return false;
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
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * Get the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * @return An Integer of 0 if this isn't selected or an Integer of 1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * this is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * @see javax.swing.AbstractButton#isSelected()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        public Number getCurrentAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         * Set the value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * @return True if the value was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        public boolean setCurrentAccessibleValue(Number n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * Get the minimum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        public Number getMinimumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * Get the maximum value of this object as a Number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * @return An Integer of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        public Number getMaximumAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            return Integer.valueOf(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return AccessibleRole.PUSH_BUTTON;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    } // inner class AccessibleAWTButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
}