author | prr |
Mon, 14 Nov 2016 09:59:36 -0800 | |
changeset 42216 | 621af0ebf6c4 |
parent 41807 | f9eb6cb54fed |
child 43075 | 668b6d00cc79 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package javax.swing.text; |
|
26 |
||
27 |
import sun.swing.SwingUtilities2; |
|
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 | 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 | 34 |
|
35 |
/** |
|
36 |
* Implements a View suitable for use in JPasswordField |
|
37 |
* UI implementations. This is basically a field ui that |
|
38 |
* renders its contents as the echo character specified |
|
39 |
* in the associated component (if it can narrow the |
|
40 |
* component to a JPasswordField). |
|
41 |
* |
|
42 |
* @author Timothy Prinzing |
|
43 |
* @see View |
|
44 |
*/ |
|
45 |
public class PasswordView extends FieldView { |
|
46 |
||
47 |
/** |
|
48 |
* Constructs a new view wrapped on an element. |
|
49 |
* |
|
50 |
* @param elem the element |
|
51 |
*/ |
|
52 |
public PasswordView(Element elem) { |
|
53 |
super(elem); |
|
54 |
} |
|
55 |
||
56 |
/** |
|
57 |
* Renders the given range in the model as normal unselected |
|
58 |
* text. This sets the foreground color and echos the characters |
|
59 |
* using the value returned by getEchoChar(). |
|
60 |
* |
|
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 | 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 | 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 | 74 |
protected int drawUnselectedText(Graphics g, int x, int y, |
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 | 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 | 93 |
Container c = getContainer(); |
94 |
if (c instanceof JPasswordField) { |
|
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 | 103 |
} |
104 |
if (f.isEnabled()) { |
|
105 |
g.setColor(f.getForeground()); |
|
106 |
} |
|
107 |
else { |
|
108 |
g.setColor(f.getDisabledTextColor()); |
|
109 |
} |
|
110 |
char echoChar = f.getEchoChar(); |
|
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 | 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 | 119 |
} |
120 |
} |
|
121 |
return x; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Renders the given range in the model as selected text. This |
|
126 |
* is implemented to render the text in the color specified in |
|
127 |
* the hosting component. It assumes the highlighter will render |
|
128 |
* the selected background. Uses the result of getEchoChar() to |
|
129 |
* display the characters. |
|
130 |
* |
|
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 >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
133 |
* @param y the starting Y coordinate >= 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 >= 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 >= 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 >= 0 |
2 | 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 | 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 | 144 |
protected int drawSelectedText(Graphics g, int x, |
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 | 161 |
g.setColor(selected); |
162 |
Container c = getContainer(); |
|
163 |
if (c instanceof JPasswordField) { |
|
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 | 172 |
} |
173 |
char echoChar = f.getEchoChar(); |
|
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 | 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 | 183 |
} |
184 |
} |
|
185 |
return x; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Renders the echo character, or whatever graphic should be used |
|
190 |
* to display the password characters. The color in the Graphics |
|
191 |
* object is set to the appropriate foreground color for selected |
|
192 |
* or unselected text. |
|
193 |
* |
|
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 >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
196 |
* @param y the starting Y coordinate >= 0 |
2 | 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 >= 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 | 202 |
*/ |
41807
f9eb6cb54fed
8156217: Selected text is shifted on HiDPI display
alexsch
parents:
39553
diff
changeset
|
203 |
@Deprecated(since = "9") |
2 | 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 | 206 |
} |
207 |
||
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 | 240 |
* Provides a mapping from the document model coordinate space |
241 |
* to the coordinate space of the view mapped to it. |
|
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 >= 0 |
2 | 244 |
* @param a the allocated region to render into |
245 |
* @return the bounding box of the given position |
|
246 |
* @exception BadLocationException if the given position does not |
|
247 |
* represent a valid location in the associated document |
|
248 |
* @see View#modelToView |
|
249 |
*/ |
|
250 |
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { |
|
251 |
Container c = getContainer(); |
|
252 |
if (c instanceof JPasswordField) { |
|
253 |
JPasswordField f = (JPasswordField) c; |
|
254 |
if (! f.echoCharIsSet()) { |
|
255 |
return super.modelToView(pos, a, b); |
|
256 |
} |
|
257 |
char echoChar = f.getEchoChar(); |
|
258 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
259 |
||
260 |
Rectangle alloc = adjustAllocation(a).getBounds(); |
|
261 |
int dx = (pos - getStartOffset()) * m.charWidth(echoChar); |
|
262 |
alloc.x += dx; |
|
263 |
alloc.width = 1; |
|
264 |
return alloc; |
|
265 |
} |
|
266 |
return null; |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Provides a mapping from the view coordinate space to the logical |
|
271 |
* coordinate space of the model. |
|
272 |
* |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
273 |
* @param fx the X coordinate >= 0.0f |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
274 |
* @param fy the Y coordinate >= 0.0f |
2 | 275 |
* @param a the allocated region to render into |
276 |
* @return the location within the model that best represents the |
|
277 |
* given point in the view |
|
278 |
* @see View#viewToModel |
|
279 |
*/ |
|
280 |
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) { |
|
281 |
bias[0] = Position.Bias.Forward; |
|
282 |
int n = 0; |
|
283 |
Container c = getContainer(); |
|
284 |
if (c instanceof JPasswordField) { |
|
285 |
JPasswordField f = (JPasswordField) c; |
|
286 |
if (! f.echoCharIsSet()) { |
|
287 |
return super.viewToModel(fx, fy, a, bias); |
|
288 |
} |
|
289 |
char echoChar = f.getEchoChar(); |
|
290 |
int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar); |
|
291 |
a = adjustAllocation(a); |
|
292 |
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : |
|
293 |
a.getBounds(); |
|
294 |
n = (charWidth > 0 ? |
|
295 |
((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE); |
|
296 |
if (n < 0) { |
|
297 |
n = 0; |
|
298 |
} |
|
299 |
else if (n > (getStartOffset() + getDocument().getLength())) { |
|
300 |
n = getDocument().getLength() - getStartOffset(); |
|
301 |
} |
|
302 |
} |
|
303 |
return getStartOffset() + n; |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Determines the preferred span for this view along an |
|
308 |
* axis. |
|
309 |
* |
|
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 >= 0. |
2 | 312 |
* Typically the view is told to render into the span |
313 |
* that is returned, although there is no guarantee. |
|
314 |
* The parent may choose to resize or break the view. |
|
315 |
*/ |
|
316 |
public float getPreferredSpan(int axis) { |
|
317 |
switch (axis) { |
|
318 |
case View.X_AXIS: |
|
319 |
Container c = getContainer(); |
|
320 |
if (c instanceof JPasswordField) { |
|
321 |
JPasswordField f = (JPasswordField) c; |
|
322 |
if (f.echoCharIsSet()) { |
|
323 |
char echoChar = f.getEchoChar(); |
|
324 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
325 |
Document doc = getDocument(); |
|
326 |
return m.charWidth(echoChar) * getDocument().getLength(); |
|
327 |
} |
|
328 |
} |
|
329 |
} |
|
330 |
return super.getPreferredSpan(axis); |
|
331 |
} |
|
332 |
||
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 | 353 |
} |