src/java.desktop/share/classes/java/awt/Label.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: 1183
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: 1183
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: 1183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1183
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.peer.LabelPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
    33
 * A {@code Label} object is a component for placing text in a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * container. A label displays a single line of read-only text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The text can be changed by the application, but a user cannot edit it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * For example, the code&nbsp;.&nbsp;.&nbsp;.
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 20451
diff changeset
    39
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <hr><blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * add(new Label("Hi There!"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * add(new Label("Another Label"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * </pre></blockquote><hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * produces the following labels:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
58325
d32a3b1ca84a 8225372: accessibility errors in tables in java.desktop files
serb
parents: 47216
diff changeset
    48
 * <img src="doc-files/Label-1.gif" alt="Two labels: 'Hi There!' and
d32a3b1ca84a 8225372: accessibility errors in tables in java.desktop files
serb
parents: 47216
diff changeset
    49
 * 'Another label'" style="margin: 7px 10px;">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author      Sami Shaio
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23328
diff changeset
    52
 * @since       1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class Label extends Component implements Accessible {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Indicates that the label should be left justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int LEFT        = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Indicates that the label should be centered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int CENTER      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Indicates that the label should be right justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int RIGHT       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The text of this label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * This text can be modified by the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * but never by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #getText()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see #setText(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    String text;
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 label's alignment.  The default alignment is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * to be left justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @see #getAlignment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #setAlignment(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int    alignment = LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final String base = "label";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     private static final long serialVersionUID = 3094126758329070636L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Constructs an empty label.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   110
     * The text of the label is the empty string {@code ""}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public Label() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this("", LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Constructs a new label with the specified string of text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * left justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param text the string that the label presents.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   123
     *        A {@code null} value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *        will be accepted without causing a NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *        to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Label(String text) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this(text, LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Constructs a new label that presents the specified string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * text with the specified alignment.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   137
     * Possible values for {@code alignment} are {@code Label.LEFT},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   138
     * {@code Label.RIGHT}, and {@code Label.CENTER}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param text the string that the label presents.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   140
     *        A {@code null} value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *        will be accepted without causing a NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *        to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param     alignment   the alignment value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public Label(String text, int alignment) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        setAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Read a label from an object input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception HeadlessException if
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   157
     * {@code GraphicsEnvironment.isHeadless()} returns
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   158
     * {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throws ClassNotFoundException, IOException, HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Construct a name for this component.  Called by getName() when the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   171
     * name is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (Label.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return base + nameCounter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Creates the peer for this label.  The peer allows us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * modify the appearance of the label without changing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (peer == null)
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 25859
diff changeset
   187
                peer = getComponentFactory().createLabel(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            super.addNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Gets the current alignment of this label. Possible values are
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   194
     * {@code Label.LEFT}, {@code Label.RIGHT}, and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   195
     * {@code Label.CENTER}.
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   196
     * @return the alignment of this label
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   197
     * @see java.awt.Label#setAlignment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public int getAlignment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Sets the alignment for this label to the specified alignment.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   205
     * Possible values are {@code Label.LEFT},
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   206
     * {@code Label.RIGHT}, and {@code Label.CENTER}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param      alignment    the alignment to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception  IllegalArgumentException if an improper value for
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   209
     *                          {@code alignment} is given.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see        java.awt.Label#getAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public synchronized void setAlignment(int alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        switch (alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
          case LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
          case CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
          case RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            this.alignment = alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            LabelPeer peer = (LabelPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                peer.setAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        throw new IllegalArgumentException("improper alignment: " + alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Gets the text of this label.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   229
     * @return     the text of this label, or {@code null} if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   230
     *             the text has been set to {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see        java.awt.Label#setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        return text;
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
     * Sets the text for this label to the specified text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param      text the text that this label displays. If
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   240
     *             {@code text} is {@code null}, it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *             treated for display purposes like an empty
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   242
     *             string {@code ""}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @see        java.awt.Label#getText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void setText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean testvalid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (text != this.text && (this.text == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                      !this.text.equals(text))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                LabelPeer peer = (LabelPeer)this.peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    peer.setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                testvalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // 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: 2
diff changeset
   260
        if (testvalid) {
80d6aafba03a 6682046: Mixing code does not always recalculate shapes correctly when resizing components
anthony
parents: 2
diff changeset
   261
            invalidateIfValid();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   266
     * Returns a string representing the state of this {@code Label}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * implementations. The returned string may be empty but may not be
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   270
     * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return     the parameter string of this label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    protected String paramString() {
23299
6a715c2969cf 8034068: Label.toString performance improvement
serb
parents: 21957
diff changeset
   275
        String align = "";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        switch (alignment) {
23299
6a715c2969cf 8034068: Label.toString performance improvement
serb
parents: 21957
diff changeset
   277
            case LEFT:   align = "left"; break;
6a715c2969cf 8034068: Label.toString performance improvement
serb
parents: 21957
diff changeset
   278
            case CENTER: align = "center"; break;
6a715c2969cf 8034068: Label.toString performance improvement
serb
parents: 21957
diff changeset
   279
            case RIGHT:  align = "right"; break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
23299
6a715c2969cf 8034068: Label.toString performance improvement
serb
parents: 21957
diff changeset
   281
        return super.paramString() + ",align=" + align + ",text=" + text;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Gets the AccessibleContext associated with this Label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * For labels, the AccessibleContext takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * AccessibleAWTLabel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * A new AccessibleAWTLabel instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @return an AccessibleAWTLabel that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *         AccessibleContext of this Label
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            accessibleContext = new AccessibleAWTLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * This class implements accessibility support for the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 30471
diff changeset
   314
     * {@code Label} class.  It provides an implementation of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Java Accessibility API appropriate to label user-interface elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    protected class AccessibleAWTLabel extends AccessibleAWTComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * JDK 1.3 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        private static final long serialVersionUID = -3568967560160480438L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   325
        /**
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   326
         * Constructor for the accessible label.
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 24865
diff changeset
   327
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public AccessibleAWTLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * @see AccessibleContext#setAccessibleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (accessibleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                return accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (getText() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    return super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    return getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * @return an instance of AccessibleRole describing the role of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return AccessibleRole.LABEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    } // inner class AccessibleAWTLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}