1 7 package javax.swing.text; 8 9 import com.sun.java.swing.SwingUtilities2; 10 import java.awt.*; 11 import javax.swing.JPasswordField ; 12 13 24 public class PasswordView extends FieldView { 25 26 31 public PasswordView(Element elem) { 32 super(elem); 33 } 34 35 48 protected int drawUnselectedText(Graphics g, int x, int y, 49 int p0, int p1) throws BadLocationException { 50 51 Container c = getContainer(); 52 if (c instanceof JPasswordField ) { 53 JPasswordField f = (JPasswordField ) 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 87 protected int drawSelectedText(Graphics g, int x, 88 int y, int p0, int p1) throws BadLocationException { 89 g.setColor(selected); 90 Container c = getContainer(); 91 if (c instanceof JPasswordField ) { 92 JPasswordField f = (JPasswordField ) 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 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 135 public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException { 136 Container c = getContainer(); 137 if (c instanceof JPasswordField ) { 138 JPasswordField f = (JPasswordField ) 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 165 public int viewToModel(float fx, float fy, Shape a, Position.Bias [] bias) { 166 bias[0] = Position.Bias.Forward; 167 int n = 0; 168 Container c = getContainer(); 169 if (c instanceof JPasswordField ) { 170 JPasswordField f = (JPasswordField ) 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 200 public float getPreferredSpan(int axis) { 201 switch (axis) { 202 case View.X_AXIS: 203 Container c = getContainer(); 204 if (c instanceof JPasswordField ) { 205 JPasswordField f = (JPasswordField ) c; 206 if (f.echoCharIsSet()) { 207 char echoChar = f.getEchoChar(); 208 FontMetrics m = f.getFontMetrics(f.getFont()); 209 Document 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 |