KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > PasswordView


1 /*
2  * @(#)PasswordView.java 1.20 04/04/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import com.sun.java.swing.SwingUtilities2;
10 import java.awt.*;
11 import javax.swing.JPasswordField JavaDoc;
12
13 /**
14  * Implements a View suitable for use in JPasswordField
15  * UI implementations. This is basically a field ui that
16  * renders its contents as the echo character specified
17  * in the associated component (if it can narrow the
18  * component to a JPasswordField).
19  *
20  * @author Timothy Prinzing
21  * @version 1.20 04/15/04
22  * @see View
23  */

24 public class PasswordView extends FieldView JavaDoc {
25
26     /**
27      * Constructs a new view wrapped on an element.
28      *
29      * @param elem the element
30      */

31     public PasswordView(Element JavaDoc elem) {
32     super(elem);
33     }
34
35     /**
36      * Renders the given range in the model as normal unselected
37      * text. This sets the foreground color and echos the characters
38      * using the value returned by getEchoChar().
39      *
40      * @param g the graphics context
41      * @param x the starting X coordinate >= 0
42      * @param y the starting Y coordinate >= 0
43      * @param p0 the starting offset in the model >= 0
44      * @param p1 the ending offset in the model >= p0
45      * @return the X location of the end of the range >= 0
46      * @exception BadLocationException if p0 or p1 are out of range
47      */

48     protected int drawUnselectedText(Graphics g, int x, int y,
49                      int p0, int p1) throws BadLocationException JavaDoc {
50
51     Container c = getContainer();
52     if (c instanceof JPasswordField JavaDoc) {
53         JPasswordField JavaDoc f = (JPasswordField JavaDoc) c;
54         if (! f.echoCharIsSet()) {
55         return super.drawUnselectedText(g, x, y, p0, p1);
56         }
57             if (f.isEnabled()) {
58                 g.setColor(f.getForeground());
59             }
60             else {
61                 g.setColor(f.getDisabledTextColor());
62             }
63         char echoChar = f.getEchoChar();
64         int n = p1 - p0;
65         for (int i = 0; i < n; i++) {
66         x = drawEchoCharacter(g, x, y, echoChar);
67         }
68     }
69     return x;
70     }
71
72     /**
73      * Renders the given range in the model as selected text. This
74      * is implemented to render the text in the color specified in
75      * the hosting component. It assumes the highlighter will render
76      * the selected background. Uses the result of getEchoChar() to
77      * display the characters.
78      *
79      * @param g the graphics context
80      * @param x the starting X coordinate >= 0
81      * @param y the starting Y coordinate >= 0
82      * @param p0 the starting offset in the model >= 0
83      * @param p1 the ending offset in the model >= p0
84      * @return the X location of the end of the range >= 0
85      * @exception BadLocationException if p0 or p1 are out of range
86      */

87     protected int drawSelectedText(Graphics g, int x,
88                    int y, int p0, int p1) throws BadLocationException JavaDoc {
89     g.setColor(selected);
90     Container c = getContainer();
91     if (c instanceof JPasswordField JavaDoc) {
92         JPasswordField JavaDoc f = (JPasswordField JavaDoc) c;
93         if (! f.echoCharIsSet()) {
94         return super.drawSelectedText(g, x, y, p0, p1);
95         }
96         char echoChar = f.getEchoChar();
97         int n = p1 - p0;
98         for (int i = 0; i < n; i++) {
99         x = drawEchoCharacter(g, x, y, echoChar);
100         }
101     }
102     return x;
103     }
104
105     /**
106      * Renders the echo character, or whatever graphic should be used
107      * to display the password characters. The color in the Graphics
108      * object is set to the appropriate foreground color for selected
109      * or unselected text.
110      *
111      * @param g the graphics context
112      * @param x the starting X coordinate >= 0
113      * @param y the starting Y coordinate >= 0
114      * @param c the echo character
115      * @return the updated X position >= 0
116      */

117     protected int drawEchoCharacter(Graphics g, int x, int y, char c) {
118     ONE[0] = c;
119         SwingUtilities2.drawChars(Utilities.getJComponent(this),
120                                   g, ONE, 0, 1, x, y);
121     return x + g.getFontMetrics().charWidth(c);
122     }
123
124     /**
125      * Provides a mapping from the document model coordinate space
126      * to the coordinate space of the view mapped to it.
127      *
128      * @param pos the position to convert >= 0
129      * @param a the allocated region to render into
130      * @return the bounding box of the given position
131      * @exception BadLocationException if the given position does not
132      * represent a valid location in the associated document
133      * @see View#modelToView
134      */

135     public Shape modelToView(int pos, Shape a, Position.Bias JavaDoc b) throws BadLocationException JavaDoc {
136     Container c = getContainer();
137     if (c instanceof JPasswordField JavaDoc) {
138         JPasswordField JavaDoc f = (JPasswordField JavaDoc) c;
139         if (! f.echoCharIsSet()) {
140         return super.modelToView(pos, a, b);
141         }
142         char echoChar = f.getEchoChar();
143         FontMetrics m = f.getFontMetrics(f.getFont());
144         
145         Rectangle alloc = adjustAllocation(a).getBounds();
146         int dx = (pos - getStartOffset()) * m.charWidth(echoChar);
147         alloc.x += dx;
148         alloc.width = 1;
149         return alloc;
150     }
151     return null;
152     }
153
154     /**
155      * Provides a mapping from the view coordinate space to the logical
156      * coordinate space of the model.
157      *
158      * @param fx the X coordinate >= 0.0f
159      * @param fy the Y coordinate >= 0.0f
160      * @param a the allocated region to render into
161      * @return the location within the model that best represents the
162      * given point in the view
163      * @see View#viewToModel
164      */

165     public int viewToModel(float fx, float fy, Shape a, Position.Bias JavaDoc[] bias) {
166     bias[0] = Position.Bias.Forward;
167     int n = 0;
168     Container c = getContainer();
169     if (c instanceof JPasswordField JavaDoc) {
170         JPasswordField JavaDoc f = (JPasswordField JavaDoc) c;
171         if (! f.echoCharIsSet()) {
172         return super.viewToModel(fx, fy, a, bias);
173         }
174         char echoChar = f.getEchoChar();
175         FontMetrics m = f.getFontMetrics(f.getFont());
176         a = adjustAllocation(a);
177         Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
178                               a.getBounds();
179         n = ((int)fx - alloc.x) / m.charWidth(echoChar);
180         if (n < 0) {
181         n = 0;
182         }
183         else if (n > (getStartOffset() + getDocument().getLength())) {
184         n = getDocument().getLength() - getStartOffset();
185         }
186     }
187     return getStartOffset() + n;
188     }
189
190     /**
191      * Determines the preferred span for this view along an
192      * axis.
193      *
194      * @param axis may be either View.X_AXIS or View.Y_AXIS
195      * @return the span the view would like to be rendered into >= 0.
196      * Typically the view is told to render into the span
197      * that is returned, although there is no guarantee.
198      * The parent may choose to resize or break the view.
199      */

200     public float getPreferredSpan(int axis) {
201     switch (axis) {
202     case View.X_AXIS:
203             Container c = getContainer();
204             if (c instanceof JPasswordField JavaDoc) {
205                 JPasswordField JavaDoc f = (JPasswordField JavaDoc) c;
206                 if (f.echoCharIsSet()) {
207                     char echoChar = f.getEchoChar();
208                     FontMetrics m = f.getFontMetrics(f.getFont());
209                     Document JavaDoc doc = getDocument();
210                     return m.charWidth(echoChar) * getDocument().getLength();
211                 }
212             }
213         }
214         return super.getPreferredSpan(axis);
215     }
216
217     static char[] ONE = new char[1];
218 }
219
Popular Tags