- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathPasswordView.java
334 lines (315 loc) · 12.8 KB
/
PasswordView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing.text;
importsun.swing.SwingUtilities2;
importjava.awt.*;
importjava.awt.font.FontRenderContext;
importjavax.swing.JPasswordField;
/**
* Implements a View suitable for use in JPasswordField
* UI implementations. This is basically a field ui that
* renders its contents as the echo character specified
* in the associated component (if it can narrow the
* component to a JPasswordField).
*
* @author Timothy Prinzing
* @see View
*/
publicclassPasswordViewextendsFieldView {
/**
* Constructs a new view wrapped on an element.
*
* @param elem the element
*/
publicPasswordView(Elementelem) {
super(elem);
}
/**
* Renders the given range in the model as normal unselected
* text. This sets the foreground color and echos the characters
* using the value returned by getEchoChar().
*
* @param g the graphics context
* @param x the starting X coordinate >= 0
* @param y the starting Y coordinate >= 0
* @param p0 the starting offset in the model >= 0
* @param p1 the ending offset in the model >= p0
* @return the X location of the end of the range >= 0
* @throws BadLocationException if p0 or p1 are out of range
*
* @deprecated replaced by
* {@link #drawUnselectedText(Graphics2D, float, float, int, int)}
*/
@Deprecated(since = "9")
@Override
protectedintdrawUnselectedText(Graphicsg, intx, inty,
intp0, intp1) throwsBadLocationException {
return (int) drawUnselectedTextImpl(g, x, y, p0, p1, false);
}
@Override
protectedfloatdrawUnselectedText(Graphics2Dg, floatx, floaty,
intp0, intp1)
throwsBadLocationException
{
returndrawUnselectedTextImpl(g, x, y, p0, p1, true);
}
@SuppressWarnings("deprecation")
privatefloatdrawUnselectedTextImpl(Graphicsg, floatx, floaty,
intp0, intp1,
booleanuseFPAPI)
throwsBadLocationException
{
Containerc = getContainer();
if (cinstanceofJPasswordField) {
JPasswordFieldf = (JPasswordField) c;
if (!f.echoCharIsSet()) {
booleanuseDrawUnselectedFPAPI = useFPAPI
&& drawUnselectedTextOverridden
&& ginstanceofGraphics2D;
return (useDrawUnselectedFPAPI )
? super.drawUnselectedText((Graphics2D) g, x, y, p0, p1)
: super.drawUnselectedText(g, (int) x, (int) y, p0, p1);
}
if (f.isEnabled()) {
g.setColor(f.getForeground());
}
else {
g.setColor(f.getDisabledTextColor());
}
charechoChar = f.getEchoChar();
intn = p1 - p0;
booleanuseEchoCharFPAPI = useFPAPI
&& drawEchoCharacterOverridden
&& ginstanceofGraphics2D;
for (inti = 0; i < n; i++) {
x = (useEchoCharFPAPI)
? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
: drawEchoCharacter(g, (int) x, (int) y, echoChar);
}
}
returnx;
}
/**
* Renders the given range in the model as selected text. This
* is implemented to render the text in the color specified in
* the hosting component. It assumes the highlighter will render
* the selected background. Uses the result of getEchoChar() to
* display the characters.
*
* @param g the graphics context
* @param x the starting X coordinate >= 0
* @param y the starting Y coordinate >= 0
* @param p0 the starting offset in the model >= 0
* @param p1 the ending offset in the model >= p0
* @return the X location of the end of the range >= 0
* @throws BadLocationException if p0 or p1 are out of range
*
* @deprecated replaced by
* {@link #drawSelectedText(Graphics2D, float, float, int, int)}
*/
@Deprecated(since = "9")
@Override
protectedintdrawSelectedText(Graphicsg, intx,
inty, intp0, intp1) throwsBadLocationException {
return (int) drawSelectedTextImpl(g, x, y, p0, p1, false);
}
@Override
protectedfloatdrawSelectedText(Graphics2Dg, floatx, floaty,
intp0, intp1) throwsBadLocationException
{
returndrawSelectedTextImpl(g, x, y, p0, p1, true);
}
@SuppressWarnings("deprecation")
privatefloatdrawSelectedTextImpl(Graphicsg, floatx, floaty,
intp0, intp1,
booleanuseFPAPI)
throwsBadLocationException {
g.setColor(selected);
Containerc = getContainer();
if (cinstanceofJPasswordField) {
JPasswordFieldf = (JPasswordField) c;
if (!f.echoCharIsSet()) {
booleanuseDrawUnselectedFPAPI = useFPAPI
&& drawSelectedTextOverridden
&& ginstanceofGraphics2D;
return (useFPAPI)
? super.drawSelectedText((Graphics2D) g, x, y, p0, p1)
: super.drawSelectedText(g, (int) x, (int) y, p0, p1);
}
charechoChar = f.getEchoChar();
intn = p1 - p0;
booleanuseEchoCharFPAPI = useFPAPI
&& drawEchoCharacterOverridden
&& ginstanceofGraphics2D;
for (inti = 0; i < n; i++) {
x = (useEchoCharFPAPI)
? drawEchoCharacter((Graphics2D) g, x, y, echoChar)
: drawEchoCharacter(g, (int) x, (int) y, echoChar);
}
}
returnx;
}
/**
* Renders the echo character, or whatever graphic should be used
* to display the password characters. The color in the Graphics
* object is set to the appropriate foreground color for selected
* or unselected text.
*
* @param g the graphics context
* @param x the starting X coordinate >= 0
* @param y the starting Y coordinate >= 0
* @param c the echo character
* @return the updated X position >= 0
*
* @deprecated replaced by
* {@link #drawEchoCharacter(Graphics2D, float, float, char)}
*/
@Deprecated(since = "9")
protectedintdrawEchoCharacter(Graphicsg, intx, inty, charc) {
return (int) drawEchoCharacterImpl(g, x, y, c, false);
}
/**
* Renders the echo character, or whatever graphic should be used
* to display the password characters. The color in the Graphics
* object is set to the appropriate foreground color for selected
* or unselected text.
*
* @param g the graphics context
* @param x the starting X coordinate {@code >= 0}
* @param y the starting Y coordinate {@code >= 0}
* @param c the echo character
* @return the updated X position {@code >= 0}
*
* @since 9
*/
protectedfloatdrawEchoCharacter(Graphics2Dg, floatx, floaty, charc) {
returndrawEchoCharacterImpl(g, x, y, c, true);
}
privatefloatdrawEchoCharacterImpl(Graphicsg, floatx, floaty,
charc, booleanuseFPAPI) {
ONE[0] = c;
SwingUtilities2.drawChars(Utilities.getJComponent(this),
g, ONE, 0, 1, x, y);
if (useFPAPI) {
returnx + g.getFontMetrics().charWidth(c);
} else {
FontRenderContextfrc = g.getFontMetrics().getFontRenderContext();
returnx + (float) g.getFont().getStringBounds(ONE, 0, 1, frc).getWidth();
}
}
/**
* Provides a mapping from the document model coordinate space
* to the coordinate space of the view mapped to it.
*
* @param pos the position to convert >= 0
* @param a the allocated region to render into
* @return the bounding box of the given position
* @throws BadLocationException if the given position does not
* represent a valid location in the associated document
* @see View#modelToView
*/
publicShapemodelToView(intpos, Shapea, Position.Biasb) throwsBadLocationException {
Containerc = getContainer();
if (cinstanceofJPasswordField) {
JPasswordFieldf = (JPasswordField) c;
if (! f.echoCharIsSet()) {
returnsuper.modelToView(pos, a, b);
}
charechoChar = f.getEchoChar();
FontMetricsm = f.getFontMetrics(f.getFont());
Rectanglealloc = adjustAllocation(a).getBounds();
intdx = (pos - getStartOffset()) * m.charWidth(echoChar);
alloc.x += dx;
alloc.width = 1;
returnalloc;
}
returnnull;
}
/**
* Provides a mapping from the view coordinate space to the logical
* coordinate space of the model.
*
* @param fx the X coordinate >= 0.0f
* @param fy the Y coordinate >= 0.0f
* @param a the allocated region to render into
* @return the location within the model that best represents the
* given point in the view
* @see View#viewToModel
*/
publicintviewToModel(floatfx, floatfy, Shapea, Position.Bias[] bias) {
bias[0] = Position.Bias.Forward;
intn = 0;
Containerc = getContainer();
if (cinstanceofJPasswordField) {
JPasswordFieldf = (JPasswordField) c;
if (! f.echoCharIsSet()) {
returnsuper.viewToModel(fx, fy, a, bias);
}
charechoChar = f.getEchoChar();
intcharWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar);
a = adjustAllocation(a);
Rectanglealloc = (ainstanceofRectangle) ? (Rectangle)a :
a.getBounds();
n = (charWidth > 0 ?
((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE);
if (n < 0) {
n = 0;
}
elseif (n > (getStartOffset() + getDocument().getLength())) {
n = getDocument().getLength() - getStartOffset();
}
}
returngetStartOffset() + n;
}
/**
* Determines the preferred span for this view along an
* axis.
*
* @param axis may be either View.X_AXIS or View.Y_AXIS
* @return the span the view would like to be rendered into >= 0.
* Typically the view is told to render into the span
* that is returned, although there is no guarantee.
* The parent may choose to resize or break the view.
*/
publicfloatgetPreferredSpan(intaxis) {
switch (axis) {
caseView.X_AXIS:
Containerc = getContainer();
if (cinstanceofJPasswordField) {
JPasswordFieldf = (JPasswordField) c;
if (f.echoCharIsSet()) {
charechoChar = f.getEchoChar();
FontMetricsm = f.getFontMetrics(f.getFont());
Documentdoc = getDocument();
returnm.charWidth(echoChar) * getDocument().getLength();
}
}
}
returnsuper.getPreferredSpan(axis);
}
staticchar[] ONE = newchar[1];
privatefinalbooleandrawEchoCharacterOverridden =
getFPMethodOverridden(getClass(), "drawEchoCharacter", FPMethodArgs.GNNC);
}