author | darcy |
Wed, 22 Jan 2014 23:20:58 -0800 | |
changeset 22567 | 5816a47fa4dd |
parent 20158 | 1c5d22e5b898 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2006, 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.*; |
|
29 |
import javax.swing.JPasswordField; |
|
30 |
||
31 |
/** |
|
32 |
* Implements a View suitable for use in JPasswordField |
|
33 |
* UI implementations. This is basically a field ui that |
|
34 |
* renders its contents as the echo character specified |
|
35 |
* in the associated component (if it can narrow the |
|
36 |
* component to a JPasswordField). |
|
37 |
* |
|
38 |
* @author Timothy Prinzing |
|
39 |
* @see View |
|
40 |
*/ |
|
41 |
public class PasswordView extends FieldView { |
|
42 |
||
43 |
/** |
|
44 |
* Constructs a new view wrapped on an element. |
|
45 |
* |
|
46 |
* @param elem the element |
|
47 |
*/ |
|
48 |
public PasswordView(Element elem) { |
|
49 |
super(elem); |
|
50 |
} |
|
51 |
||
52 |
/** |
|
53 |
* Renders the given range in the model as normal unselected |
|
54 |
* text. This sets the foreground color and echos the characters |
|
55 |
* using the value returned by getEchoChar(). |
|
56 |
* |
|
57 |
* @param g the graphics context |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
58 |
* @param x the starting X coordinate >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
59 |
* @param y the starting Y coordinate >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
60 |
* @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
|
61 |
* @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
|
62 |
* @return the X location of the end of the range >= 0 |
2 | 63 |
* @exception BadLocationException if p0 or p1 are out of range |
64 |
*/ |
|
65 |
protected int drawUnselectedText(Graphics g, int x, int y, |
|
66 |
int p0, int p1) throws BadLocationException { |
|
67 |
||
68 |
Container c = getContainer(); |
|
69 |
if (c instanceof JPasswordField) { |
|
70 |
JPasswordField f = (JPasswordField) c; |
|
71 |
if (! f.echoCharIsSet()) { |
|
72 |
return super.drawUnselectedText(g, x, y, p0, p1); |
|
73 |
} |
|
74 |
if (f.isEnabled()) { |
|
75 |
g.setColor(f.getForeground()); |
|
76 |
} |
|
77 |
else { |
|
78 |
g.setColor(f.getDisabledTextColor()); |
|
79 |
} |
|
80 |
char echoChar = f.getEchoChar(); |
|
81 |
int n = p1 - p0; |
|
82 |
for (int i = 0; i < n; i++) { |
|
83 |
x = drawEchoCharacter(g, x, y, echoChar); |
|
84 |
} |
|
85 |
} |
|
86 |
return x; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Renders the given range in the model as selected text. This |
|
91 |
* is implemented to render the text in the color specified in |
|
92 |
* the hosting component. It assumes the highlighter will render |
|
93 |
* the selected background. Uses the result of getEchoChar() to |
|
94 |
* display the characters. |
|
95 |
* |
|
96 |
* @param g the graphics context |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
97 |
* @param x the starting X coordinate >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
98 |
* @param y the starting Y coordinate >= 0 |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
99 |
* @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
|
100 |
* @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
|
101 |
* @return the X location of the end of the range >= 0 |
2 | 102 |
* @exception BadLocationException if p0 or p1 are out of range |
103 |
*/ |
|
104 |
protected int drawSelectedText(Graphics g, int x, |
|
105 |
int y, int p0, int p1) throws BadLocationException { |
|
106 |
g.setColor(selected); |
|
107 |
Container c = getContainer(); |
|
108 |
if (c instanceof JPasswordField) { |
|
109 |
JPasswordField f = (JPasswordField) c; |
|
110 |
if (! f.echoCharIsSet()) { |
|
111 |
return super.drawSelectedText(g, x, y, p0, p1); |
|
112 |
} |
|
113 |
char echoChar = f.getEchoChar(); |
|
114 |
int n = p1 - p0; |
|
115 |
for (int i = 0; i < n; i++) { |
|
116 |
x = drawEchoCharacter(g, x, y, echoChar); |
|
117 |
} |
|
118 |
} |
|
119 |
return x; |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Renders the echo character, or whatever graphic should be used |
|
124 |
* to display the password characters. The color in the Graphics |
|
125 |
* object is set to the appropriate foreground color for selected |
|
126 |
* or unselected text. |
|
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 |
2 | 131 |
* @param c the echo character |
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
132 |
* @return the updated X position >= 0 |
2 | 133 |
*/ |
134 |
protected int drawEchoCharacter(Graphics g, int x, int y, char c) { |
|
135 |
ONE[0] = c; |
|
136 |
SwingUtilities2.drawChars(Utilities.getJComponent(this), |
|
137 |
g, ONE, 0, 1, x, y); |
|
138 |
return x + g.getFontMetrics().charWidth(c); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Provides a mapping from the document model coordinate space |
|
143 |
* to the coordinate space of the view mapped to it. |
|
144 |
* |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
145 |
* @param pos the position to convert >= 0 |
2 | 146 |
* @param a the allocated region to render into |
147 |
* @return the bounding box of the given position |
|
148 |
* @exception BadLocationException if the given position does not |
|
149 |
* represent a valid location in the associated document |
|
150 |
* @see View#modelToView |
|
151 |
*/ |
|
152 |
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { |
|
153 |
Container c = getContainer(); |
|
154 |
if (c instanceof JPasswordField) { |
|
155 |
JPasswordField f = (JPasswordField) c; |
|
156 |
if (! f.echoCharIsSet()) { |
|
157 |
return super.modelToView(pos, a, b); |
|
158 |
} |
|
159 |
char echoChar = f.getEchoChar(); |
|
160 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
161 |
||
162 |
Rectangle alloc = adjustAllocation(a).getBounds(); |
|
163 |
int dx = (pos - getStartOffset()) * m.charWidth(echoChar); |
|
164 |
alloc.x += dx; |
|
165 |
alloc.width = 1; |
|
166 |
return alloc; |
|
167 |
} |
|
168 |
return null; |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Provides a mapping from the view coordinate space to the logical |
|
173 |
* coordinate space of the model. |
|
174 |
* |
|
20158
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
175 |
* @param fx the X coordinate >= 0.0f |
1c5d22e5b898
8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents:
5506
diff
changeset
|
176 |
* @param fy the Y coordinate >= 0.0f |
2 | 177 |
* @param a the allocated region to render into |
178 |
* @return the location within the model that best represents the |
|
179 |
* given point in the view |
|
180 |
* @see View#viewToModel |
|
181 |
*/ |
|
182 |
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) { |
|
183 |
bias[0] = Position.Bias.Forward; |
|
184 |
int n = 0; |
|
185 |
Container c = getContainer(); |
|
186 |
if (c instanceof JPasswordField) { |
|
187 |
JPasswordField f = (JPasswordField) c; |
|
188 |
if (! f.echoCharIsSet()) { |
|
189 |
return super.viewToModel(fx, fy, a, bias); |
|
190 |
} |
|
191 |
char echoChar = f.getEchoChar(); |
|
192 |
int charWidth = f.getFontMetrics(f.getFont()).charWidth(echoChar); |
|
193 |
a = adjustAllocation(a); |
|
194 |
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : |
|
195 |
a.getBounds(); |
|
196 |
n = (charWidth > 0 ? |
|
197 |
((int)fx - alloc.x) / charWidth : Integer.MAX_VALUE); |
|
198 |
if (n < 0) { |
|
199 |
n = 0; |
|
200 |
} |
|
201 |
else if (n > (getStartOffset() + getDocument().getLength())) { |
|
202 |
n = getDocument().getLength() - getStartOffset(); |
|
203 |
} |
|
204 |
} |
|
205 |
return getStartOffset() + n; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Determines the preferred span for this view along an |
|
210 |
* axis. |
|
211 |
* |
|
212 |
* @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
|
213 |
* @return the span the view would like to be rendered into >= 0. |
2 | 214 |
* Typically the view is told to render into the span |
215 |
* that is returned, although there is no guarantee. |
|
216 |
* The parent may choose to resize or break the view. |
|
217 |
*/ |
|
218 |
public float getPreferredSpan(int axis) { |
|
219 |
switch (axis) { |
|
220 |
case View.X_AXIS: |
|
221 |
Container c = getContainer(); |
|
222 |
if (c instanceof JPasswordField) { |
|
223 |
JPasswordField f = (JPasswordField) c; |
|
224 |
if (f.echoCharIsSet()) { |
|
225 |
char echoChar = f.getEchoChar(); |
|
226 |
FontMetrics m = f.getFontMetrics(f.getFont()); |
|
227 |
Document doc = getDocument(); |
|
228 |
return m.charWidth(echoChar) * getDocument().getLength(); |
|
229 |
} |
|
230 |
} |
|
231 |
} |
|
232 |
return super.getPreferredSpan(axis); |
|
233 |
} |
|
234 |
||
235 |
static char[] ONE = new char[1]; |
|
236 |
} |