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