KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > wizards > NewEntryDnWizardPage


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.browser.ui.wizards;
22
23
24 import java.util.Arrays JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
27 import org.apache.directory.ldapstudio.browser.common.widgets.DnBuilderWidget;
28 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent;
29 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
32 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
34 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob;
35 import org.apache.directory.ldapstudio.browser.core.model.DN;
36 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
37 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
38 import org.apache.directory.ldapstudio.browser.core.model.IValue;
39 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException;
40 import org.apache.directory.ldapstudio.browser.core.model.RDN;
41 import org.apache.directory.ldapstudio.browser.core.model.RDNPart;
42 import org.apache.directory.ldapstudio.browser.core.model.schema.Subschema;
43 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
44 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
45 import org.eclipse.jface.dialogs.MessageDialog;
46 import org.eclipse.jface.wizard.IWizardPage;
47 import org.eclipse.jface.wizard.WizardPage;
48 import org.eclipse.swt.widgets.Composite;
49
50
51 /**
52  * The NewEntryDnWizardPage is used to compose the new entry's
53  * distinguished name.
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public class NewEntryDnWizardPage extends WizardPage implements WidgetModifyListener
59 {
60
61     /** The wizard. */
62     private NewEntryWizard wizard;
63
64     /** The DN builder widget. */
65     private DnBuilderWidget dnBuilderWidget;
66
67
68     /**
69      * Creates a new instance of NewEntryDnWizardPage.
70      *
71      * @param pageName the page name
72      * @param wizard the wizard
73      */

74     public NewEntryDnWizardPage( String JavaDoc pageName, NewEntryWizard wizard )
75     {
76         super( pageName );
77         setTitle( "Distinguished Name" );
78         setDescription( "Please select the parent of the new entry and enter the RDN." );
79         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_ENTRY_WIZARD ) );
80         setPageComplete( false );
81
82         this.wizard = wizard;
83     }
84
85
86     /**
87      * {@inheritDoc}
88      */

89     public void dispose()
90     {
91         if ( dnBuilderWidget != null )
92         {
93             dnBuilderWidget.removeWidgetModifyListener( this );
94             dnBuilderWidget.dispose();
95             dnBuilderWidget = null;
96         }
97         super.dispose();
98     }
99
100
101     /**
102      * Validates the input fields.
103      */

104     private void validate()
105     {
106         if ( dnBuilderWidget.getRdn() != null && dnBuilderWidget.getParentDn() != null )
107         {
108             setPageComplete( true );
109             saveState();
110         }
111         else
112         {
113             setPageComplete( false );
114         }
115     }
116
117
118     /**
119      * Initializes the DN builder widget with the DN of
120      * the prototype entry. Called when this page becomes visible.
121      */

122     private void loadState()
123     {
124         DummyEntry newEntry = wizard.getPrototypeEntry();
125
126         Subschema subschema = newEntry.getSubschema();
127         String JavaDoc[] attributeNames = subschema.getAllAttributeNames();
128
129         DN parentDn = null;
130         if ( newEntry.getDn().getParentDn() != null )
131         {
132             parentDn = newEntry.getDn().getParentDn();
133         }
134         else if ( wizard.getSelectedEntry() != null )
135         {
136             parentDn = wizard.getSelectedEntry().getDn();
137         }
138
139         RDN rdn = newEntry.getRdn();
140
141         dnBuilderWidget.setInput( wizard.getSelectedConnection(), attributeNames, rdn, parentDn );
142     }
143
144
145     /**
146      * Saves the DN of the DN builder widget to the prototype entry.
147      */

148     private void saveState()
149     {
150         DummyEntry newEntry = wizard.getPrototypeEntry();
151
152         try
153         {
154             EventRegistry.suspendEventFireingInCurrentThread();
155
156             // remove old RDN
157
RDNPart[] oldRdnParts = newEntry.getRdn().getParts();
158             for ( int i = 0; i < oldRdnParts.length; i++ )
159             {
160                 IAttribute attribute = newEntry.getAttribute( oldRdnParts[i].getType() );
161                 if ( attribute != null )
162                 {
163                     IValue[] values = attribute.getValues();
164                     for ( int v = 0; v < values.length; v++ )
165                     {
166                         if ( values[v].getStringValue().equals( oldRdnParts[i].getUnencodedValue() ) )
167                         {
168                             attribute.deleteValue( values[v] );
169                         }
170                     }
171                 }
172             }
173
174             // set new DN
175
DN dn = new DN( dnBuilderWidget.getRdn(), dnBuilderWidget.getParentDn() );
176             newEntry.setDn( dn );
177
178             // add new RDN
179
RDNPart[] newRdnParts = dn.getRdn().getParts();
180             for ( int i = 0; i < newRdnParts.length; i++ )
181             {
182                 IAttribute rdnAttribute = newEntry.getAttribute( newRdnParts[i].getType() );
183                 if ( rdnAttribute == null )
184                 {
185                     rdnAttribute = new Attribute( newEntry, newRdnParts[i].getType() );
186                     newEntry.addAttribute( rdnAttribute );
187                 }
188                 String JavaDoc rdnValue = newRdnParts[i].getUnencodedValue();
189                 String JavaDoc[] stringValues = rdnAttribute.getStringValues();
190                 if ( !Arrays.asList( stringValues ).contains( rdnValue ) )
191                 {
192                     rdnAttribute.addValue( new Value( rdnAttribute, rdnValue ) );
193                 }
194             }
195
196         }
197         catch ( ModelModificationException e )
198         {
199             e.printStackTrace();
200         }
201         finally
202         {
203             EventRegistry.resumeEventFireingInCurrentThread();
204         }
205     }
206
207
208     /**
209      * {@inheritDoc}
210      *
211      * This implementation initializes DN builder widghet with the
212      * DN of the protoype entry.
213      */

214     public void setVisible( boolean visible )
215     {
216         super.setVisible( visible );
217
218         if ( visible )
219         {
220             loadState();
221             validate();
222         }
223     }
224
225
226     /**
227      * {@inheritDoc}
228      *
229      * This implementation just checks if this page is complete. It
230      * doesn't call {@link #getNextPage()} to avoid unneeded
231      * invokings of {@link ReadEntryJob}s.
232      */

233     public boolean canFlipToNextPage()
234     {
235         return isPageComplete();
236     }
237
238
239     /**
240      * {@inheritDoc}
241      *
242      * This implementation invokes a {@link ReadEntryJob} to check if an
243      * entry with the composed DN already exists.
244      */

245     public IWizardPage getNextPage()
246     {
247
248         dnBuilderWidget.validate();
249         final RDN[] rdns = new RDN[]
250             { dnBuilderWidget.getRdn() };
251         final DN[] parentDns = new DN[]
252             { dnBuilderWidget.getParentDn() };
253         final DN dn = new DN( rdns[0], parentDns[0] );
254
255         // check if parent exists or new entry already exists
256
ReadEntryJob readEntryJob1 = new ReadEntryJob( wizard.getSelectedConnection(), parentDns[0] );
257         RunnableContextJobAdapter.execute( readEntryJob1, getContainer(), false );
258         IEntry parentEntry = readEntryJob1.getReadEntry();
259         if ( parentEntry == null )
260         {
261             getShell().getDisplay().syncExec( new Runnable JavaDoc()
262             {
263                 public void run()
264                 {
265                     MessageDialog.openError( getShell(), "Error", "Parent " + dnBuilderWidget.getParentDn().toString()
266                         + " doesn't exists" );
267                 }
268             } );
269             return null;
270         }
271         ReadEntryJob readEntryJob2 = new ReadEntryJob( wizard.getSelectedConnection(), dn );
272         RunnableContextJobAdapter.execute( readEntryJob2, getContainer(), false );
273         IEntry entry = readEntryJob2.getReadEntry();
274         if ( entry != null )
275         {
276             getShell().getDisplay().syncExec( new Runnable JavaDoc()
277             {
278                 public void run()
279                 {
280                     MessageDialog.openError( getShell(), "Error", "Entry " + dn.toString() + " already exists" );
281                 }
282             } );
283             return null;
284         }
285
286         return super.getNextPage();
287     }
288
289
290     /**
291      * {@inheritDoc}
292      */

293     public void createControl( Composite parent )
294     {
295         dnBuilderWidget = new DnBuilderWidget( true, true );
296         dnBuilderWidget.addWidgetModifyListener( this );
297         Composite composite = dnBuilderWidget.createContents( parent );
298
299         setControl( composite );
300     }
301
302
303     /**
304      * {@inheritDoc}
305      */

306     public void widgetModified( WidgetModifyEvent event )
307     {
308         validate();
309     }
310
311
312     /**
313      * Saves the dialogs settings.
314      */

315     public void saveDialogSettings()
316     {
317         dnBuilderWidget.saveDialogSettings();
318     }
319
320 }
Popular Tags