1. /*
  2. * @(#)JPasswordField.java 1.32 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import javax.swing.text.*;
  9. import javax.swing.plaf.*;
  10. import javax.accessibility.*;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. /**
  15. * JPasswordField is a lightweight component that allows the editing
  16. * of a single line of text where the view indicates something was
  17. * typed, but does not show the original characters. It is intended
  18. * to be source-compatible with java.awt.TextField used with echoChar
  19. * set. It is provided seperately to make it easier to safely change
  20. * the ui for the JTextField without affecting password entries.
  21. * <p>
  22. * For the keyboard keys used by this component in the standard Look and
  23. * Feel (L&F) renditions, see the
  24. * <a href="doc-files/Key-Index.html#JPasswordField">JPasswordField</a>
  25. * key assignments.
  26. * <p>
  27. * <strong>Warning:</strong>
  28. * Serialized objects of this class will not be compatible with
  29. * future Swing releases. The current serialization support is appropriate
  30. * for short term storage or RMI between applications running the same
  31. * version of Swing. A future release of Swing will provide support for
  32. * long term persistence.
  33. *
  34. * @beaninfo
  35. * attribute: isContainer false
  36. *
  37. * @author Timothy Prinzing
  38. * @version 1.32, 11/29/01
  39. */
  40. public class JPasswordField extends JTextField {
  41. /**
  42. * Constructs a new JPasswordField, with a default document, null starting
  43. * text string, and 0 column width.
  44. */
  45. public JPasswordField() {
  46. this(null,null,0);
  47. }
  48. /**
  49. * Constructs a new JPasswordField initialized with the specified text.
  50. * The document model is set to the default, and the number of columns to 0.
  51. *
  52. * @param text the text to be displayed, null if none
  53. */
  54. public JPasswordField(String text) {
  55. this(null, text, 0);
  56. }
  57. /**
  58. * Constructs a new empty JPasswordField with the specified
  59. * number of columns. A default model is created, and the initial string
  60. * is set to null.
  61. *
  62. * @param columns the number of columns >= 0
  63. */
  64. public JPasswordField(int columns) {
  65. this(null, null, columns);
  66. }
  67. /**
  68. * Constructs a new JPasswordField initialized with the specified text
  69. * and columns. The document model is set to the default.
  70. *
  71. * @param text the text to be displayed, null if none
  72. * @param columns the number of columns >= 0
  73. */
  74. public JPasswordField(String text, int columns) {
  75. this(null, text, columns);
  76. }
  77. /**
  78. * Constructs a new JPasswordField that uses the given text storage
  79. * model and the given number of columns. This is the constructor
  80. * through which the other constructors feed. The echo character is
  81. * set to '*'. If the document model is null, a default one will be
  82. * created.
  83. *
  84. * @param doc the text storage to use
  85. * @param txt the text to be displayed, null if none
  86. * @param columns the number of columns to use to calculate
  87. * the preferred width >= 0. If columns is set to zero, the
  88. * preferred width will be whatever naturally results from
  89. * the component implementation.
  90. */
  91. public JPasswordField(Document doc, String txt, int columns) {
  92. super(doc, txt, columns);
  93. echoChar = '*';
  94. }
  95. /**
  96. * Returns the name of the L&F class that renders this component.
  97. *
  98. * @return "PasswordFieldUI"
  99. * @see JComponent#getUIClassID
  100. * @see UIDefaults#getUI
  101. */
  102. public String getUIClassID() {
  103. return uiClassID;
  104. }
  105. /**
  106. * Returns the character to be used for echoing. The default is '*'.
  107. *
  108. * @return the echo character, 0 if unset
  109. * @see #setEchoChar
  110. * @see #echoCharIsSet
  111. */
  112. public char getEchoChar() {
  113. return echoChar;
  114. }
  115. /**
  116. * Sets the echo character for this JPasswordField. Note
  117. * that this is largely a suggestion to the view as the
  118. * view that gets installed can use whatever graphic techniques
  119. * it desires to represent the field. Setting a value of 0 unsets
  120. * the echo character.
  121. *
  122. * @param c the echo character to display
  123. * @see #echoCharIsSet
  124. * @see #getEchoChar
  125. * @beaninfo
  126. * description: character to display in place of the real characters
  127. */
  128. public void setEchoChar(char c) {
  129. echoChar = c;
  130. }
  131. /**
  132. * Returns true if this JPasswordField has a character set for
  133. * echoing. A character is considered to be set if the echo character
  134. * is not 0.
  135. *
  136. * @return true if a character is set for echoing
  137. * @see #setEchoChar
  138. * @see #getEchoChar
  139. */
  140. public boolean echoCharIsSet() {
  141. return echoChar != 0;
  142. }
  143. // --- JTextComponent methods ----------------------------------
  144. /**
  145. * Normally transfers the currently selected range in the associated
  146. * text model to the system clipboard, removing the contents
  147. * from the model. This is not a good thing for a password field
  148. * and is reimplemented to simply beep.
  149. */
  150. public void cut() {
  151. getToolkit().beep();
  152. }
  153. /**
  154. * Normally transfers the currently selected range in the associated
  155. * text model to the system clipboard, leaving the contents
  156. * in the text model. This is not a good thing for a password field
  157. * and is reimplemented to simply beep.
  158. */
  159. public void copy() {
  160. getToolkit().beep();
  161. }
  162. /**
  163. * Returns the text contained in this TextComponent. If the underlying
  164. * document is null, will give a NullPointerException.
  165. * <p>
  166. * For security reasons, this method is deprecated. Use the
  167. * getPassword method instead.
  168. * @deprecated As of JDK version 1.2,
  169. * replaced by <code>getPassword()</code>.
  170. * @return the text
  171. */
  172. public String getText() {
  173. return super.getText();
  174. }
  175. /**
  176. * Fetches a portion of the text represented by the
  177. * component. Returns an empty string if length is 0.
  178. * <p>
  179. * For security reasons, this method is deprecated. Use the
  180. * getPassword method instead.
  181. * @deprecated As of JDK version 1.2,
  182. * replaced by <code>getPassword()</code>.
  183. * @param offs the offset >= 0
  184. * @param len the length >= 0
  185. * @return the text
  186. * @exception BadLocationException if the offset or length are invalid
  187. */
  188. public String getText(int offs, int len) throws BadLocationException {
  189. return super.getText(offs, len);
  190. }
  191. /**
  192. * Returns the text contained in this TextComponent. If the underlying
  193. * document is null, will give a NullPointerException. For stronger
  194. * security, it is recommended that the returned character array be
  195. * cleared after use by setting each character to zero.
  196. *
  197. * @return the text
  198. */
  199. public char[] getPassword() {
  200. Document doc = getDocument();
  201. Segment txt = new Segment();
  202. try {
  203. doc.getText(0, doc.getLength(), txt); // use the non-String API
  204. } catch (BadLocationException e) {
  205. return null;
  206. }
  207. char[] retValue = new char[txt.count];
  208. System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
  209. return retValue;
  210. }
  211. /**
  212. * See readObject() and writeObject() in JComponent for more
  213. * information about serialization in Swing.
  214. */
  215. private void writeObject(ObjectOutputStream s) throws IOException {
  216. s.defaultWriteObject();
  217. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  218. ui.installUI(this);
  219. }
  220. }
  221. // --- variables -----------------------------------------------
  222. /**
  223. * @see #getUIClassID
  224. * @see #readObject
  225. */
  226. private static final String uiClassID = "PasswordFieldUI";
  227. private char echoChar;
  228. /**
  229. * Returns a string representation of this JPasswordField. This method
  230. * is intended to be used only for debugging purposes, and the
  231. * content and format of the returned string may vary between
  232. * implementations. The returned string may be empty but may not
  233. * be <code>null</code>.
  234. *
  235. * @return a string representation of this JPasswordField.
  236. */
  237. protected String paramString() {
  238. return super.paramString() +
  239. ",echoChar=" + echoChar;
  240. }
  241. /////////////////
  242. // Accessibility support
  243. ////////////////
  244. /**
  245. * Gets the AccessibleContext associated with this JPasswordField.
  246. * A new context is created as necessary.
  247. *
  248. * @return the AccessibleContext of this JPasswordField
  249. */
  250. public AccessibleContext getAccessibleContext() {
  251. if (accessibleContext == null) {
  252. accessibleContext = new AccessibleJPasswordField();
  253. }
  254. return accessibleContext;
  255. }
  256. /**
  257. * The class used to obtain the accessible role for this object.
  258. * <p>
  259. * <strong>Warning:</strong>
  260. * Serialized objects of this class will not be compatible with
  261. * future Swing releases. The current serialization support is appropriate
  262. * for short term storage or RMI between applications running the same
  263. * version of Swing. A future release of Swing will provide support for
  264. * long term persistence.
  265. */
  266. protected class AccessibleJPasswordField extends AccessibleJTextField {
  267. /**
  268. * Gets the role of this object.
  269. *
  270. * @return an instance of AccessibleRole describing the role of the
  271. * object (AccessibleRole.PASSWORD_TEXT)
  272. * @see AccessibleRole
  273. */
  274. public AccessibleRole getAccessibleRole() {
  275. return AccessibleRole.PASSWORD_TEXT;
  276. }
  277. }
  278. }