KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > valueeditors > dn > DnValueEditor


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.dn;
22
23
24 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog;
25 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy;
26 import org.apache.directory.ldapstudio.browser.core.model.DN;
27 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
28 import org.apache.directory.ldapstudio.browser.core.model.IValue;
29 import org.apache.directory.ldapstudio.browser.core.model.NameException;
30 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogStringValueEditor;
31 import org.eclipse.swt.widgets.Shell;
32
33
34 /**
35  * Implementation of IValueEditor for syntax 1.3.6.1.4.1.1466.115.121.1.12
36  * (Distinguished Name).
37  *
38  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
39  * @version $Rev$, $Date$
40  */

41 public class DnValueEditor extends AbstractDialogStringValueEditor
42 {
43
44     /**
45      * {@inheritDoc}
46      *
47      * This implementation opens the DnDialog.
48      */

49     protected boolean openDialog( Shell shell )
50     {
51         Object JavaDoc value = getValue();
52         if ( value != null && value instanceof DnValueEditorRawValueWrapper )
53         {
54             DnValueEditorRawValueWrapper wrapper = ( DnValueEditorRawValueWrapper ) value;
55             DN dn;
56             try
57             {
58                 dn = new DN( wrapper.dn );
59             }
60             catch ( NameException e )
61             {
62                 dn = null;
63             }
64             DnDialog dialog = new DnDialog( shell, wrapper.connection, dn );
65             if ( dialog.open() == TextDialog.OK && dialog.getDn() != null )
66             {
67                 setValue( dialog.getDn().toString() );
68                 return true;
69             }
70         }
71         return false;
72     }
73
74
75     /**
76      * {@inheritDoc}
77      *
78      * Returns a DnValueEditorRawValueWrapper with the connection of
79      * the attribute hierarchy and a null DN if there are no values
80      * in attributeHierarchy.
81      *
82      * Returns a DnValueEditorRawValueWrapper with the connection of
83      * the attribute hierarchy and a DN if there is one value
84      * in attributeHierarchy.
85      */

86     public Object JavaDoc getRawValue( AttributeHierarchy attributeHierarchy )
87     {
88         if ( attributeHierarchy == null )
89         {
90             return null;
91         }
92         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 )
93         {
94             IConnection connection = attributeHierarchy.getAttribute().getEntry().getConnection();
95             return new DnValueEditorRawValueWrapper( connection, null );
96         }
97         else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 )
98         {
99             IConnection connection = attributeHierarchy.getAttribute().getEntry().getConnection();
100             return new DnValueEditorRawValueWrapper( connection, getDisplayValue( attributeHierarchy ) );
101         }
102         else
103         {
104             return null;
105         }
106     }
107
108
109     /**
110      * {@inheritDoc}
111      *
112      * Returns a DnValueEditorRawValueWrapper with the connection of
113      * the value and a DN build from the given value.
114      */

115     public Object JavaDoc getRawValue( IValue value )
116     {
117         Object JavaDoc o = super.getRawValue( value );
118         if ( o != null && o instanceof String JavaDoc )
119         {
120             IConnection connection = value.getAttribute().getEntry().getConnection();
121             return new DnValueEditorRawValueWrapper( connection, ( String JavaDoc ) o );
122         }
123
124         return null;
125     }
126
127
128     /**
129      * {@inheritDoc}
130      *
131      * Returns a DnValueEditorRawValueWrapper with the given
132      * connection and a DN build from the given value.
133      */

134     public Object JavaDoc getRawValue( IConnection connection, Object JavaDoc value )
135     {
136         Object JavaDoc o = super.getRawValue( connection, value );
137         if ( o != null && o instanceof String JavaDoc )
138         {
139             return new DnValueEditorRawValueWrapper( connection, ( String JavaDoc ) o );
140         }
141
142         return null;
143     }
144
145     /**
146      * The DnValueEditorRawValueWrapper is used to pass contextual
147      * information to the opened DnDialog.
148      *
149      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
150      * @version $Rev$, $Date$
151      */

152     private class DnValueEditorRawValueWrapper
153     {
154         /** The connection, used in DnDialog to browse for an entry */
155         private IConnection connection;
156
157         /** The DN, used as initial value in DnDialog */
158         private String JavaDoc dn;
159
160
161         /**
162          * Creates a new instance of DnValueEditorRawValueWrapper.
163          *
164          * @param connection the connection
165          * @param dn the DN
166          */

167         private DnValueEditorRawValueWrapper( IConnection connection, String JavaDoc dn )
168         {
169             this.connection = connection;
170             this.dn = dn;
171         }
172
173
174         /**
175          * {@inheritDoc}
176          */

177         public String JavaDoc toString()
178         {
179             return dn == null ? "" : dn;
180         }
181
182     }
183
184 }
185
Popular Tags