jdk/src/java.desktop/share/classes/javax/swing/text/PasswordView.java
author alexsch
Thu, 22 Dec 2016 12:09:34 +0300
changeset 43075 668b6d00cc79
parent 42216 621af0ebf6c4
permissions -rw-r--r--
8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143 Reviewed-by: flar, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20158
diff changeset
     2
 * Copyright (c) 1997, 2013, 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.*;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    29
import java.awt.font.FontRenderContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.JPasswordField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Implements a View suitable for use in JPasswordField
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * UI implementations.  This is basically a field ui that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * renders its contents as the echo character specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * in the associated component (if it can narrow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * component to a JPasswordField).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class PasswordView extends FieldView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Constructs a new view wrapped on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public PasswordView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Renders the given range in the model as normal unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * text.  This sets the foreground color and echos the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * using the value returned by getEchoChar().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    59
     * @param x the starting X coordinate >= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    60
     * @param y the starting Y coordinate >= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    61
     * @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
    62
     * @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
    63
     * @return the X location of the end of the range >= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception BadLocationException if p0 or p1 are out of range
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    65
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    66
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    67
     *     {@link #drawUnselectedText(Graphics2D, float, float, int, int)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    69
    @Deprecated(since = "9")
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    70
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected int drawUnselectedText(Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                     int p0, int p1) throws BadLocationException {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    73
        return (int) drawUnselectedTextImpl(g, x, y, p0, p1, false);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    74
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    76
    @Override
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    77
    protected float drawUnselectedText(Graphics2D g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    78
                                       int p0, int p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    79
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    80
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    81
        return drawUnselectedTextImpl(g, x, y, p0, p1, true);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    82
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    83
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 41807
diff changeset
    84
    @SuppressWarnings("deprecation")
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    85
    private float drawUnselectedTextImpl(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    86
                                         int p0, int p1,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    87
                                         boolean useFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    88
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    89
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            JPasswordField f = (JPasswordField) c;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    93
            if (!f.echoCharIsSet()) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    94
                boolean useDrawUnselectedFPAPI = useFPAPI
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    95
                        && drawUnselectedTextOverridden
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    96
                        && g instanceof Graphics2D;
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    97
                return (useDrawUnselectedFPAPI )
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    98
                        ? super.drawUnselectedText((Graphics2D) g, x, y, p0, p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    99
                        : super.drawUnselectedText(g, (int) x, (int) y, p0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (f.isEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                g.setColor(f.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                g.setColor(f.getDisabledTextColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            int n = p1 - p0;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   109
            boolean useEchoCharFPAPI = useFPAPI
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   110
                    && drawEchoCharacterOverridden
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   111
                    && g instanceof Graphics2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            for (int i = 0; i < n; i++) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   113
                x = (useEchoCharFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   114
                        ? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   115
                        : drawEchoCharacter(g, (int) x, (int) y, echoChar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Renders the given range in the model as selected text.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * is implemented to render the text in the color specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * the hosting component.  It assumes the highlighter will render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * the selected background.  Uses the result of getEchoChar() to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * display the characters.
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
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   131
     * @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
   132
     * @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
   133
     * @return the X location of the end of the range &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @exception BadLocationException if p0 or p1 are out of range
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   135
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   136
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   137
     *     {@link #drawSelectedText(Graphics2D, float, float, int, int)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   139
    @Deprecated(since = "9")
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   140
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    protected int drawSelectedText(Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                   int y, int p0, int p1) throws BadLocationException {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   143
        return (int) drawSelectedTextImpl(g, x, y, p0, p1, false);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   144
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   145
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   146
    @Override
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   147
    protected float drawSelectedText(Graphics2D g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   148
                                     int p0, int p1) throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   149
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   150
        return drawSelectedTextImpl(g, x, y, p0, p1, true);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   151
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   152
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 41807
diff changeset
   153
    @SuppressWarnings("deprecation")
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   154
    private float drawSelectedTextImpl(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   155
                                       int p0, int p1,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   156
                                       boolean useFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   157
            throws BadLocationException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        g.setColor(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            JPasswordField f = (JPasswordField) c;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   162
            if (!f.echoCharIsSet()) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   163
                boolean useDrawUnselectedFPAPI = useFPAPI
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   164
                        && drawSelectedTextOverridden
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   165
                        && g instanceof Graphics2D;
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   166
                return (useFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   167
                        ? super.drawSelectedText((Graphics2D) g, x, y, p0, p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   168
                        : super.drawSelectedText(g, (int) x, (int) y, p0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int n = p1 - p0;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   172
            boolean useEchoCharFPAPI = useFPAPI
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   173
                    && drawEchoCharacterOverridden
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   174
                    && g instanceof Graphics2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            for (int i = 0; i < n; i++) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   176
                x = (useEchoCharFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   177
                        ? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   178
                        : drawEchoCharacter(g, (int) x, (int) y, echoChar);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   179
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Renders the echo character, or whatever graphic should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to display the password characters.  The color in the Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * object is set to the appropriate foreground color for selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * or unselected text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   192
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   193
     * @param y the starting Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param c the echo character
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   195
     * @return the updated X position &gt;= 0
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   196
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   197
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   198
     *     {@link #drawEchoCharacter(Graphics2D, float, float, char)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   200
    @Deprecated(since = "9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   202
        return (int) drawEchoCharacterImpl(g, x, y, c, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   206
     * Renders the echo character, or whatever graphic should be used
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   207
     * to display the password characters.  The color in the Graphics
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   208
     * object is set to the appropriate foreground color for selected
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   209
     * or unselected text.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   210
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   211
     * @param g the graphics context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   212
     * @param x the starting X coordinate {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   213
     * @param y the starting Y coordinate {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   214
     * @param c the echo character
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   215
     * @return the updated X position {@code >= 0}
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   216
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   217
     * @since 9
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   218
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   219
    protected float drawEchoCharacter(Graphics2D g, float x, float y, char c) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   220
        return drawEchoCharacterImpl(g, x, y, c, true);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   221
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   222
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   223
    private float drawEchoCharacterImpl(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   224
                                        char c, boolean useFPAPI) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   225
        ONE[0] = c;
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   226
        SwingUtilities2.drawChars(Utilities.getJComponent(this),
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   227
                                  g, ONE, 0, 1, x, y);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   228
        if (useFPAPI) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   229
            return x + g.getFontMetrics().charWidth(c);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   230
        } else {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   231
            FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   232
            return x + (float) g.getFont().getStringBounds(ONE, 0, 1, frc).getWidth();
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   233
        }
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   234
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   235
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 25859
diff changeset
   236
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   240
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return super.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            FontMetrics m = f.getFontMetrics(f.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Rectangle alloc = adjustAllocation(a).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            int dx = (pos - getStartOffset()) * m.charWidth(echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            alloc.x += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            alloc.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return null;
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
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   270
     * @param fx the X coordinate &gt;= 0.0f
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   271
     * @param fy the Y coordinate &gt;= 0.0f
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *  given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (! f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                return super.viewToModel(fx, fy, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            a = adjustAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                              a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            n = (charWidth > 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                 ((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            else if (n > (getStartOffset() + getDocument().getLength())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                n = getDocument().getLength() - getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return getStartOffset() + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @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
   308
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (c instanceof JPasswordField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                JPasswordField f = (JPasswordField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                if (f.echoCharIsSet()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    char echoChar = f.getEchoChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    FontMetrics m = f.getFontMetrics(f.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    return m.charWidth(echoChar) * getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        return super.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    static char[] ONE = new char[1];
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   331
43075
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42216
diff changeset
   332
    private final boolean drawEchoCharacterOverridden =
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42216
diff changeset
   333
            getFPMethodOverridden(getClass(), "drawEchoCharacter", FPMethodArgs.GNNC);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
}