GNU Classpath (0.17) | ||
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.Graphics; 43: 44: import javax.swing.JPasswordField; 45: 46: public class PasswordView extends FieldView 47: { 48: /** 49: * Buffer for putting the echo char into it and 50: * then using it to draw it into the view. 51: */ 52: private char[] oneCharBuffer = new char[1]; 53: 54: public PasswordView(Element elem) 55: { 56: super(elem); 57: } 58: 59: /** 60: * Draws one echo character at a given position. 61: * 62: * @param g the <code>Graphics</code> object to draw to 63: * @param x the x-position 64: * @param y the y-position 65: * @param ch the echo character 66: * 67: * @return the next x position right of the drawn character 68: */ 69: protected int drawEchoCharacter(Graphics g, int x, int y, char ch) 70: { 71: // Update font metrics. 72: updateMetrics(); 73: 74: // Draw character. 75: oneCharBuffer[0] = ch; 76: g.drawChars(oneCharBuffer, 0, 1, x, y); 77: 78: // Return new x position right of drawn character. 79: return x + metrics.charWidth(ch); 80: } 81: 82: private char getEchoChar() 83: { 84: char ch = ((JPasswordField) getContainer()).getEchoChar(); 85: 86: if (ch == 0) 87: ch = '*'; 88: 89: return ch; 90: } 91: 92: /** 93: * Draws selected text at a given position. 94: * 95: * @param g the <code>Graphics</code> object to draw to 96: * @param x the x-position 97: * @param y the y-position 98: * @param p0 the position of the first character to draw 99: * @param p1 the position of the first character not to draw 100: * 101: * @return the next x position right of the drawn character 102: */ 103: protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) 104: throws BadLocationException 105: { 106: // FIXME: Throw BadLocationException somehow. 107: 108: // Update font metrics. 109: updateMetrics(); 110: 111: // Get echo character. 112: char ch = getEchoChar(); 113: 114: // Set color for selected text. 115: g.setColor(selectedColor); 116: g.setColor(Color.BLACK); 117: 118: // Initialize buffer for faster drawing of all characters. 119: int len = p1 - p0; 120: char[] buffer = new char[len]; 121: for (int index = 0; index < len; ++index) 122: buffer[index] = ch; 123: 124: // Draw echo charaters. 125: g.drawChars(buffer, 0, len, x, y); 126: 127: // Return new x position right of all drawn characters. 128: return x + len * metrics.charWidth(ch); 129: } 130: 131: /** 132: * Draws unselected text at a given position. 133: * 134: * @param g the <code>Graphics</code> object to draw to 135: * @param x the x-position 136: * @param y the y-position 137: * @param p0 the position of the first character to draw 138: * @param p1 the position of the first character not to draw 139: * 140: * @return the next x position right of the drawn character 141: */ 142: protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) 143: throws BadLocationException 144: { 145: // FIXME: Throw BadLocationException somehow. 146: 147: // Update font metrics. 148: updateMetrics(); 149: 150: // Get echo character. 151: char ch = getEchoChar(); 152: 153: // Set color for unselected text. 154: g.setColor(unselectedColor); 155: g.setColor(Color.BLACK); 156: 157: // Initialize buffer for faster drawing of all characters. 158: int len = p1 - p0; 159: char[] buffer = new char[len]; 160: for (int index = 0; index < len; ++index) 161: buffer[index] = ch; 162: 163: // Draw echo charaters. 164: g.drawChars(buffer, 0, len, x, y); 165: 166: // Return new x position right of all drawn characters. 167: return x + len * metrics.charWidth(ch); 168: } 169: }
GNU Classpath (0.17) |