jdk/src/share/classes/java/awt/Label.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1183 80d6aafba03a
child 20451 4cedf4e1560a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1183
diff changeset
     2
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A <code>Label</code> object is a component for placing text in a
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;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <img src="doc-files/Label-1.gif" alt="Two labels: 'Hi There!' and 'Another label'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * ALIGN=center HSPACE=10 VSPACE=7>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author      Sami Shaio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since       JDK1.0
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
     * @since   JDK1.0t.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int RIGHT       = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The text of this label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * This text can be modified by the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * but never by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see #getText()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see #setText(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The label's alignment.  The default alignment is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * to be left justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see #getAlignment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see #setAlignment(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    int    alignment = LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final String base = "label";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private static int nameCounter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     private static final long serialVersionUID = 3094126758329070636L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Constructs an empty label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The text of the label is the empty string <code>""</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Label() throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        this("", LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Constructs a new label with the specified string of text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * left justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param text the string that the label presents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *        A <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *        will be accepted without causing a NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *        to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public Label(String text) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this(text, LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs a new label that presents the specified string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * text with the specified alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Possible values for <code>alignment</code> are <code>Label.LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param text the string that the label presents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *        A <code>null</code> value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *        will be accepted without causing a NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *        to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param     alignment   the alignment value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception HeadlessException if GraphicsEnvironment.isHeadless()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public Label(String text, int alignment) throws HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        setAlignment(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Read a label from an object input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>GraphicsEnvironment.isHeadless()</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        throws ClassNotFoundException, IOException, HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Construct a name for this component.  Called by getName() when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * name is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    String constructComponentName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        synchronized (Label.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return base + nameCounter++;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Creates the peer for this label.  The peer allows us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * modify the appearance of the label without changing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void addNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        synchronized (getTreeLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (peer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                peer = getToolkit().createLabel(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            super.addNotify();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Gets the current alignment of this label. Possible values are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <code>Label.LEFT</code>, <code>Label.RIGHT</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <code>Label.CENTER</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see        java.awt.Label#setAlignment
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Possible values are <code>Label.LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <code>Label.RIGHT</code>, and <code>Label.CENTER</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *                          <code>alignment</code> is given.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @return     the text of this label, or <code>null</code> if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *             the text has been set to <code>null</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *             <code>text</code> is <code>null</code>, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *             treated for display purposes like an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *             string <code>""</code>.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Returns a string representing the state of this <code>Label</code>.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>null</code>.
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() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        String str = ",align=";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        switch (alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
          case LEFT:   str += "left"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
          case CENTER: str += "center"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
          case RIGHT:  str += "right"; break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return super.paramString() + str + ",text=" + text;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <code>Label</code> class.  It provides an implementation of the
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        public AccessibleAWTLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * @see AccessibleContext#setAccessibleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (accessibleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                return accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (getText() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    return super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    return getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * @return an instance of AccessibleRole describing the role of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return AccessibleRole.LABEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    } // inner class AccessibleAWTLabel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}