KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > websphere6 > ui > nodes > editors > WSPasswordEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.websphere6.ui.nodes.editors;
20
21 import java.beans.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.border.*;
26
27 import org.openide.explorer.propertysheet.editors.*;
28 import org.openide.explorer.propertysheet.*;
29
30 import org.netbeans.modules.j2ee.websphere6.util.*;
31
32 /**
33  * An editor for the password field that appears on the properties sheet for
34  * the instance node.
35  *
36  * @author Kirill Sorokin
37  */

38 public class WSPasswordEditor extends PropertyEditorSupport
39         implements ExPropertyEditor {
40     
41     /**
42      * The internal store for the edited object, for this editor we assume
43      * that it's a string
44      */

45     private String JavaDoc value = "";
46     
47     /**
48      * Returns the edited object as a string.
49      *
50      * @return the string representation of the edited object
51      */

52     public String JavaDoc getAsText() {
53         if (WSDebug.isEnabled()) // debug output
54
WSDebug.notify(getClass(), "getAsText()"); // NOI18N
55

56         // return the object masquerading its real value with asterisks
57
return value.replaceAll(".", "*"); // NOI18N
58
}
59     
60     /**
61      * Sets the edited object value by supplying a string representation of
62      * the new value
63      *
64      * @param string the string representation of the new value
65      */

66     public void setAsText(String JavaDoc string) throws IllegalArgumentException JavaDoc {
67         if (WSDebug.isEnabled()) // debug output
68
WSDebug.notify(getClass(), "setAsText(" + string + ")"); // NOI18N
69

70         // if the supplied string is not null 0 update the value and notify the
71
// listeners
72
if (string != null) {
73             value = string;
74             firePropertyChange();
75         }
76     }
77     
78     /**
79      * Implementation of ExPropertyEditor interface
80      * Working is not checked
81      */

82     private PropertyEnv myPropertyEnv = null;
83     
84     public void attachEnv(PropertyEnv env) {
85         myPropertyEnv = env;
86     }
87     
88     /**
89      * Sets the edited object value
90      *
91      * @param object the new value
92      */

93     public void setValue(Object JavaDoc object) {
94         if (WSDebug.isEnabled()) // debug output
95
WSDebug.notify(getClass(), "setValue(" + object + ")"); // NOI18N
96

97         // if the supplied object is not null - update the value
98
if (object != null) {
99             value = object.toString();
100         }
101     }
102     
103     /**
104      * Returns the edited object's value
105      *
106      * @return the edited object's value
107      */

108     public Object JavaDoc getValue() {
109         if (WSDebug.isEnabled()) // debug output
110
WSDebug.notify(getClass(), "getValue()"); // NOI18N
111

112         // return
113
return value;
114     }
115     
116     /**
117      * Returns the custom in-line editor for the object. In this case it's
118      * a password field
119      *
120      * @return a swing component for editing the object
121      */

122     public Component getInPlaceCustomEditor() {
123         if (WSDebug.isEnabled()) // debug output
124
WSDebug.notify(getClass(), "getInPlaceCustomEditor()"); // NOI18N
125

126         // init the password field
127
JPasswordField textfield = new JPasswordField(value);
128         
129         // set its looks
130
textfield.setEchoChar('*');
131         textfield.setBorder(new EmptyBorder(0, 0, 0, 0));
132         textfield.setMargin(new Insets(0, 0, 0, 0));
133         
134         // select the component's text
135
textfield.selectAll();
136         
137         // add a key listener
138
textfield.addKeyListener(new PasswordListener());
139         
140         // return the component
141
return textfield;
142     }
143     
144     /**
145      * Tells whether this editor support custom in-line editing
146      *
147      * @return true
148      */

149     public boolean hasInPlaceCustomEditor() {
150         if (WSDebug.isEnabled()) // debug output
151
WSDebug.notify(getClass(), "hasInPlaceCustomEditor()"); // NOI18N
152

153         // return
154
return true;
155     }
156     
157     /**
158      * This method is not clear - but apparently there are no tagged values in
159      * a password, so we return false
160      *
161      * @return false
162      */

163     public boolean supportsEditingTaggedValues() {
164         if (WSDebug.isEnabled()) // debug output
165
WSDebug.notify(getClass(),
166                     "supportsEditingTaggedValues()"); // NOI18N
167

168         // return
169
return false;
170     }
171     
172     /**
173      * This a listener that is attached to the editor field and watches
174      * keystrokes appending the input characters to the value
175      *
176      * @author Kirill Sorokin
177      */

178     private class PasswordListener extends KeyAdapter {
179         /**
180          * Triggered when a keyboard button is released
181          *
182          * @param event the corresponding keyboard event
183          */

184         public void keyReleased(KeyEvent event) {
185             if (WSDebug.isEnabled()) // debug output
186
WSDebug.notify(getClass(), "keyReleased(" + event + // NOI18N
187
")"); // NOI18N
188

189             // get the event's source (in out case it would be a password field
190
JPasswordField field = (JPasswordField) event.getSource();
191             
192             // update the edited object with the newly entered value
193
value = new String JavaDoc(field.getPassword());
194             
195             // notify the listeners
196
firePropertyChange();
197             
198             // if the enter key was pressed simulate the pressing of the
199
// escape key so that the editor closes
200
if(event.getKeyCode() == KeyEvent.VK_ENTER){
201                 KeyEvent escapeEvent = new KeyEvent(event.getComponent(),
202                         KeyEvent.KEY_PRESSED, 0, 0, KeyEvent.VK_ESCAPE,
203                         KeyEvent.CHAR_UNDEFINED);
204                 KeyboardFocusManager.getCurrentKeyboardFocusManager().
205                         dispatchKeyEvent(escapeEvent);
206             }
207         }
208     }
209     
210 }
Popular Tags