Frames | No Frames |
1: /* JPasswordField.java -- 2: Copyright (C) 2002, 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; 40: 41: import java.io.IOException; 42: import java.io.ObjectOutputStream; 43: 44: import javax.accessibility.AccessibleContext; 45: import javax.accessibility.AccessibleRole; 46: import javax.swing.text.BadLocationException; 47: import javax.swing.text.Document; 48: 49: /** 50: * class JPasswordField 51: * 52: * @author Andrew Selkirk 53: * @author Lillian Angel 54: * @version 1.0 55: */ 56: public class JPasswordField extends JTextField 57: { 58: /** 59: * AccessibleJPasswordField 60: */ 61: protected class AccessibleJPasswordField extends AccessibleJTextField 62: { 63: private static final long serialVersionUID = -8477039424200681086L; 64: 65: /** 66: * Constructor AccessibleJPasswordField 67: */ 68: protected AccessibleJPasswordField() 69: { 70: // Nothing to do here. 71: } 72: 73: /** 74: * getAccessibleRole 75: * 76: * @return AccessibleRole 77: */ 78: public AccessibleRole getAccessibleRole() 79: { 80: return AccessibleRole.PASSWORD_TEXT; 81: } 82: } 83: 84: /** 85: * echoChar. Default is 0. 86: */ 87: private char echoChar = 0; 88: 89: /** 90: * Creates a <code>JPasswordField</code> object. 91: */ 92: public JPasswordField() 93: { 94: this(null, null, 0); 95: } 96: 97: /** 98: * Creates a <code>JPasswordField</code> object. 99: * 100: * @param text the initial text 101: */ 102: public JPasswordField(String text) 103: { 104: this(null, text, 0); 105: } 106: 107: /** 108: * Creates a <code>JPasswordField</code> object. 109: * 110: * @param columns the number of columns 111: */ 112: public JPasswordField(int columns) 113: { 114: this(null, null, columns); 115: } 116: 117: /** 118: * Creates a <code>JPasswordField</code> object. 119: * 120: * @param text the initial text 121: * @param columns the number of columns 122: */ 123: public JPasswordField(String text, int columns) 124: { 125: this(null, text, columns); 126: } 127: 128: /** 129: * Creates a <code>JPasswordField</code> object. 130: * 131: * @param document the document to use 132: * @param text the initial text 133: * @param columns the number of columns 134: */ 135: public JPasswordField(Document document, String text, int columns) 136: { 137: super(document, text, columns); 138: } 139: 140: /** 141: * writeObject 142: * 143: * @param stream the stream to write to 144: * 145: * @exception IOException if an error occurs 146: */ 147: private void writeObject(ObjectOutputStream stream) throws IOException 148: { 149: // TODO: Implement me. 150: } 151: 152: /** 153: * Returns the <code>UIClassID</code> 154: * 155: * @return the string "PasswordFieldUI" 156: */ 157: public String getUIClassID() 158: { 159: return "PasswordFieldUI"; 160: } 161: 162: /** 163: * getEchoChar 164: * 165: * @return the echo char 166: */ 167: public char getEchoChar() 168: { 169: return echoChar; 170: } 171: 172: /** 173: * setEchoChar 174: * 175: * @param echo the echo char 176: */ 177: public void setEchoChar(char echo) 178: { 179: this.echoChar = echo; 180: } 181: 182: /** 183: * Returns true if this JPasswordField has a character set for echoing. 184: * A character is considered to be set if the echo character is not 0. 185: * 186: * @return <code>true</code> if the echo char is set, 187: * <code>false</code> otherwise. 188: */ 189: public boolean echoCharIsSet() 190: { 191: return echoChar != 0; 192: } 193: 194: /** 195: * Copies the selected text into the clipboard. This operation is not 196: * allowed in a password input field. 197: */ 198: public void copy() 199: { 200: UIManager.getLookAndFeel().provideErrorFeedback(this); 201: } 202: 203: /** 204: * Cuts the selected text and puts it into the clipboard. This operation 205: * is not allowed in a password input field. 206: */ 207: public void cut() 208: { 209: UIManager.getLookAndFeel().provideErrorFeedback(this); 210: } 211: 212: /** 213: * Returns the text contained in this TextComponent. If the 214: * underlying document is null, will give a NullPointerException. 215: * 216: * @return String 217: * 218: * @deprecated 219: */ 220: public String getText() 221: { 222: try 223: { 224: return getDocument().getText(0, getDocument().getLength()); 225: } 226: catch (BadLocationException ble) 227: { 228: // This should never happen. 229: throw new AssertionError(ble); 230: } 231: } 232: 233: /** 234: * Fetches a portion of the text represented by the component. 235: * Returns an empty string if length is 0. If the 236: * underlying document is null, will give a NullPointerException. 237: * 238: * @param offset TODO 239: * @param length TODO 240: * 241: * @return String 242: * 243: * @exception BadLocationException TODO 244: * 245: * @deprecated 246: */ 247: public String getText(int offset, int length) throws BadLocationException 248: { 249: return getDocument().getText(offset, length); 250: } 251: 252: /** 253: * Returns the text contained in this TextComponent. If the underlying 254: * document is null, will give a NullPointerException. 255: * For stronger security, it is recommended that the returned character 256: * array be cleared after use by setting each character to zero. 257: * 258: * @return char[] 259: */ 260: public char[] getPassword() 261: { 262: return getText().toCharArray(); 263: } 264: 265: /** 266: * Returns a string representation of this JPasswordField. This method is 267: * intended to be used only for debugging purposes, 268: * and the content and format of the returned string may vary between 269: * implementations. The returned string may be empty but may not be null. 270: * 271: * @return String 272: */ 273: protected String paramString() 274: { 275: try 276: { 277: return getText(); 278: } 279: catch (NullPointerException npe) 280: { 281: return ""; 282: } 283: } 284: 285: /** 286: * getAccessibleContext 287: * 288: * @return the <code>AccessibleContext</code> object 289: */ 290: public AccessibleContext getAccessibleContext() 291: { 292: if (accessibleContext == null) 293: accessibleContext = new AccessibleJPasswordField(); 294: 295: return accessibleContext; 296: } 297: }