1 |
/* PasswordView.java -- |
/* PasswordView.java -- |
2 |
Copyright (C) 2004 Free Software Foundation, Inc. |
Copyright (C) 2004 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
6 |
GNU Classpath is free software; you can redistribute it and/or modify |
GNU Classpath is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2, or (at your option) |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
any later version. |
10 |
|
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
General Public License for more details. |
General Public License for more details. |
15 |
|
|
16 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
17 |
along with GNU Classpath; see the file COPYING. If not, write to the |
along with GNU Classpath; see the file COPYING. If not, write to the |
18 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
02110-1301 USA. |
02110-1301 USA. |
20 |
|
|
21 |
Linking this library statically or dynamically with other modules is |
Linking this library statically or dynamically with other modules is |
22 |
making a combined work based on this library. Thus, the terms and |
making a combined work based on this library. Thus, the terms and |
23 |
conditions of the GNU General Public License cover the whole |
conditions of the GNU General Public License cover the whole |
24 |
combination. |
combination. |
25 |
|
|
26 |
As a special exception, the copyright holders of this library give you |
As a special exception, the copyright holders of this library give you |
27 |
permission to link this library with independent modules to produce an |
permission to link this library with independent modules to produce an |
28 |
executable, regardless of the license terms of these independent |
executable, regardless of the license terms of these independent |
29 |
modules, and to copy and distribute the resulting executable under |
modules, and to copy and distribute the resulting executable under |
30 |
terms of your choice, provided that you also meet, for each linked |
terms of your choice, provided that you also meet, for each linked |
31 |
independent module, the terms and conditions of the license of that |
independent module, the terms and conditions of the license of that |
32 |
module. An independent module is a module which is not derived from |
module. An independent module is a module which is not derived from |
33 |
or based on this library. If you modify this library, you may extend |
or based on this library. If you modify this library, you may extend |
34 |
this exception to your version of the library, but you are not |
this exception to your version of the library, but you are not |
35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package javax.swing.text; |
package javax.swing.text; |
40 |
|
|
41 |
import java.awt.Color; |
import java.awt.Color; |
42 |
|
import java.awt.FontMetrics; |
43 |
import java.awt.Graphics; |
import java.awt.Graphics; |
44 |
|
import java.awt.Shape; |
45 |
|
|
46 |
import javax.swing.JPasswordField; |
import javax.swing.JPasswordField; |
47 |
|
|
48 |
public class PasswordView extends FieldView |
public class PasswordView |
49 |
|
extends FieldView |
50 |
{ |
{ |
51 |
/** |
/** |
52 |
* Buffer for putting the echo char into it and |
* Buffer for putting the echo char into it and |
53 |
* then using it to draw it into the view. |
* then using it to draw it into the view. |
54 |
*/ |
*/ |
55 |
private char[] oneCharBuffer = new char[1]; |
private char[] oneCharBuffer = new char[1]; |
56 |
|
|
57 |
public PasswordView(Element elem) |
public PasswordView(Element elem) |
58 |
{ |
{ |
59 |
super(elem); |
super(elem); |
73 |
{ |
{ |
74 |
// Update font metrics. |
// Update font metrics. |
75 |
updateMetrics(); |
updateMetrics(); |
76 |
|
|
77 |
// Draw character. |
// Draw character. |
78 |
oneCharBuffer[0] = ch; |
oneCharBuffer[0] = ch; |
79 |
g.drawChars(oneCharBuffer, 0, 1, x, y); |
g.drawChars(oneCharBuffer, 0, 1, x, y); |
85 |
private char getEchoChar() |
private char getEchoChar() |
86 |
{ |
{ |
87 |
char ch = ((JPasswordField) getContainer()).getEchoChar(); |
char ch = ((JPasswordField) getContainer()).getEchoChar(); |
88 |
|
|
89 |
if (ch == 0) |
if (ch == 0) |
90 |
ch = '*'; |
ch = '*'; |
91 |
|
|
110 |
|
|
111 |
// Update font metrics. |
// Update font metrics. |
112 |
updateMetrics(); |
updateMetrics(); |
113 |
|
|
114 |
// Get echo character. |
// Get echo character. |
115 |
char ch = getEchoChar(); |
char ch = getEchoChar(); |
116 |
|
|
117 |
// Set color for selected text. |
// Set color for selected text. |
118 |
g.setColor(selectedColor); |
g.setColor(selectedColor); |
119 |
g.setColor(Color.BLACK); |
g.setColor(Color.BLACK); |
123 |
char[] buffer = new char[len]; |
char[] buffer = new char[len]; |
124 |
for (int index = 0; index < len; ++index) |
for (int index = 0; index < len; ++index) |
125 |
buffer[index] = ch; |
buffer[index] = ch; |
126 |
|
|
127 |
// Draw echo charaters. |
// Draw echo charaters. |
128 |
g.drawChars(buffer, 0, len, x, y); |
g.drawChars(buffer, 0, len, x, y); |
129 |
|
|
149 |
|
|
150 |
// Update font metrics. |
// Update font metrics. |
151 |
updateMetrics(); |
updateMetrics(); |
152 |
|
|
153 |
// Get echo character. |
// Get echo character. |
154 |
char ch = getEchoChar(); |
char ch = getEchoChar(); |
155 |
|
Segment segment = new Segment(); |
156 |
|
|
157 |
// Set color for unselected text. |
// Set color for unselected text. |
158 |
g.setColor(unselectedColor); |
g.setColor(unselectedColor); |
159 |
g.setColor(Color.BLACK); |
g.setColor(Color.BLACK); |
160 |
|
|
161 |
// Initialize buffer for faster drawing of all characters. |
// Initialize buffer for faster drawing of all characters. |
162 |
int len = p1 - p0; |
p1--; |
163 |
|
getDocument().getText(p0, p1 - p0, segment); |
164 |
|
int len = segment.toString().length(); |
165 |
|
|
166 |
char[] buffer = new char[len]; |
char[] buffer = new char[len]; |
167 |
for (int index = 0; index < len; ++index) |
for (int index = 0; index < len; ++index) |
168 |
buffer[index] = ch; |
buffer[index] = ch; |
169 |
|
|
170 |
|
y += getPreferredSpan(Y_AXIS)/2; |
171 |
|
|
172 |
// Draw echo charaters. |
// Draw echo charaters. |
173 |
g.drawChars(buffer, 0, len, x, y); |
g.drawChars(buffer, 0, len, x, y); |
174 |
|
|
175 |
// Return new x position right of all drawn characters. |
// Return new x position right of all drawn characters. |
176 |
return x + len * metrics.charWidth(ch); |
return x + (len * metrics.charWidth(ch)); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
|
* Determines the preferred span for this view along an axis. |
181 |
|
* |
182 |
|
* @param axis to get the preferred span of |
183 |
|
* @return the preferred span of the axis |
184 |
|
*/ |
185 |
|
public float getPreferredSpan(int axis) |
186 |
|
{ |
187 |
|
if (axis != X_AXIS && axis != Y_AXIS) |
188 |
|
throw new IllegalArgumentException(); |
189 |
|
|
190 |
|
FontMetrics fm = getFontMetrics(); |
191 |
|
|
192 |
|
if (axis == Y_AXIS) |
193 |
|
return fm.getHeight(); |
194 |
|
|
195 |
|
String text; |
196 |
|
Element elem = getElement(); |
197 |
|
|
198 |
|
try |
199 |
|
{ |
200 |
|
text = elem.getDocument().getText(elem.getStartOffset(), |
201 |
|
elem.getEndOffset()); |
202 |
|
} |
203 |
|
catch (BadLocationException e) |
204 |
|
{ |
205 |
|
// This should never happen. |
206 |
|
text = ""; |
207 |
|
} |
208 |
|
return fm.stringWidth(text); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Provides a mapping from the document model coordinate space to the |
213 |
|
* coordinate space of the view mapped to it. |
214 |
|
* |
215 |
|
* @param pos - the position to convert >= 0 |
216 |
|
* @param a - the allocated region to render into |
217 |
|
* @param b - typesafe enumeration to indicate bias to a position in the model. |
218 |
|
* @return the bounding box of the given position |
219 |
|
* @throws BadLocationException if the given position does not |
220 |
|
* represent a valid location in the associated document |
221 |
|
*/ |
222 |
|
public Shape modelToView(int pos, Shape a, Position.Bias b) |
223 |
|
throws BadLocationException |
224 |
|
{ |
225 |
|
return super.modelToView(pos, a, b); |
226 |
} |
} |
|
} |
|
227 |
|
|
228 |
|
/** |
229 |
|
* Provides a mapping from the view coordinate space to the logical |
230 |
|
* coordinate space of the model. |
231 |
|
* |
232 |
|
* @param fx - the X coordinate >= 0.0f |
233 |
|
* @param fy - the Y coordinate >= 0.0f |
234 |
|
* @param a - the allocated region to render into |
235 |
|
* @param bias - typesafe enumeration to indicate bias to a position in the model. |
236 |
|
* @return the location within the model that best represents |
237 |
|
* the given point in the view |
238 |
|
* |
239 |
|
*/ |
240 |
|
public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) |
241 |
|
{ |
242 |
|
return super.viewToModel(fx, fy, a, bias); |
243 |
|
} |
244 |
|
} |