KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > JPasswordField


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: JPasswordField.java,v $
11    Revision 1.15 2004/05/06 12:35:22 bobintetley
12    Parity with Swing constants for Binary Compatibility + fixes to JDesktopPane
13
14    Revision 1.14 2004/05/04 09:31:43 bobintetley
15    PlainDocument/View support and implementation. Build script supports java/javax
16    packages - fix to build script to use nested args in bootclasspath (single path broke on my Ant 1.6.1/Linux)
17
18    Revision 1.13 2004/04/30 13:20:43 bobintetley
19    Fix to unrealised peer preferred sizes, forwarding window events to
20    content panes and fix for mouse drag events.
21
22    Revision 1.12 2004/04/28 08:38:11 bobintetley
23    Hierarchy fixes, code cleanup for base classes, additional javadocs and use of flag to identify JComponent descendants with peers
24
25    Revision 1.11 2004/01/16 15:53:32 bobintetley
26    Many compatibility methods added to Container, Component, JInternalFrame,
27       UIManager, SwingUtilities, JTabbedPane, JPasswordField, JCheckBox
28       and JRadioButton.
29
30    Revision 1.10 2003/12/16 15:47:45 bobintetley
31    Thread safety added to common methods
32
33    Revision 1.9 2003/12/16 13:14:33 bobintetley
34    Use of SwingWTUtils.isSWTControlAvailable instead of null test
35
36    Revision 1.8 2003/12/15 18:29:57 bobintetley
37    Changed setParent() method to setSwingWTParent() to avoid conflicts with applications
38
39    Revision 1.7 2003/12/14 09:13:38 bobintetley
40    Added CVS log to source headers
41
42 */

43
44
45 package swingwtx.swing;
46
47 import swingwtx.swing.text.*;
48
49 import org.eclipse.swt.widgets.*;
50 import org.eclipse.swt.*;
51
52 public class JPasswordField extends swingwtx.swing.JTextField {
53
54     /** The peer */
55     protected Text ppeer = null;
56     /** Cache of the text to be loaded into the peer */
57     protected String JavaDoc pText = "";
58     /** The char to use instead of showing what's typed
59      * (defaults to a *)
60      */

61     protected char pEchoChar = '*';
62     
63     /** Return value for thread-safe property accessors */
64     private String JavaDoc retVal = "";
65     /** Return value for char array */
66     private char[] retChars = null;
67
68     /** Creates a new JPasswordField */
69     public JPasswordField() {
70         this(null, "", -1);
71     }
72     /** Creates a new JPasswordField
73      * @param text The text to put in the field
74      */

75     public JPasswordField(String JavaDoc text) {
76         this(null, text, 0);
77     }
78     /** Creates a new JPasswordField
79      * @param columns The number of chars allowed
80      */

81     public JPasswordField(int columns) {
82         this(null, "", columns);
83     }
84     /** Creates a new JPasswordField
85      * @param text The text for the field
86      * @param columns The number of chars allowed
87      */

88     public JPasswordField(String JavaDoc text, int columns) {
89         this(null, text, columns);
90     }
91     /** Creates a new JPasswordField
92      * @param document The Document model to supply data to the component
93      * @param text The text to put in the field
94      * @param columns The number of chars allowed
95      */

96     public JPasswordField(Document document, String JavaDoc text, int columns) {
97         super();
98         if (document != null) setDocument(document);
99         if (text != null) { setText(text); view.updateModelFromComponent(getText()); }
100     }
101     
102     /** @return The char used to output instead of the typed character */
103     public char getEchoChar() { if (!SwingWTUtils.isSWTControlAvailable(ppeer)) return pEchoChar; else return ppeer.getEchoChar(); }
104     /** @param c The char to output instead of what's typed */
105     public void setEchoChar(char c) { if (!SwingWTUtils.isSWTControlAvailable(ppeer)) pEchoChar = c; else ppeer.setEchoChar(c); }
106     /** @return true if the echo char is set. This class defaults * as the echo char, so it's always set */
107     public boolean isEchoCharSet() { return true; }
108     /** @return the text of the field */
109     public String JavaDoc getText() {
110         retVal = "";
111         SwingUtilities.invokeSync(new Runnable JavaDoc() {
112             public void run() {
113                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
114                     retVal = ppeer.getText();
115                 else retVal = pText;
116             }
117         });
118         return retVal;
119     }
120     
121     /** @return The text of the field as a char array */
122     public char[] getPassword() {
123         retChars = null;
124         SwingUtilities.invokeSync(new Runnable JavaDoc() {
125             public void run() {
126                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
127                     retChars = ppeer.getText().toCharArray();
128                 else retChars = pText.toCharArray();
129             }
130         });
131         return retChars;
132     }
133     
134     /** @param text The new text for the field */
135     public void setText(final String JavaDoc text) {
136         pText = text;
137         SwingUtilities.invokeSync(new Runnable JavaDoc() {
138             public void run() {
139                 if (SwingWTUtils.isSWTControlAvailable(ppeer))
140                     ppeer.setText(text);}
141         });
142     }
143     
144     /** Overriden to calculate non-realised
145      * preferred size.
146      */

147     protected swingwt.awt.Dimension calculatePreferredSize() {
148         swingwt.awt.Dimension size = new swingwt.awt.Dimension(
149             SwingWTUtils.getRenderStringWidth(pText),
150             SwingWTUtils.getRenderStringHeight(pText) + 4);
151         setSize(size);
152         return size;
153     }
154     
155     /**
156      * Once a parent component receives an "add" call for a child, this being
157      * the child, this should be called to tell us to instantiate the peer
158      * and load in any cached properties.
159      */

160     public void setSwingWTParent(swingwt.awt.Container parent) throws Exception JavaDoc {
161         descendantHasPeer = true;
162         ppeer = new Text(parent.getComposite(), SWT.BORDER | SWT.SINGLE);
163         ppeer.setEchoChar(pEchoChar);
164         ppeer.setText(pText);
165         peer = ppeer;
166         this.parent = parent;
167     }
168     
169 }
170
Popular Tags