001/* JPasswordField.java -- 002 Copyright (C) 2002, 2004 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package javax.swing; 040 041import java.io.IOException; 042import java.io.ObjectOutputStream; 043 044import javax.accessibility.AccessibleContext; 045import javax.accessibility.AccessibleRole; 046import javax.swing.text.BadLocationException; 047import javax.swing.text.Document; 048 049/** 050 * class JPasswordField 051 * 052 * @author Andrew Selkirk 053 * @author Lillian Angel 054 * @version 1.0 055 */ 056public class JPasswordField extends JTextField 057{ 058 /** 059 * AccessibleJPasswordField 060 */ 061 protected class AccessibleJPasswordField extends AccessibleJTextField 062 { 063 private static final long serialVersionUID = -8477039424200681086L; 064 065 /** 066 * Constructor AccessibleJPasswordField 067 */ 068 protected AccessibleJPasswordField() 069 { 070 // Nothing to do here. 071 } 072 073 /** 074 * getAccessibleRole 075 * 076 * @return AccessibleRole 077 */ 078 public AccessibleRole getAccessibleRole() 079 { 080 return AccessibleRole.PASSWORD_TEXT; 081 } 082 } 083 084 /** 085 * echoChar. Default is 0. 086 */ 087 private char echoChar = 0; 088 089 /** 090 * Creates a <code>JPasswordField</code> object. 091 */ 092 public JPasswordField() 093 { 094 this(null, null, 0); 095 } 096 097 /** 098 * Creates a <code>JPasswordField</code> object. 099 * 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}