jdk/src/share/classes/javax/swing/JLabel.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20458 f2423fb3fd19
child 21592 da6abe91602a
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
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: 2473
diff changeset
     2
 * Copyright (c) 1997, 2009, 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: 2473
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: 2473
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: 2473
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2473
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 javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.*;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    34
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.plaf.LabelUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.html.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.plaf.basic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * A display area for a short text string or an image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * A label does not react to input events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * As a result, it cannot get the keyboard focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * A label can, however, display a keyboard alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * as a convenience for a nearby component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * that has a keyboard alternative but can't display it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A <code>JLabel</code> object can display
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * either text, an image, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * You can specify where in the label's display area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the label's contents are aligned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * by setting the vertical and horizontal alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * By default, labels are vertically centered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * in their display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Text-only labels are leading edge aligned, by default;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * image-only labels are horizontally centered, by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * You can also specify the position of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * relative to the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * By default, text is on the trailing edge of the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * with the text and image vertically aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * A label's leading and trailing edge are determined from the value of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * {@link java.awt.ComponentOrientation} property.  At present, the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * ComponentOrientation setting maps the leading edge to left and the trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * edge to right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Finally, you can use the <code>setIconTextGap</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * to specify how many pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * should appear between the text and the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * The default is 4 pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 19360
diff changeset
    83
 * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/label.html">How to Use Labels</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * in <em>The Java Tutorial</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * for further documentation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
    97
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *   attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * description: A component that displays a short string and an icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
   107
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
public class JLabel extends JComponent implements SwingConstants, Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static final String uiClassID = "LabelUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private int mnemonic = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private int mnemonicIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    private String text = "";         // "" rather than null, for BeanBox
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Icon defaultIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private Icon disabledIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private boolean disabledIconSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private int verticalAlignment = CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private int horizontalAlignment = LEADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private int verticalTextPosition = CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private int horizontalTextPosition = TRAILING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private int iconTextGap = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    protected Component labelFor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Client property key used to determine what label is labeling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * component.  This is generally not used by labels, but is instead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * used by components such as text areas that are being labeled by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * labels.  When the labelFor property of a label is set, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * automatically set the LABELED_BY_PROPERTY of the component being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * labelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see #setLabelFor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static final String LABELED_BY_PROPERTY = "labeledBy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates a <code>JLabel</code> instance with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * text, image, and horizontal alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * The label is centered vertically in its display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The text is on the trailing edge of the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param text  The text to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param icon  The image to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param horizontalAlignment  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *           <code>LEADING</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public JLabel(String text, Icon icon, int horizontalAlignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        setText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        setHorizontalAlignment(horizontalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        setAlignmentX(LEFT_ALIGNMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Creates a <code>JLabel</code> instance with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * text and horizontal alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The label is centered vertically in its display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param text  The text to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param horizontalAlignment  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *           <code>LEADING</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public JLabel(String text, int horizontalAlignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this(text, null, horizontalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Creates a <code>JLabel</code> instance with the specified text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * The label is aligned against the leading edge of its display area,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * and centered vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param text  The text to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public JLabel(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this(text, null, LEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Creates a <code>JLabel</code> instance with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * image and horizontal alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * The label is centered vertically in its display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param image  The image to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param horizontalAlignment  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *           <code>LEADING</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public JLabel(Icon image, int horizontalAlignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this(null, image, horizontalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Creates a <code>JLabel</code> instance with the specified image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * The label is centered vertically and horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * in its display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param image  The image to be displayed by the label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public JLabel(Icon image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this(null, image, CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Creates a <code>JLabel</code> instance with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * no image and with an empty string for the title.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * The label is centered vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * in its display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * The label's contents, once set, will be displayed on the leading edge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * of the label's display area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public JLabel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this("", null, LEADING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns the L&F object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return LabelUI object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public LabelUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return (LabelUI)ui;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Sets the L&F object that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param ui  the LabelUI L&F object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *       hidden: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *  description: The UI object that implements the Component's LookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public void setUI(LabelUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        // disabled icon is generated by LF so it should be unset here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (!disabledIconSet && disabledIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            setDisabledIcon(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Resets the UI property to a value from the current look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see JComponent#updateUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        setUI((LabelUI)UIManager.getUI(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns a string that specifies the name of the l&f class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @return String "LabelUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns the text string that the label displays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see #setText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Defines the single line of text this component will display.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * the value of text is null or empty string, nothing is displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * The default value of this property is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #setVerticalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @see #setHorizontalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @see #setIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *  description: Defines the single line of text this component will display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public void setText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        String oldAccessibleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (accessibleContext != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            oldAccessibleName = accessibleContext.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        String oldValue = this.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        firePropertyChange("text", oldValue, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        setDisplayedMnemonicIndex(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                      SwingUtilities.findDisplayedMnemonicIndex(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                          text, getDisplayedMnemonic()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if ((accessibleContext != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            && (accessibleContext.getAccessibleName() != oldAccessibleName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        oldAccessibleName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        accessibleContext.getAccessibleName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (text == null || oldValue == null || !text.equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            repaint();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns the graphic image (glyph, icon) that the label displays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return an Icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @see #setIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public Icon getIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return defaultIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Defines the icon this component will display.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * the value of icon is null, nothing is displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * The default value of this property is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #setVerticalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @see #setHorizontalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @see #getIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *    preferred: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *  description: The icon this component will display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public void setIcon(Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Icon oldValue = defaultIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        defaultIcon = icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        /* If the default icon has really changed and we had
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * generated the disabled icon for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * (in other words, setDisabledIcon() was never called), then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * clear the disabledIcon field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if ((defaultIcon != oldValue) && !disabledIconSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            disabledIcon = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        firePropertyChange("icon", oldValue, defaultIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if ((accessibleContext != null) && (oldValue != defaultIcon)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                accessibleContext.firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                        AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        oldValue, defaultIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        /* If the default icon has changed and the new one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * a different size, then revalidate.   Repaint if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * default icon has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (defaultIcon != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if ((defaultIcon == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                (oldValue == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                (defaultIcon.getIconWidth() != oldValue.getIconWidth()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                (defaultIcon.getIconHeight() != oldValue.getIconHeight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns the icon used by the label when it's disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * If no disabled icon has been set this will forward the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * the look and feel to construct an appropriate disabled Icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Some look and feels might not render the disabled Icon, in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * case they will ignore this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return the <code>disabledIcon</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see #setDisabledIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @see javax.swing.LookAndFeel#getDisabledIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @see ImageIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   427
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public Icon getDisabledIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (!disabledIconSet && disabledIcon == null && defaultIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, defaultIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (disabledIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                firePropertyChange("disabledIcon", null, disabledIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        return disabledIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Set the icon to be displayed if this JLabel is "disabled"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * (JLabel.setEnabled(false)).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * The default value of this property is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param disabledIcon the Icon to display when the component is disabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see #getDisabledIcon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @see #setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *  description: The icon to display if the label is disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public void setDisabledIcon(Icon disabledIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        Icon oldValue = this.disabledIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        this.disabledIcon = disabledIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        disabledIconSet = (disabledIcon != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        firePropertyChange("disabledIcon", oldValue, disabledIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (disabledIcon != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            if (disabledIcon == null || oldValue == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                disabledIcon.getIconWidth() != oldValue.getIconWidth() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                disabledIcon.getIconHeight() != oldValue.getIconHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (!isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Specify a keycode that indicates a mnemonic key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * This property is used when the label is part of a larger component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * If the labelFor property of the label is not null, the label will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * call the requestFocus method of the component specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * labelFor property when the mnemonic is activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @see #getLabelFor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @see #setLabelFor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *  description: The mnemonic keycode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void setDisplayedMnemonic(int key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        int oldKey = mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        mnemonic = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        firePropertyChange("displayedMnemonic", oldKey, mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        setDisplayedMnemonicIndex(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            SwingUtilities.findDisplayedMnemonicIndex(getText(), mnemonic));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if (key != oldKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Specifies the displayedMnemonic as a char value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @param aChar  a char specifying the mnemonic to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * @see #setDisplayedMnemonic(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    public void setDisplayedMnemonic(char aChar) {
2473
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 466
diff changeset
   507
        int vk = java.awt.event.KeyEvent.getExtendedKeyCodeForChar(aChar);
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 466
diff changeset
   508
        if (vk != java.awt.event.KeyEvent.VK_UNDEFINED) {
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 466
diff changeset
   509
            setDisplayedMnemonic(vk);
3f4bbd3be2f1 6680988: KeyEvent is still missing VK values for many keyboards
yan
parents: 466
diff changeset
   510
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Return the keycode that indicates a mnemonic key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * This property is used when the label is part of a larger component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * If the labelFor property of the label is not null, the label will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * call the requestFocus method of the component specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * labelFor property when the mnemonic is activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return int value for the mnemonic key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @see #getLabelFor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @see #setLabelFor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public int getDisplayedMnemonic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        return mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Provides a hint to the look and feel as to which character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * text should be decorated to represent the mnemonic. Not all look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * feels may support this. A value of -1 indicates either there is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * mnemonic, the mnemonic character is not contained in the string, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * the developer does not wish the mnemonic to be displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * The value of this is updated as the properties relating to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * mnemonic change (such as the mnemonic itself, the text...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * You should only ever have to call this if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * you do not wish the default character to be underlined. For example, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * to be decorated, as 'Save <u>A</u>s', you would have to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <code>setDisplayedMnemonicIndex(5)</code> after invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <code>setDisplayedMnemonic(KeyEvent.VK_A)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param index Index into the String to underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @exception IllegalArgumentException will be thrown if <code>index</code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *            is >= length of the text, or < -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *  description: the index into the String to draw the keyboard character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *               mnemonic at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public void setDisplayedMnemonicIndex(int index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                             throws IllegalArgumentException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int oldValue = mnemonicIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            mnemonicIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            String text = getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            int textLength = (text == null) ? 0 : text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (index < -1 || index >= textLength) {  // index out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                throw new IllegalArgumentException("index == " + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        mnemonicIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        firePropertyChange("displayedMnemonicIndex", oldValue, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (index != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns the character, as an index, that the look and feel should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * provide decoration for as representing the mnemonic character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return index representing mnemonic character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @see #setDisplayedMnemonicIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public int getDisplayedMnemonicIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return mnemonicIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Verify that key is a legal value for the horizontalAlignment properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param key the property value to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param message the IllegalArgumentException detail message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception IllegalArgumentException if key isn't LEFT, CENTER, RIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * LEADING or TRAILING.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @see #setHorizontalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @see #setHorizontalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    protected int checkHorizontalKey(int key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if ((key == LEFT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            (key == CENTER) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            (key == RIGHT) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            (key == LEADING) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            (key == TRAILING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw new IllegalArgumentException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Verify that key is a legal value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * verticalAlignment or verticalTextPosition properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @param key the property value to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param message the IllegalArgumentException detail message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @exception IllegalArgumentException if key isn't TOP, CENTER, or BOTTOM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @see #setVerticalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @see #setVerticalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    protected int checkVerticalKey(int key, String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            throw new IllegalArgumentException(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Returns the amount of space between the text and the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * displayed in this label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return an int equal to the number of pixels between the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *         and the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @see #setIconTextGap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    public int getIconTextGap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return iconTextGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * If both the icon and text properties are set, this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * defines the space between them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * The default value of this property is 4 pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @see #getIconTextGap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *  description: If both the icon and text properties are set, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *               property defines the space between them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    public void setIconTextGap(int iconTextGap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        int oldValue = this.iconTextGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        this.iconTextGap = iconTextGap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        firePropertyChange("iconTextGap", oldValue, iconTextGap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (iconTextGap != oldValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Returns the alignment of the label's contents along the Y axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @return   The value of the verticalAlignment property, one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *           following constants defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *           <code>TOP</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *           <code>CENTER</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *           <code>BOTTOM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @see #setVerticalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public int getVerticalAlignment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        return verticalAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Sets the alignment of the label's contents along the Y axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * The default value of this property is CENTER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param alignment One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *           <code>TOP</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *           <code>CENTER</code> (the default), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *           <code>BOTTOM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @see #getVerticalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *         enum: TOP    SwingConstants.TOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *               CENTER SwingConstants.CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *               BOTTOM SwingConstants.BOTTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *  description: The alignment of the label's contents along the Y axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    public void setVerticalAlignment(int alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if (alignment == verticalAlignment) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        int oldValue = verticalAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        firePropertyChange("verticalAlignment", oldValue, verticalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Returns the alignment of the label's contents along the X axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @return   The value of the horizontalAlignment property, one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *           following constants defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *           <code>LEADING</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @see #setHorizontalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    public int getHorizontalAlignment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        return horizontalAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Sets the alignment of the label's contents along the X axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @param alignment  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *           <code>CENTER</code> (the default for image-only labels),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     *           <code>LEADING</code> (the default for text-only labels) or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @see #getHorizontalAlignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *         enum: LEFT     SwingConstants.LEFT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *               CENTER   SwingConstants.CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *               RIGHT    SwingConstants.RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *               LEADING  SwingConstants.LEADING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *               TRAILING SwingConstants.TRAILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *  description: The alignment of the label's content along the X axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public void setHorizontalAlignment(int alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (alignment == horizontalAlignment) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        int oldValue = horizontalAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        horizontalAlignment = checkHorizontalKey(alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                                 "horizontalAlignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        firePropertyChange("horizontalAlignment",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                           oldValue, horizontalAlignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Returns the vertical position of the label's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * relative to its image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @return   One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *           <code>TOP</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *           <code>CENTER</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *           <code>BOTTOM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @see #setVerticalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public int getVerticalTextPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return verticalTextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Sets the vertical position of the label's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * relative to its image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * The default value of this property is CENTER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * @param textPosition  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *           <code>TOP</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *           <code>CENTER</code> (the default), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *           <code>BOTTOM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @see #getVerticalTextPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *         enum: TOP    SwingConstants.TOP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *               CENTER SwingConstants.CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *               BOTTOM SwingConstants.BOTTOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *  description: The vertical position of the text relative to it's image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    public void setVerticalTextPosition(int textPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        if (textPosition == verticalTextPosition) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        int old = verticalTextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        verticalTextPosition = checkVerticalKey(textPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                                                "verticalTextPosition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        firePropertyChange("verticalTextPosition", old, verticalTextPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Returns the horizontal position of the label's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * relative to its image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return   One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *           <code>LEADING</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *           <code>TRAILING</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    public int getHorizontalTextPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        return horizontalTextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * Sets the horizontal position of the label's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * relative to its image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @param textPosition  One of the following constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *           defined in <code>SwingConstants</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *           <code>LEFT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *           <code>CENTER</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *           <code>RIGHT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *           <code>LEADING</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *           <code>TRAILING</code> (the default).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @exception IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @see SwingConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *         enum: LEFT     SwingConstants.LEFT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *               CENTER   SwingConstants.CENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *               RIGHT    SwingConstants.RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *               LEADING  SwingConstants.LEADING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *               TRAILING SwingConstants.TRAILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *    attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *  description: The horizontal position of the label's text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *               relative to its image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public void setHorizontalTextPosition(int textPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int old = horizontalTextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        this.horizontalTextPosition = checkHorizontalKey(textPosition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                                "horizontalTextPosition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        firePropertyChange("horizontalTextPosition",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                           old, horizontalTextPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * This is overridden to return false if the current Icon's Image is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * not equal to the passed in Image <code>img</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @see     java.awt.image.ImageObserver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @see     java.awt.Component#imageUpdate(java.awt.Image, int, int, int, int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    public boolean imageUpdate(Image img, int infoflags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                               int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        // Don't use getDisabledIcon, will trigger creation of icon if icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        if (!isShowing() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            !SwingUtilities.doesIconReferenceImage(getIcon(), img) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            !SwingUtilities.doesIconReferenceImage(disabledIcon, img)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return super.imageUpdate(img, infoflags, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Returns a string representation of this JLabel. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @return  a string representation of this JLabel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        String textString = (text != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                             text : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        String defaultIconString = ((defaultIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                                    && (defaultIcon != this)  ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                                    defaultIcon.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        String disabledIconString = ((disabledIcon != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                     && (disabledIcon != this) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                     disabledIcon.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        String labelForString = (labelFor  != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                                 labelFor.toString() : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        String verticalAlignmentString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        if (verticalAlignment == TOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            verticalAlignmentString = "TOP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        } else if (verticalAlignment == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            verticalAlignmentString = "CENTER";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        } else if (verticalAlignment == BOTTOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            verticalAlignmentString = "BOTTOM";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        } else verticalAlignmentString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        String horizontalAlignmentString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        if (horizontalAlignment == LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            horizontalAlignmentString = "LEFT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        } else if (horizontalAlignment == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            horizontalAlignmentString = "CENTER";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        } else if (horizontalAlignment == RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            horizontalAlignmentString = "RIGHT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        } else if (horizontalAlignment == LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            horizontalAlignmentString = "LEADING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        } else if (horizontalAlignment == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            horizontalAlignmentString = "TRAILING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        } else horizontalAlignmentString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        String verticalTextPositionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        if (verticalTextPosition == TOP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            verticalTextPositionString = "TOP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        } else if (verticalTextPosition == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            verticalTextPositionString = "CENTER";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        } else if (verticalTextPosition == BOTTOM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            verticalTextPositionString = "BOTTOM";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        } else verticalTextPositionString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        String horizontalTextPositionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        if (horizontalTextPosition == LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            horizontalTextPositionString = "LEFT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        } else if (horizontalTextPosition == CENTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            horizontalTextPositionString = "CENTER";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        } else if (horizontalTextPosition == RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            horizontalTextPositionString = "RIGHT";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        } else if (horizontalTextPosition == LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            horizontalTextPositionString = "LEADING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        } else if (horizontalTextPosition == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            horizontalTextPositionString = "TRAILING";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        } else horizontalTextPositionString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        ",defaultIcon=" + defaultIconString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        ",disabledIcon=" + disabledIconString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        ",horizontalAlignment=" + horizontalAlignmentString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        ",horizontalTextPosition=" + horizontalTextPositionString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        ",iconTextGap=" + iconTextGap +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        ",labelFor=" + labelForString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        ",text=" + textString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        ",verticalAlignment=" + verticalAlignmentString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        ",verticalTextPosition=" + verticalTextPositionString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Get the component this is labelling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @return the Component this is labelling.  Can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * does not label a Component.  If the displayedMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * property is set and the labelFor property is also set, the label
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * will call the requestFocus method of the component specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * labelFor property when the mnemonic is activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @see #getDisplayedMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @see #setDisplayedMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    public Component getLabelFor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        return labelFor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * Set the component this is labelling.  Can be null if this does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * label a Component.  If the displayedMnemonic property is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * and the labelFor property is also set, the label will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * call the requestFocus method of the component specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * labelFor property when the mnemonic is activated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @param c  the Component this label is for, or null if the label is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *           not the label for a component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @see #getDisplayedMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @see #setDisplayedMnemonic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *  description: The component this is labelling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    public void setLabelFor(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        Component oldC = labelFor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        labelFor = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        firePropertyChange("labelFor", oldC, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (oldC instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            ((JComponent)oldC).putClientProperty(LABELED_BY_PROPERTY, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        if (c instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            ((JComponent)c).putClientProperty(LABELED_BY_PROPERTY, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * Get the AccessibleContext of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @return the AccessibleContext of this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     *  description: The AccessibleContext associated with this Label.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            accessibleContext = new AccessibleJLabel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * The class used to obtain the accessible role for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  1067
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
  1071
    @SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    protected class AccessibleJLabel extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        implements AccessibleText, AccessibleExtendedComponent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * Get the accessible name of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * @return the localized name of the object -- can be null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         * object does not have a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
         * @see AccessibleContext#setAccessibleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            String name = accessibleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                name = (String)getClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                name = JLabel.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                name = super.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return AccessibleRole.LABEL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
         * Get the AccessibleIcons associated with this object if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
         * or more exist.  Otherwise return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        public AccessibleIcon [] getAccessibleIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            Icon icon = getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            if (icon instanceof Accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                AccessibleContext ac =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                ((Accessible)icon).getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                if (ac != null && ac instanceof AccessibleIcon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    return new AccessibleIcon[] { (AccessibleIcon)ac };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         * Get the AccessibleRelationSet associated with this object if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
         * exists.  Otherwise return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
         * @see AccessibleRelation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        public AccessibleRelationSet getAccessibleRelationSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            // Check where the AccessibleContext's relation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            // set already contains a LABEL_FOR relation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            AccessibleRelationSet relationSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                = super.getAccessibleRelationSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            if (!relationSet.contains(AccessibleRelation.LABEL_FOR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                Component c = JLabel.this.getLabelFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                    AccessibleRelation relation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                        = new AccessibleRelation(AccessibleRelation.LABEL_FOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    relation.setTarget(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    relationSet.add(relation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return relationSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        /* AccessibleText ---------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            View view = (View)JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
         * Given a point in local coordinates, return the zero-based index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         * of the character under that Point.  If the point is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         * this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
         * @param p the Point in local coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         * @return the zero-based index of the character under Point p; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * Point is invalid returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        public int getIndexAtPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            View view = (View) JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                Rectangle r = getTextRectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                Rectangle2D.Float shape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                    new Rectangle2D.Float(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                Position.Bias bias[] = new Position.Bias[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                return view.viewToModel(p.x, p.y, shape, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        /**
19360
0fd3028166c5 7121409: Two conformance tests for AccessibleText.getCharacterBounds(int i) fail
alexsch
parents: 11268
diff changeset
  1188
         * Returns the bounding box of the character at the given
0fd3028166c5 7121409: Two conformance tests for AccessibleText.getCharacterBounds(int i) fail
alexsch
parents: 11268
diff changeset
  1189
         * index in the string.  The bounds are returned in local
0fd3028166c5 7121409: Two conformance tests for AccessibleText.getCharacterBounds(int i) fail
alexsch
parents: 11268
diff changeset
  1190
         * coordinates. If the index is invalid, <code>null</code> is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         * @param i the index into the String
19360
0fd3028166c5 7121409: Two conformance tests for AccessibleText.getCharacterBounds(int i) fail
alexsch
parents: 11268
diff changeset
  1193
         * @return the screen coordinates of the character's bounding box.
0fd3028166c5 7121409: Two conformance tests for AccessibleText.getCharacterBounds(int i) fail
alexsch
parents: 11268
diff changeset
  1194
         * If the index is invalid, <code>null</code> is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        public Rectangle getCharacterBounds(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            View view = (View) JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                Rectangle r = getTextRectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                Rectangle2D.Float shape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    new Rectangle2D.Float(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    Shape charShape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                        view.modelToView(i, shape, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    return charShape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
         * Return the number of characters (valid indicies)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
         * @return the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        public int getCharCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            View view = (View) JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                Document d = view.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                if (d instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    StyledDocument doc = (StyledDocument)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    return doc.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            return accessibleContext.getAccessibleName().length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         * Return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
         * Note: That to the right of the caret will have the same index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
         * value as the offset (the caret is between two characters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
         * @return the zero-based offset of the caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        public int getCaretPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            // There is no caret.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
         * Returns the String at a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
         * @return the letter, word, or sentence,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         *   null for an invalid index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            if (index < 0 || index >= getCharCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    return getText(index, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                    BreakIterator words = BreakIterator.getWordInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    int end = words.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    return s.substring(words.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    BreakIterator sentence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                        BreakIterator.getSentenceInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    return s.substring(sentence.previous(), end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * Returns the String after a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         * or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         * @return the letter, word, or sentence, null for an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
         *  index or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            if (index < 0 || index >= getCharCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                if (index+1 >= getCharCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                   return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    return getText(index+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    BreakIterator words = BreakIterator.getWordInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    int start = words.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    if (start == BreakIterator.DONE || start >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    int end = words.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    if (end == BreakIterator.DONE || end >= s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                    BreakIterator sentence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                        BreakIterator.getSentenceInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                    int start = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                    if (start == BreakIterator.DONE || start > s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                    int end = sentence.following(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    if (end == BreakIterator.DONE || end > s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
         * Returns the String before a given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         * @param part the AccessibleText.CHARACTER, AccessibleText.WORD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         *   or AccessibleText.SENTENCE to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         * @param index an index within the text >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * @return the letter, word, or sentence, null for an invalid index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         *  or part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            if (index < 0 || index > getCharCount()-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
            switch (part) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            case AccessibleText.CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    return getText(index-1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            case AccessibleText.WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    BreakIterator words = BreakIterator.getWordInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                    words.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    int end = words.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    end = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                    int start = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            case AccessibleText.SENTENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    String s = getText(0, getCharCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    BreakIterator sentence =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                        BreakIterator.getSentenceInstance(getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                    sentence.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    int end = sentence.following(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                    end = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
                    int start = sentence.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
                    if (start == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                    return s.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
         * Return the AttributeSet for a given character at a given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * @param i the zero-based index into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         * @return the AttributeSet of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        public AttributeSet getCharacterAttribute(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            View view = (View) JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                Document d = view.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                if (d instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
                    StyledDocument doc = (StyledDocument)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
                    Element elem = doc.getCharacterElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
                    if (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
                        return elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
         * Returns the start offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
         * @return the index into the text of the start of the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        public int getSelectionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            // Text cannot be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
         * Returns the end offset within the selected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
         * If there is no selection, but there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
         * a caret, the start and end offsets will be the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
         *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1460
         * @return the index into the text of the end of the selection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        public int getSelectionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            // Text cannot be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
         * Returns the portion of the text that is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
         * @return the String portion of the text that is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
         * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        public String getSelectedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            // Text cannot be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
         * Returns the text substring starting at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
         * offset with the specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        private String getText(int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
            View view = (View) JLabel.this.getClientProperty("html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                Document d = view.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                if (d instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                    StyledDocument doc = (StyledDocument)d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                    return doc.getText(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
         * Returns the bounding rectangle for the component text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        private Rectangle getTextRectangle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            String text = JLabel.this.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            Icon icon = (JLabel.this.isEnabled()) ? JLabel.this.getIcon() : JLabel.this.getDisabledIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            if ((icon == null) && (text == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            Rectangle paintIconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            Rectangle paintTextR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            Rectangle paintViewR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            Insets paintViewInsets = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
            paintViewInsets = JLabel.this.getInsets(paintViewInsets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            paintViewR.x = paintViewInsets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            paintViewR.y = paintViewInsets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            paintViewR.width = JLabel.this.getWidth() - (paintViewInsets.left + paintViewInsets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            paintViewR.height = JLabel.this.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            String clippedText = SwingUtilities.layoutCompoundLabel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                (JComponent)JLabel.this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                getFontMetrics(getFont()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                JLabel.this.getVerticalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                JLabel.this.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                JLabel.this.getVerticalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                JLabel.this.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                paintViewR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                paintIconR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                paintTextR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                JLabel.this.getIconTextGap());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
            return paintTextR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        // ----- AccessibleExtendedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
         * Returns the AccessibleExtendedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
         * @return the AccessibleExtendedComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        AccessibleExtendedComponent getAccessibleExtendedComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
         * Returns the tool tip text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
         * @return the tool tip text, if supported, of the object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
         * otherwise, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        public String getToolTipText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            return JLabel.this.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
         * Returns the titled border text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
         * @return the titled border text, if supported, of the object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
         * otherwise, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        public String getTitledBorderText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
            return super.getTitledBorderText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         * Returns key bindings associated with this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
         * @return the key bindings, if supported, of the object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
         * otherwise, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
         * @see AccessibleKeyBinding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        public AccessibleKeyBinding getAccessibleKeyBinding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
            int mnemonic = JLabel.this.getDisplayedMnemonic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            if (mnemonic == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
            return new LabelKeyBinding(mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        class LabelKeyBinding implements AccessibleKeyBinding {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            int mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            LabelKeyBinding(int mnemonic) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                this.mnemonic = mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
             * Returns the number of key bindings for this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
             * @return the zero-based number of key bindings for this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            public int getAccessibleKeyBindingCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
             * Returns a key binding for this object.  The value returned is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
             * java.lang.Object which must be cast to appropriate type depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
             * on the underlying implementation of the key.  For example, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
             * Object returned is a javax.swing.KeyStroke, the user of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
             * method should do the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
             * <nf><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
             * Component c = <get the component that has the key bindings>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
             * AccessibleContext ac = c.getAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
             * AccessibleKeyBinding akb = ac.getAccessibleKeyBinding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
             * for (int i = 0; i < akb.getAccessibleKeyBindingCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
             *     Object o = akb.getAccessibleKeyBinding(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
             *     if (o instanceof javax.swing.KeyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
             *         javax.swing.KeyStroke keyStroke = (javax.swing.KeyStroke)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
             *         <do something with the key binding>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
             *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
             * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
             * </code></nf>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
             * @param i zero-based index of the key bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
             * @return a javax.lang.Object which specifies the key binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
             * @exception IllegalArgumentException if the index is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
             * out of bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
             * @see #getAccessibleKeyBindingCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            public java.lang.Object getAccessibleKeyBinding(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
                if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
                    throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                return KeyStroke.getKeyStroke(mnemonic, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    }  // AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
}