KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > password > PasswordValueEditor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.valueeditors.password;
22
23
24 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog;
25 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
26 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
27 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
28 import org.apache.directory.ldapstudio.browser.core.model.IValue;
29 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogBinaryValueEditor;
30 import org.eclipse.swt.widgets.Shell;
31
32
33 /**
34  * Implementation of IValueEditor for attribute userPassword.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev$, $Date$
38  */

39 public class PasswordValueEditor extends AbstractDialogBinaryValueEditor
40 {
41
42     /**
43      * {@inheritDoc}
44      *
45      * This implementation opens the PasswordDialog.
46      */

47     protected boolean openDialog( Shell shell )
48     {
49         Object JavaDoc value = getValue();
50         if ( value != null && value instanceof PasswordValueEditorRawValueWrapper )
51         {
52             PasswordValueEditorRawValueWrapper wrapper = ( PasswordValueEditorRawValueWrapper ) value;
53             if ( wrapper.password != null && wrapper.password instanceof byte[] )
54             {
55                 byte[] pw = ( byte[] ) wrapper.password;
56                 PasswordDialog dialog = new PasswordDialog( shell, pw, wrapper.entry );
57                 if ( dialog.open() == TextDialog.OK )
58                 {
59                     setValue( dialog.getNewPassword() );
60                     return true;
61                 }
62             }
63         }
64         return false;
65     }
66
67
68     /**
69      * {@inheritDoc}
70      *
71      * This implementation returns information about the
72      * used hash algorithm. The value stored in directory
73      * is only display when the showRawValues option is
74      * active.
75      */

76     public String JavaDoc getDisplayValue( IValue value )
77     {
78         if ( showRawValues() )
79         {
80             return getPrintableString( value );
81         }
82         else
83         {
84             if ( value == null )
85             {
86                 return "NULL";
87             }
88
89             String JavaDoc password = value.getStringValue();;
90             if ( password == null )
91             {
92                 return "NULL";
93             }
94             else
95             {
96                 String JavaDoc text;
97                 if ( "".equals( password ) )
98                 {
99                     text = "Empty password";
100                 }
101                 else if ( password.indexOf( '{' ) == 0 && password.indexOf( '}' ) > 0 )
102                 {
103                     String JavaDoc encryptionMethod = password.substring( password.indexOf( '{' ) + 1, password.indexOf( '}' ) );
104                     text = encryptionMethod + " encrypted password";
105                 }
106                 else
107                 {
108                     text = "Plain text password";
109                 }
110                 return text;
111             }
112         }
113     }
114
115
116     /**
117      * {@inheritDoc}
118      *
119      * Returns a PasswordValueEditorRawValueWrapper with empty
120      * password.
121      */

122     protected Object JavaDoc getEmptyRawValue( IAttribute attribute )
123     {
124         return new PasswordValueEditorRawValueWrapper( new byte[0], attribute.getEntry() );
125     }
126
127
128     /**
129      * {@inheritDoc}
130      *
131      * Returns a PasswordValueEditorRawValueWrapper.
132      */

133     public Object JavaDoc getRawValue( IValue value )
134     {
135         Object JavaDoc password = super.getRawValue( value );
136         return new PasswordValueEditorRawValueWrapper( password, value.getAttribute().getEntry() );
137     }
138
139
140     /**
141      * {@inheritDoc}
142      *
143      * Returns a PasswordValueEditorRawValueWrapper with
144      * null entry.
145      */

146     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
147     {
148         Object JavaDoc password = super.getRawValue( connection, value );
149         return new PasswordValueEditorRawValueWrapper( password, null );
150     }
151
152     /**
153      * The PasswordValueEditorRawValueWrapper is used to pass contextual
154      * information to the opened PasswordDialog.
155      *
156      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
157      * @version $Rev$, $Date$
158      */

159     private class PasswordValueEditorRawValueWrapper
160     {
161         /** The password, used as initial value in PasswordDialog */
162         private Object JavaDoc password;
163
164         /** The entry, used for the bind operation in PasswordDialog */
165         private IEntry entry;
166
167
168         /**
169          * Creates a new instance of PasswordValueEditorRawValueWrapper.
170          *
171          * @param password the password
172          * @param entry the entry
173          */

174         private PasswordValueEditorRawValueWrapper( Object JavaDoc password, IEntry entry )
175         {
176             this.password = password;
177             this.entry = entry;
178         }
179     }
180
181 }
182
Popular Tags