jdk/src/share/classes/javax/swing/text/PasswordView.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 20158 1c5d22e5b898
child 23010 6dadb192ad81
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, 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.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.JPasswordField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Implements a View suitable for use in JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * UI implementations.  This is basically a field ui that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * renders its contents as the echo character specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * in the associated component (if it can narrow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * component to a JPasswordField).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class PasswordView extends FieldView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Constructs a new view wrapped on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public PasswordView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Renders the given range in the model as normal unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * text.  This sets the foreground color and echos the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * using the value returned by getEchoChar().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    58
     * @param x the starting X coordinate >= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    59
     * @param y the starting Y coordinate >= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    60
     * @param p0 the starting offset in the model >= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    61
     * @param p1 the ending offset in the model >= p0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    62
     * @return the X location of the end of the range >= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @exception BadLocationException if p0 or p1 are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected int drawUnselectedText(Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                     int p0, int p1) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                return super.drawUnselectedText(g, x, y, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (f.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                g.setColor(f.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                g.setColor(f.getDisabledTextColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int n = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                x = drawEchoCharacter(g, x, y, echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return x;
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
     * Renders the given range in the model as selected text.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * is implemented to render the text in the color specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * the hosting component.  It assumes the highlighter will render
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the selected background.  Uses the result of getEchoChar() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * display the characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    97
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    98
     * @param y the starting Y coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    99
     * @param p0 the starting offset in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   100
     * @param p1 the ending offset in the model &gt;= p0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   101
     * @return the X location of the end of the range &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception BadLocationException if p0 or p1 are out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected int drawSelectedText(Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                   int y, int p0, int p1) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        g.setColor(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return super.drawSelectedText(g, x, y, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int n = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                x = drawEchoCharacter(g, x, y, echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return x;
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
     * Renders the echo character, or whatever graphic should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * to display the password characters.  The color in the Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * object is set to the appropriate foreground color for selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * or unselected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   129
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   130
     * @param y the starting Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param c the echo character
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   132
     * @return the updated X position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ONE[0] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        SwingUtilities2.drawChars(Utilities.getJComponent(this),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                                  g, ONE, 0, 1, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return x + g.getFontMetrics().charWidth(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   145
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return super.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            FontMetrics m = f.getFontMetrics(f.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Rectangle alloc = adjustAllocation(a).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int dx = (pos - getStartOffset()) * m.charWidth(echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            alloc.x += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            alloc.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   175
     * @param fx the X coordinate &gt;= 0.0f
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   176
     * @param fy the Y coordinate &gt;= 0.0f
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *  given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return super.viewToModel(fx, fy, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            a = adjustAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                              a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            n = (charWidth > 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                 ((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            else if (n > (getStartOffset() + getDocument().getLength())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                n = getDocument().getLength() - getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return getStartOffset() + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   213
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    FontMetrics m = f.getFontMetrics(f.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    return m.charWidth(echoChar) * getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return super.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static char[] ONE = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
}