Frames | No Frames |
1: /* PasswordView.java -- 2: Copyright (C) 2004 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: 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 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: 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 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: 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 34: 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 36: exception statement from your version. */ 37: 38: 39: package javax.swing.text; 40: 41: import java.awt.Color; 42: import java.awt.FontMetrics; 43: import java.awt.Graphics; 44: import java.awt.Shape; 45: 46: import javax.swing.JPasswordField; 47: 48: public class PasswordView 49: extends FieldView 50: { 51: /** 52: * Buffer for putting the echo char into it and 53: * then using it to draw it into the view. 54: */ 55: private char[] oneCharBuffer = new char[1]; 56: 57: public PasswordView(Element elem) 58: { 59: super(elem); 60: } 61: 62: /** 63: * Draws one echo character at a given position. 64: * 65: * @param g the <code>Graphics</code> object to draw to 66: * @param x the x-position 67: * @param y the y-position 68: * @param ch the echo character 69: * 70: * @return the next x position right of the drawn character 71: */ 72: protected int drawEchoCharacter(Graphics g, int x, int y, char ch) 73: { 74: // Update font metrics. 75: updateMetrics(); 76: 77: // Draw character. 78: oneCharBuffer[0] = ch; 79: g.drawChars(oneCharBuffer, 0, 1, x, y); 80: 81: // Return new x position right of drawn character. 82: return x + metrics.charWidth(ch); 83: } 84: 85: private char getEchoChar() 86: { 87: char ch = ((JPasswordField) getContainer()).getEchoChar(); 88: 89: if (ch == 0) 90: ch = '*'; 91: 92: return ch; 93: } 94: 95: /** 96: * Draws selected text at a given position. 97: * 98: * @param g the <code>Graphics</code> object to draw to 99: * @param x the x-position 100: * @param y the y-position 101: * @param p0 the position of the first character to draw 102: * @param p1 the position of the first character not to draw 103: * 104: * @return the next x position right of the drawn character 105: */ 106: protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) 107: throws BadLocationException 108: { 109: // FIXME: Throw BadLocationException somehow. 110: 111: // Update font metrics. 112: updateMetrics(); 113: 114: // Get echo character. 115: char ch = getEchoChar(); 116: 117: // Set color for selected text. 118: g.setColor(selectedColor); 119: g.setColor(Color.BLACK); 120: 121: // Initialize buffer for faster drawing of all characters. 122: int len = p1 - p0; 123: char[] buffer = new char[len]; 124: for (int index = 0; index < len; ++index) 125: buffer[index] = ch; 126: 127: // Draw echo charaters. 128: g.drawChars(buffer, 0, len, x, y); 129: 130: // Return new x position right of all drawn characters. 131: return x + len * metrics.charWidth(ch); 132: } 133: 134: /** 135: * Draws unselected text at a given position. 136: * 137: * @param g the <code>Graphics</code> object to draw to 138: * @param x the x-position 139: * @param y the y-position 140: * @param p0 the position of the first character to draw 141: * @param p1 the position of the first character not to draw 142: * 143: * @return the next x position right of the drawn character 144: */ 145: protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) 146: throws BadLocationException 147: { 148: // FIXME: Throw BadLocationException somehow. 149: 150: // Update font metrics. 151: updateMetrics(); 152: 153: // Get echo character. 154: char ch = getEchoChar(); 155: Segment segment = new Segment(); 156: 157: // Set color for unselected text. 158: g.setColor(unselectedColor); 159: g.setColor(Color.BLACK); 160: 161: // Initialize buffer for faster drawing of all characters. 162: p1--; 163: getDocument().getText(p0, p1 - p0, segment); 164: int len = segment.toString().length(); 165: 166: char[] buffer = new char[len]; 167: for (int index = 0; index < len; ++index) 168: buffer[index] = ch; 169: 170: y += getPreferredSpan(Y_AXIS)/2; 171: 172: // Draw echo charaters. 173: g.drawChars(buffer, 0, len, x, y); 174: 175: // Return new x position right of all drawn characters. 176: 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: }