jdk/src/share/classes/javax/swing/JPasswordField.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html">How to Use Text Fields</a>,
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public class JPasswordField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Constructs a new <code>JPasswordField</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * with a default document, <code>null</code> starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * text string, and 0 column width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public JPasswordField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this(null,null,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Constructs a new <code>JPasswordField</code> initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * with the specified text.  The document model is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * default, and the number of columns to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param text the text to be displayed, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public JPasswordField(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this(null, text, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Constructs a new empty <code>JPasswordField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * number of columns.  A default model is created, and the initial string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * is set to <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public JPasswordField(int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this(null, null, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a new <code>JPasswordField</code> initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the specified text and columns.  The document model is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param text the text to be displayed, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param columns the number of columns >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public JPasswordField(String text, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this(null, text, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Constructs a new <code>JPasswordField</code> that uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * given text storage model and the given number of columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * This is the constructor through which the other constructors feed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The echo character is set to '*', but may be changed by the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Look and Feel.  If the document model is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>null</code>, a default one will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param doc  the text storage to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param txt the text to be displayed, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param columns  the number of columns to use to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *   the preferred width >= 0; if columns is set to zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   preferred width will be whatever naturally results from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *   the component implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public JPasswordField(Document doc, String txt, int columns) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        super(doc, txt, columns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // We could either leave this on, which wouldn't be secure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // or obscure the composted text, which essentially makes displaying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // it useless. Therefore, we turn off input methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        enableInputMethods(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Returns the name of the L&F class that renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return the string "PasswordFieldUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
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
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void updateUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if(!echoCharSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            echoChar = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        super.updateUI();
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
     * Returns the character to be used for echoing.  The default is '*'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The default may be different depending on the currently running Look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * and Feel. For example, Metal/Ocean's default is a bullet character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @return the echo character, 0 if unset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @see #setEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see #echoCharIsSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public char getEchoChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return echoChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Sets the echo character for this <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Note that this is largely a suggestion, since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * view that gets installed can use whatever graphic techniques
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * it desires to represent the field.  Setting a value of 0 indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * that you wish to see the text as it is typed, similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the behavior of a standard <code>JTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param c the echo character to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @see #echoCharIsSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #getEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * description: character to display in place of the real characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void setEchoChar(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        echoChar = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        echoCharSet = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Returns true if this <code>JPasswordField</code> has a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * set for echoing.  A character is considered to be set if the echo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * character is not 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return true if a character is set for echoing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see #setEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #getEchoChar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public boolean echoCharIsSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return echoChar != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // --- JTextComponent methods ----------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Invokes <code>provideErrorFeedback</code> on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * look and feel, which typically initiates an error beep.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * The normal behavior of transferring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * currently selected range in the associated text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * to the system clipboard, and removing the contents from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the model, is not acceptable for a password field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public void cut() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            UIManager.getLookAndFeel().provideErrorFeedback(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            super.cut();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
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
     * Invokes <code>provideErrorFeedback</code> on the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * look and feel, which typically initiates an error beep.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * The normal behavior of transferring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * currently selected range in the associated text model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * to the system clipboard, and leaving the contents from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * the model, is not acceptable for a password field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void copy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            UIManager.getLookAndFeel().provideErrorFeedback(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            super.copy();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns the text contained in this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * If the underlying document is <code>null</code>, will give a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <code>NullPointerException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * For security reasons, this method is deprecated.  Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     <code>* getPassword</code> method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @deprecated As of Java 2 platform v1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * replaced by <code>getPassword</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public String getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return super.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Fetches a portion of the text represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * component.  Returns an empty string if length is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * For security reasons, this method is deprecated.  Use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <code>getPassword</code> method instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @deprecated As of Java 2 platform v1.2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * replaced by <code>getPassword</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param offs the offset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param len the length >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception BadLocationException if the offset or length are invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public String getText(int offs, int len) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return super.getText(offs, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Returns the text contained in this <code>TextComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * If the underlying document is <code>null</code>, will give a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>NullPointerException</code>.  For stronger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * security, it is recommended that the returned character array be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * cleared after use by setting each character to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public char[] getPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Segment txt = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            doc.getText(0, doc.getLength(), txt); // use the non-String API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        char[] retValue = new char[txt.count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * See readObject() and writeObject() in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
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
    // --- variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private static final String uiClassID = "PasswordFieldUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private char echoChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private boolean echoCharSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns a string representation of this <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * This method is intended to be used only for debugging purposes, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * content and format of the returned string may vary between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * implementations. The returned string may be empty but may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @return  a string representation of this <code>JPasswordField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        ",echoChar=" + echoChar;
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
     * This method is a hack to get around the fact that we cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * directly override setUIProperty because part of the inheritance heirarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * goes outside of the javax.swing package, and therefore calling a package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * private method isn't allowed. This method should return true if the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * was handled, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    boolean customSetUIProperty(String propertyName, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (propertyName == "echoChar") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (!echoCharSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                setEchoChar((Character)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                echoCharSet = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
/////////////////
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
// Accessibility support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
////////////////
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
     * Returns the <code>AccessibleContext</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>JPasswordField</code>. For password fields, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <code>AccessibleContext</code> takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <code>AccessibleJPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * A new <code>AccessibleJPasswordField</code> instance is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return an <code>AccessibleJPasswordField</code> that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *         <code>AccessibleContext</code> of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         <code>JPasswordField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            accessibleContext = new AccessibleJPasswordField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>JPasswordField</code> class.  It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Java Accessibility API appropriate to password field user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    protected class AccessibleJPasswordField extends AccessibleJTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * Gets the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         *   object (AccessibleRole.PASSWORD_TEXT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return AccessibleRole.PASSWORD_TEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         * Gets the <code>AccessibleText</code> for the <code>JPasswordField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * The returned object also implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         * <code>AccessibleExtendedText</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
         * @return <code>AccessibleText</code> for the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * @see javax.accessibility.AccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * @see javax.accessibility.AccessibleContext#getAccessibleText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         * @see javax.accessibility.AccessibleText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * @see javax.accessibility.AccessibleExtendedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
         * Returns a String filled with password echo characters. The String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
         * contains one echo character for each character (including whitespace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
         * that the user entered in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        private String getEchoString(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            char[] buffer = new char[str.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            Arrays.fill(buffer, getEchoChar());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            return new String(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * Returns the <code>String</code> at a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        public String getAtIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
           String str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                str = super.getAtIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                // Treat the text displayed in the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                // as one word and sentence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                char password[] = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                if (password == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    index < 0 || index >= password.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                str = new String(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         * Returns the <code>String</code> after a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        public String getAfterIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                String str = super.getAfterIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                // There is no word or sentence after the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                // displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
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
         * Returns the <code>String</code> before a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * @param part the <code>CHARACTER</code>, <code>WORD</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * <code>SENTENCE</code> to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * @return a <code>String</code> if <code>part</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * <code>index</code> are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        public String getBeforeIndex(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                String str = super.getBeforeIndex(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                // There is no word or sentence before the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                // displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
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
         * Returns the text between two <code>indices</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * @param startIndex the start index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * @param endIndex the end index in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * @return the text string if the indices are valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * Otherwise, <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        public String getTextRange(int startIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            String str = super.getTextRange(startIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            return getEchoString(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
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
         * Returns the <code>AccessibleTextSequence</code> at a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        public AccessibleTextSequence getTextSequenceAt(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                AccessibleTextSequence seq = super.getTextSequenceAt(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                // Treat the text displayed in the JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // as one word, sentence, line and attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                char password[] = getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if (password == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    index < 0 || index >= password.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                String text = new String(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return new AccessibleTextSequence(0, password.length - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                                                  getEchoString(text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
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
         * Returns the <code>AccessibleTextSequence</code> after a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        public AccessibleTextSequence getTextSequenceAfter(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                AccessibleTextSequence seq = super.getTextSequenceAfter(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                // There is no word, sentence, line or attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                // after the text displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
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
         * Returns the <code>AccessibleTextSequence</code> before a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * @param part the <code>CHARACTER</code>, <code>WORD</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         * <code>SENTENCE</code>, <code>LINE</code> or <code>ATTRIBUTE_RUN</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
         * retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
         * @param index an index within the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
         * @return an <code>AccessibleTextSequence</code> specifying the text if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
         * <code>part</code> and <code>index</code> are valid.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * <code>null</code> is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * @see javax.accessibility.AccessibleText#CHARACTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         * @see javax.accessibility.AccessibleText#WORD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
         * @see javax.accessibility.AccessibleText#SENTENCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
         * @see javax.accessibility.AccessibleExtendedText#LINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
         * @see javax.accessibility.AccessibleExtendedText#ATTRIBUTE_RUN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
         * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        public AccessibleTextSequence getTextSequenceBefore(int part, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            if (part == AccessibleText.CHARACTER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                AccessibleTextSequence seq = super.getTextSequenceBefore(part, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                if (seq == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                return new AccessibleTextSequence(seq.startIndex, seq.endIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                                                  getEchoString(seq.text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                // There is no word, sentence, line or attribute run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                // before the text displayed in the JPasswordField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
}