KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
25 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserCategory;
26 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserEntryPage;
27 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserSearchResultPage;
28 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
29 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry;
30 import org.apache.directory.ldapstudio.browser.core.jobs.CreateEntryJob;
31 import org.apache.directory.ldapstudio.browser.core.model.DN;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
34 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
35 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
36 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
37 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
38 import org.apache.directory.ldapstudio.browser.core.model.IValue;
39 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
40 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
41 import org.eclipse.jface.viewers.IStructuredSelection;
42 import org.eclipse.jface.wizard.IWizardPage;
43 import org.eclipse.jface.wizard.Wizard;
44 import org.eclipse.jface.wizard.WizardPage;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Composite;
49 import org.eclipse.ui.INewWizard;
50 import org.eclipse.ui.IWorkbench;
51 import org.eclipse.ui.PlatformUI;
52
53
54 /**
55  * The NewEntryWizard is used to create a new entry from scratch or by
56  * using another entry as template.
57  *
58  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
59  * @version $Rev$, $Date$
60  */

61 public class NewEntryWizard extends Wizard implements INewWizard
62 {
63
64     /** The type page. */
65     private NewEntryTypeWizardPage typePage;
66
67     /** The object class page. */
68     private NewEntryObjectclassWizardPage ocPage;
69
70     /** The dn page. */
71     private NewEntryDnWizardPage dnPage;
72
73     /** The attributes page. */
74     private NewEntryAttributesWizardPage attributePage;
75
76     /** The selected entry. */
77     private IEntry selectedEntry;
78
79     /** The selected connection. */
80     private IConnection selectedConnection;
81
82     /** The prototype entry. */
83     private DummyEntry prototypeEntry;
84
85
86     /**
87      * Creates a new instance of NewEntryWizard.
88      */

89     public NewEntryWizard()
90     {
91         setWindowTitle( "New Entry" );
92         setNeedsProgressMonitor( true );
93     }
94
95
96     /**
97      * Gets the id.
98      *
99      * @return the id
100      */

101     public static String JavaDoc getId()
102     {
103         return NewEntryWizard.class.getName();
104     }
105
106
107     /**
108      * {@inheritDoc}
109      */

110     public void init( IWorkbench workbench, IStructuredSelection selection )
111     {
112         // determine the currently selected entry
113
Object JavaDoc o = selection.getFirstElement();
114         if ( o instanceof IEntry )
115         {
116             selectedEntry = ( ( IEntry ) o );
117             selectedConnection = selectedEntry.getConnection();
118         }
119         else if ( o instanceof ISearchResult )
120         {
121             selectedEntry = ( ( ISearchResult ) o ).getEntry();
122             selectedConnection = selectedEntry.getConnection();
123         }
124         else if ( o instanceof IBookmark )
125         {
126             selectedEntry = ( ( IBookmark ) o ).getEntry();
127             selectedConnection = selectedEntry.getConnection();
128         }
129         else if ( o instanceof IAttribute )
130         {
131             selectedEntry = ( ( IAttribute ) o ).getEntry();
132             selectedConnection = selectedEntry.getConnection();
133         }
134         else if ( o instanceof IValue )
135         {
136             selectedEntry = ( ( IValue ) o ).getAttribute().getEntry();
137             selectedConnection = selectedEntry.getConnection();
138         }
139         else if ( o instanceof ISearch )
140         {
141             selectedEntry = null;
142             selectedConnection = ( ( ISearch ) o ).getConnection();
143         }
144         else if ( o instanceof IConnection )
145         {
146             selectedEntry = null;
147             selectedConnection = ( IConnection ) o;
148         }
149         else if ( o instanceof BrowserCategory )
150         {
151             selectedEntry = null;
152             selectedConnection = ( ( BrowserCategory ) o ).getParent();
153         }
154         else if ( o instanceof BrowserSearchResultPage )
155         {
156             selectedEntry = null;
157             selectedConnection = ( ( BrowserSearchResultPage ) o ).getSearch().getConnection();
158         }
159         else if ( o instanceof BrowserEntryPage )
160         {
161             selectedEntry = null;
162             selectedConnection = ( ( BrowserEntryPage ) o ).getEntry().getConnection();
163         }
164         else
165         {
166             selectedEntry = null;
167             selectedConnection = null;
168         }
169
170         if ( selectedConnection != null )
171         {
172             selectedConnection.suspend();
173             prototypeEntry = new DummyEntry( new DN(), selectedConnection );
174         }
175     }
176
177
178     /**
179      * {@inheritDoc}
180      */

181     public void addPages()
182     {
183         if ( selectedConnection != null )
184         {
185             typePage = new NewEntryTypeWizardPage( NewEntryTypeWizardPage.class.getName(), this );
186             addPage( typePage );
187
188             ocPage = new NewEntryObjectclassWizardPage( NewEntryObjectclassWizardPage.class.getName(), this );
189             addPage( ocPage );
190
191             dnPage = new NewEntryDnWizardPage( NewEntryDnWizardPage.class.getName(), this );
192             addPage( dnPage );
193
194             attributePage = new NewEntryAttributesWizardPage( NewEntryAttributesWizardPage.class.getName(), this );
195             addPage( attributePage );
196         }
197         else
198         {
199             IWizardPage page = new DummyWizardPage();
200             addPage( page );
201         }
202     }
203
204
205     /**
206      * {@inheritDoc}
207      */

208     public void createPageControls( Composite pageContainer )
209     {
210         super.createPageControls( pageContainer );
211
212         // set help context ID
213
if ( selectedConnection != null )
214         {
215             PlatformUI.getWorkbench().getHelpSystem().setHelp( typePage.getControl(),
216                 BrowserUIPlugin.PLUGIN_ID + "." + "tools_newentry_wizard" );
217             PlatformUI.getWorkbench().getHelpSystem().setHelp( ocPage.getControl(),
218                 BrowserUIPlugin.PLUGIN_ID + "." + "tools_newentry_wizard" );
219             PlatformUI.getWorkbench().getHelpSystem().setHelp( dnPage.getControl(),
220                 BrowserUIPlugin.PLUGIN_ID + "." + "tools_newentry_wizard" );
221             PlatformUI.getWorkbench().getHelpSystem().setHelp( attributePage.getControl(),
222                 BrowserUIPlugin.PLUGIN_ID + "." + "tools_newentry_wizard" );
223         }
224     }
225
226     /**
227      * Just a dummy page.
228      *
229      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
230      * @version $Rev$, $Date$
231      */

232     class DummyWizardPage extends WizardPage
233     {
234
235         /**
236          * Creates a new instance of DummyWizardPage.
237          */

238         protected DummyWizardPage()
239         {
240             super( "" );
241             setTitle( "No connection selected or connection is closed" );
242             setDescription( "In order to use the entry creation wizard please select an entry or connection." );
243             setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor(
244                 BrowserUIConstants.IMG_ENTRY_WIZARD ) );
245             setPageComplete( true );
246         }
247
248
249         /**
250          * {@inheritDoc}
251          */

252         public void createControl( Composite parent )
253         {
254             Composite composite = new Composite( parent, SWT.NONE );
255             GridLayout gl = new GridLayout( 1, false );
256             composite.setLayout( gl );
257             composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
258
259             setControl( composite );
260         }
261     }
262
263
264     /**
265      * {@inheritDoc}
266      */

267     public boolean performCancel()
268     {
269         if ( selectedConnection != null && selectedConnection.isOpened() )
270         {
271             EventRegistry.suspendEventFireingInCurrentThread();
272             selectedConnection.reset();
273             EventRegistry.resumeEventFireingInCurrentThread();
274             selectedConnection.reset();
275         }
276
277         return true;
278     }
279
280
281     /**
282      * {@inheritDoc}
283      */

284     public boolean performFinish()
285     {
286         try
287         {
288             if ( selectedConnection != null && selectedConnection.isOpened() )
289             {
290                 typePage.saveDialogSettings();
291                 ocPage.saveDialogSettings();
292                 dnPage.saveDialogSettings();
293
294                 getSelectedConnection().reset();
295
296                 CreateEntryJob job = new CreateEntryJob( new IEntry[]
297                     { getPrototypeEntry() } );
298                 RunnableContextJobAdapter.execute( job, getContainer() );
299
300                 if ( !job.getExternalResult().isOK() )
301                 {
302                     getSelectedConnection().suspend();
303                     return false;
304                 }
305                 else
306                 {
307                     return true;
308                 }
309             }
310             else
311             {
312                 return true;
313             }
314         }
315         catch ( Throwable JavaDoc t )
316         {
317             t.printStackTrace();
318             return false;
319         }
320     }
321
322
323     /**
324      * Gets the selected entry.
325      *
326      * @return the selected entry
327      */

328     public IEntry getSelectedEntry()
329     {
330         return selectedEntry;
331     }
332
333
334     /**
335      * Gets the selected connection.
336      *
337      * @return the selected connection
338      */

339     public IConnection getSelectedConnection()
340     {
341         return selectedConnection;
342     }
343
344
345     /**
346      * Gets the prototype entry.
347      *
348      * @return the prototype entry
349      */

350     public DummyEntry getPrototypeEntry()
351     {
352         return prototypeEntry;
353     }
354
355
356     /**
357      * Sets the prototype entry.
358      *
359      * @param getPrototypeEntry the prototype entry
360      */

361     public void setPrototypeEntry( DummyEntry getPrototypeEntry )
362     {
363         this.prototypeEntry = getPrototypeEntry;
364     }
365
366 }
367
Popular Tags