jdk/src/share/classes/javax/swing/JPasswordField.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 22574 7f8ce0c8c20a
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>JPasswordField</code> is a lightweight component that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the editing of a single line of text where the view indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * something was typed, but does not show the original characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * You can find further information and examples in
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 20157
diff changeset
    42
 * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a section in <em>The Java Tutorial.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>JPasswordField</code> is intended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to be source-compatible with <code>java.awt.TextField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * used with <code>echoChar</code> set.  It is provided separately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to make it easier to safely change the UI for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>JTextField</code> without affecting password entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <strong>NOTE:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * By default, JPasswordField disables input methods; otherwise, input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * characters could be visible while they were composed using input methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * If an application needs the input methods support, please use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * inherited method, <code>enableInputMethods(true)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * 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
    67
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *  attribute: isContainer false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * description: Allows the editing of a line of text but doesn't show the characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
    77
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
public class JPasswordField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Constructs a new <code>JPasswordField</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * with a default document, <code>null</code> starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * text string, and 0 column width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public JPasswordField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this(null,null,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Constructs a new <code>JPasswordField</code> initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * with the specified text.  The document model is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * default, and the number of columns to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param text the text to be displayed, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public JPasswordField(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this(null, text, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Constructs a new empty <code>JPasswordField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * number of columns.  A default model is created, and the initial string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * is set to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   105
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public JPasswordField(int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this(null, null, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Constructs a new <code>JPasswordField</code> initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the specified text and columns.  The document model is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param text the text to be displayed, <code>null</code> if none
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   117
     * @param columns the number of columns &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public JPasswordField(String text, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this(null, text, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Constructs a new <code>JPasswordField</code> that uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * given text storage model and the given number of columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * This is the constructor through which the other constructors feed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * The echo character is set to '*', but may be changed by the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Look and Feel.  If the document model is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>null</code>, a default one will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param doc  the text storage to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param txt the text to be displayed, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param columns  the number of columns to use to calculate
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   134
     *   the preferred width &gt;= 0; if columns is set to zero, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *   preferred width will be whatever naturally results from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *   the component implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public JPasswordField(Document doc, String txt, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        super(doc, txt, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // We could either leave this on, which wouldn't be secure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // or obscure the composted text, which essentially makes displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        // it useless. Therefore, we turn off input methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        enableInputMethods(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   147
     * Returns the name of the L&amp;F class that renders this component.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return the string "PasswordFieldUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if(!echoCharSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            echoChar = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        super.updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns the character to be used for echoing.  The default is '*'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The default may be different depending on the currently running Look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * and Feel. For example, Metal/Ocean's default is a bullet character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the echo character, 0 if unset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #setEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see #echoCharIsSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public char getEchoChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return echoChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Sets the echo character for this <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Note that this is largely a suggestion, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * view that gets installed can use whatever graphic techniques
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * it desires to represent the field.  Setting a value of 0 indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * that you wish to see the text as it is typed, similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * the behavior of a standard <code>JTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param c the echo character to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #echoCharIsSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see #getEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * description: character to display in place of the real characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public void setEchoChar(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        echoChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        echoCharSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Returns true if this <code>JPasswordField</code> has a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * set for echoing.  A character is considered to be set if the echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * character is not 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @return true if a character is set for echoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #setEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see #getEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public boolean echoCharIsSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return echoChar != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // --- JTextComponent methods ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Invokes <code>provideErrorFeedback</code> on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * look and feel, which typically initiates an error beep.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * The normal behavior of transferring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * currently selected range in the associated text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * to the system clipboard, and removing the contents from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the model, is not acceptable for a password field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void cut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            UIManager.getLookAndFeel().provideErrorFeedback(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            super.cut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Invokes <code>provideErrorFeedback</code> on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * look and feel, which typically initiates an error beep.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The normal behavior of transferring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * currently selected range in the associated text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * to the system clipboard, and leaving the contents from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * the model, is not acceptable for a password field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            UIManager.getLookAndFeel().provideErrorFeedback(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            super.copy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Returns the text contained in this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If the underlying document is <code>null</code>, will give a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <code>NullPointerException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * For security reasons, this method is deprecated.  Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     <code>* getPassword</code> method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @deprecated As of Java 2 platform v1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * replaced by <code>getPassword</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return super.getText();
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
     * Fetches a portion of the text represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * component.  Returns an empty string if length is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * For security reasons, this method is deprecated.  Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <code>getPassword</code> method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @deprecated As of Java 2 platform v1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * replaced by <code>getPassword</code>.
20157
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   275
     * @param offs the offset &gt;= 0
cafca01a8e28 8025230: [cleanup] some more javadoc formatting fixes for swing
yan
parents: 5506
diff changeset
   276
     * @param len the length &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception BadLocationException if the offset or length are invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public String getText(int offs, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return super.getText(offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Returns the text contained in this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * If the underlying document is <code>null</code>, will give a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <code>NullPointerException</code>.  For stronger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * security, it is recommended that the returned character array be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * cleared after use by setting each character to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public char[] getPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Segment txt = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            doc.getText(0, doc.getLength(), txt); // use the non-String API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        char[] retValue = new char[txt.count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // --- variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private static final String uiClassID = "PasswordFieldUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private char echoChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private boolean echoCharSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns a string representation of this <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return  a string representation of this <code>JPasswordField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        ",echoChar=" + echoChar;
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
     * This method is a hack to get around the fact that we cannot
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   352
     * directly override setUIProperty because part of the inheritance hierarchy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * goes outside of the javax.swing package, and therefore calling a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * private method isn't allowed. This method should return true if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * was handled, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    boolean customSetUIProperty(String propertyName, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (propertyName == "echoChar") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if (!echoCharSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                setEchoChar((Character)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                echoCharSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns the <code>AccessibleContext</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <code>JPasswordField</code>. For password fields, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>AccessibleContext</code> takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <code>AccessibleJPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * A new <code>AccessibleJPasswordField</code> instance is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return an <code>AccessibleJPasswordField</code> that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         <code>AccessibleContext</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *         <code>JPasswordField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            accessibleContext = new AccessibleJPasswordField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <code>JPasswordField</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Java Accessibility API appropriate to password field user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * 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
   403
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected class AccessibleJPasswordField extends AccessibleJTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         *   object (AccessibleRole.PASSWORD_TEXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return AccessibleRole.PASSWORD_TEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * Gets the <code>AccessibleText</code> for the <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * The returned object also implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         * <code>AccessibleExtendedText</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * @return <code>AccessibleText</code> for the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * @see javax.accessibility.AccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * @see javax.accessibility.AccessibleContext#getAccessibleText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * @see javax.accessibility.AccessibleText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         * @see javax.accessibility.AccessibleExtendedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * Returns a String filled with password echo characters. The String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * contains one echo character for each character (including whitespace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         * that the user entered in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        private String getEchoString(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            char[] buffer = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            Arrays.fill(buffer, getEchoChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            return new String(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * Returns the <code>String</code> at a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
           String str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                str = super.getAtIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // Treat the text displayed in the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                // as one word and sentence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                char password[] = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (password == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    index < 0 || index >= password.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                str = new String(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         * Returns the <code>String</code> after a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                String str = super.getAfterIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                // There is no word or sentence after the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                // displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * Returns the <code>String</code> before a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                String str = super.getBeforeIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                // There is no word or sentence before the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                // displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         * Returns the text between two <code>indices</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * @param startIndex the start index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * @param endIndex the end index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * @return the text string if the indices are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        public String getTextRange(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            String str = super.getTextRange(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * Returns the <code>AccessibleTextSequence</code> at a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        public AccessibleTextSequence getTextSequenceAt(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                AccessibleTextSequence seq = super.getTextSequenceAt(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // Treat the text displayed in the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // as one word, sentence, line and attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                char password[] = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                if (password == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    index < 0 || index >= password.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                String text = new String(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                return new AccessibleTextSequence(0, password.length - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                                                  getEchoString(text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * Returns the <code>AccessibleTextSequence</code> after a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        public AccessibleTextSequence getTextSequenceAfter(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                AccessibleTextSequence seq = super.getTextSequenceAfter(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // There is no word, sentence, line or attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                // after the text displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
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
         * Returns the <code>AccessibleTextSequence</code> before a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        public AccessibleTextSequence getTextSequenceBefore(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                AccessibleTextSequence seq = super.getTextSequenceBefore(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                // There is no word, sentence, line or attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                // before the text displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
}