author | alexsch |
Thu, 22 Dec 2016 12:09:34 +0300 | |
changeset 43075 | 668b6d00cc79 |
parent 42216 | 621af0ebf6c4 |
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; |
2 | 30 |
import javax.swing.JPasswordField; |
31 |
||
32 |
/** |
|
33 |
* Implements a View suitable for use in JPasswordField |
|
34 |
* UI implementations. This is basically a field ui that |
|
35 |
* renders its contents as the echo character specified |
|
36 |
* in the associated component (if it can narrow the |
|
37 |
* component to a JPasswordField). |
|
38 |
* |
|
39 |
* @author Timothy Prinzing |
|
40 |
* @see View |
|
41 |
*/ |
|
42 |
public class PasswordView extends FieldView { |
|
43 |
||
44 |
/** |
|
45 |
* Constructs a new view wrapped on an element. |
|
46 |
* |
|
47 |
* @param elem the element |
|
48 |
*/ |
|
49 |
public PasswordView(Element elem) { |
|
50 |
super(elem); |
|
51 |
} |
|
52 |
||
53 |
/** |
|
54 |
* Renders the given range in the model as normal unselected |
|
55 |
* text. This sets the foreground color and echos the characters |
|
56 |
* using the value returned by getEchoChar(). |
|
57 |
* |
|
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 | 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 | 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 | 71 |
protected int drawUnselectedText(Graphics g, int x, int y, |
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 | 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 | 90 |
Container c = getContainer(); |
91 |
if (c instanceof JPasswordField) { |
|
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 | 100 |
} |
101 |
if (f.isEnabled()) { |
|
102 |
g.setColor(f.getForeground()); |
|
103 |
} |
|
104 |
else { |
|
105 |
g.setColor(f.getDisabledTextColor()); |
|
106 |
} |
|
107 |
char echoChar = f.getEchoChar(); |
|
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 | 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 | 116 |
} |
117 |
} |
|
118 |
return x; |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Renders the given range in the model as selected text. This |
|
123 |
* is implemented to render the text in the color specified in |
|
124 |
* the hosting component. It assumes the highlighter will render |
|
125 |
* the selected background. Uses the result of getEchoChar() to |
|
126 |
* display the characters. |
|
127 |
* |
|
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 >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
130 |
* @param y the starting Y coordinate >= 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 >= 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 >= 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 >= 0 |
2 | 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 | 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 | 141 |
protected int drawSelectedText(Graphics g, int x, |
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 | 158 |
g.setColor(selected); |
159 |
Container c = getContainer(); |
|
160 |
if (c instanceof JPasswordField) { |
|
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 | 169 |
} |
170 |
char echoChar = f.getEchoChar(); |
|
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 | 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 | 180 |
} |
181 |
} |
|
182 |
return x; |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Renders the echo character, or whatever graphic should be used |
|
187 |
* to display the password characters. The color in the Graphics |
|
188 |
* object is set to the appropriate foreground color for selected |
|
189 |
* or unselected text. |
|
190 |
* |
|
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 >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
193 |
* @param y the starting Y coordinate >= 0 |
2 | 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 >= 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 | 199 |
*/ |
41807
f9eb6cb54fed
8156217: Selected text is shifted on HiDPI display
alexsch
parents:
39553
diff
changeset
|
200 |
@Deprecated(since = "9") |
2 | 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 | 203 |
} |
204 |
||
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 | 237 |
* Provides a mapping from the document model coordinate space |
238 |
* to the coordinate space of the view mapped to it. |
|
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 >= 0 |
2 | 241 |
* @param a the allocated region to render into |
242 |
* @return the bounding box of the given position |
|
243 |
* @exception BadLocationException if the given position does not |
|
244 |
* represent a valid location in the associated document |
|
245 |
* @see View#modelToView |
|
246 |
*/ |
|
247 |
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { |
|
248 |
Container c = getContainer(); |
|
249 |
if (c instanceof JPasswordField) { |
|
250 |
JPasswordField f = (JPasswordField) c; |
|
251 |
if (! f.echoCharIsSet()) { |
|
252 |
return super.modelToView(pos, a, b); |
|
253 |
} |
|
254 |
char echoChar = f.getEchoChar(); |
|
255 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
256 |
||
257 |
Rectangle alloc = adjustAllocation(a).getBounds(); |
|
258 |
int dx = (pos - getStartOffset()) * m.charWidth(echoChar); |
|
259 |
alloc.x += dx; |
|
260 |
alloc.width = 1; |
|
261 |
return alloc; |
|
262 |
} |
|
263 |
return null; |
|
264 |
} |
|
265 |
||
266 |
/** |
|
267 |
* Provides a mapping from the view coordinate space to the logical |
|
268 |
* coordinate space of the model. |
|
269 |
* |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
270 |
* @param fx the X coordinate >= 0.0f |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
271 |
* @param fy the Y coordinate >= 0.0f |
2 | 272 |
* @param a the allocated region to render into |
273 |
* @return the location within the model that best represents the |
|
274 |
* given point in the view |
|
275 |
* @see View#viewToModel |
|
276 |
*/ |
|
277 |
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) { |
|
278 |
bias[0] = Position.Bias.Forward; |
|
279 |
int n = 0; |
|
280 |
Container c = getContainer(); |
|
281 |
if (c instanceof JPasswordField) { |
|
282 |
JPasswordField f = (JPasswordField) c; |
|
283 |
if (! f.echoCharIsSet()) { |
|
284 |
return super.viewToModel(fx, fy, a, bias); |
|
285 |
} |
|
286 |
char echoChar = f.getEchoChar(); |
|
287 |
int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar); |
|
288 |
a = adjustAllocation(a); |
|
289 |
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : |
|
290 |
a.getBounds(); |
|
291 |
n = (charWidth > 0 ? |
|
292 |
((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE); |
|
293 |
if (n < 0) { |
|
294 |
n = 0; |
|
295 |
} |
|
296 |
else if (n > (getStartOffset() + getDocument().getLength())) { |
|
297 |
n = getDocument().getLength() - getStartOffset(); |
|
298 |
} |
|
299 |
} |
|
300 |
return getStartOffset() + n; |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* Determines the preferred span for this view along an |
|
305 |
* axis. |
|
306 |
* |
|
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 >= 0. |
2 | 309 |
* Typically the view is told to render into the span |
310 |
* that is returned, although there is no guarantee. |
|
311 |
* The parent may choose to resize or break the view. |
|
312 |
*/ |
|
313 |
public float getPreferredSpan(int axis) { |
|
314 |
switch (axis) { |
|
315 |
case View.X_AXIS: |
|
316 |
Container c = getContainer(); |
|
317 |
if (c instanceof JPasswordField) { |
|
318 |
JPasswordField f = (JPasswordField) c; |
|
319 |
if (f.echoCharIsSet()) { |
|
320 |
char echoChar = f.getEchoChar(); |
|
321 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
322 |
Document doc = getDocument(); |
|
323 |
return m.charWidth(echoChar) * getDocument().getLength(); |
|
324 |
} |
|
325 |
} |
|
326 |
} |
|
327 |
return super.getPreferredSpan(axis); |
|
328 |
} |
|
329 |
||
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 | 334 |
} |