Source for javax.swing.JPasswordField

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