KickJava   Java API By Example, From Geeks To Geeks.

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


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.actions.SelectionUtils;
25 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
27 import org.apache.directory.ldapstudio.browser.core.jobs.SearchJob;
28 import org.apache.directory.ldapstudio.browser.core.model.DN;
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
34 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
35 import org.apache.directory.ldapstudio.browser.core.model.IValue;
36 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
37 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor;
38 import org.apache.directory.ldapstudio.ldifeditor.editor.NonExistingLdifEditorInput;
39
40 import org.eclipse.jface.text.IDocument;
41 import org.eclipse.jface.viewers.ISelection;
42 import org.eclipse.jface.viewers.IStructuredSelection;
43 import org.eclipse.jface.wizard.IWizardPage;
44 import org.eclipse.jface.wizard.Wizard;
45 import org.eclipse.jface.wizard.WizardPage;
46 import org.eclipse.swt.SWT;
47 import org.eclipse.swt.layout.GridData;
48 import org.eclipse.swt.layout.GridLayout;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.ui.IEditorInput;
51 import org.eclipse.ui.IEditorPart;
52 import org.eclipse.ui.INewWizard;
53 import org.eclipse.ui.IWorkbench;
54 import org.eclipse.ui.IWorkbenchPage;
55 import org.eclipse.ui.IWorkbenchWindow;
56 import org.eclipse.ui.PartInitException;
57 import org.eclipse.ui.PlatformUI;
58 import org.eclipse.ui.texteditor.IDocumentProvider;
59
60
61 public class BatchOperationWizard extends Wizard implements INewWizard
62 {
63
64     private IConnection connection;
65
66     private BatchOperationApplyOnWizardPage applyOnPage;
67
68     private BatchOperationTypeWizardPage typePage;
69
70     private BatchOperationLdifWizardPage ldifPage;
71
72     private BatchOperationModifyWizardPage modifyPage;
73
74     private BatchOperationFinishWizardPage finishPage;
75
76
77     public BatchOperationWizard()
78     {
79         super.setWindowTitle( "Batch Operation" );
80         super.setNeedsProgressMonitor( true );
81     }
82
83
84     public static String JavaDoc getId()
85     {
86         return BatchOperationWizard.class.getName();
87     }
88
89
90     public void init( IWorkbench workbench, IStructuredSelection selection )
91     {
92         // PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection()
93
}
94
95
96     public void addPages()
97     {
98
99         ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService()
100             .getSelection();
101         IConnection[] connections = SelectionUtils.getConnections( selection );
102         ISearch[] searches = SelectionUtils.getSearches( selection );
103         IEntry[] entries = SelectionUtils.getEntries( selection );
104         ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection );
105         IBookmark[] bookmarks = SelectionUtils.getBookmarks( selection );
106         IAttribute[] attributes = SelectionUtils.getAttributes( selection );
107         IValue[] values = SelectionUtils.getValues( selection );
108
109         // if(searches.length + entries.length + searchResults.length +
110
// bookmarks.length > 0) {
111
if ( connections.length > 0
112             && connections[0].isOpened()
113             || searches.length + entries.length + searchResults.length + bookmarks.length + attributes.length
114                 + values.length > 0 )
115         {
116
117             ISearch search = SelectionUtils.getExampleSearch( selection );
118             search.setName( null );
119             this.connection = search.getConnection();
120
121             applyOnPage = new BatchOperationApplyOnWizardPage( BatchOperationApplyOnWizardPage.class.getName(), this );
122             addPage( applyOnPage );
123
124             typePage = new BatchOperationTypeWizardPage( BatchOperationTypeWizardPage.class.getName(), this );
125             addPage( typePage );
126
127             ldifPage = new BatchOperationLdifWizardPage( BatchOperationLdifWizardPage.class.getName(), this );
128             addPage( ldifPage );
129
130             modifyPage = new BatchOperationModifyWizardPage( BatchOperationModifyWizardPage.class.getName(), this );
131             addPage( modifyPage );
132
133             finishPage = new BatchOperationFinishWizardPage( BatchOperationFinishWizardPage.class.getName(), this );
134             addPage( finishPage );
135         }
136         else
137         {
138             IWizardPage page = new DummyWizardPage();
139             addPage( page );
140         }
141     }
142
143
144     /**
145      * {@inheritDoc}
146      */

147     public void createPageControls( Composite pageContainer )
148     {
149         super.createPageControls( pageContainer );
150         
151         // set help context ID
152
PlatformUI.getWorkbench().getHelpSystem().setHelp( applyOnPage.getControl(),
153             BrowserUIPlugin.PLUGIN_ID + "." + "tools_batchoperation_wizard" );
154         PlatformUI.getWorkbench().getHelpSystem().setHelp( typePage.getControl(),
155             BrowserUIPlugin.PLUGIN_ID + "." + "tools_batchoperation_wizard" );
156         PlatformUI.getWorkbench().getHelpSystem().setHelp( ldifPage.getControl(),
157             BrowserUIPlugin.PLUGIN_ID + "." + "tools_batchoperation_wizard" );
158         PlatformUI.getWorkbench().getHelpSystem().setHelp( modifyPage.getControl(),
159             BrowserUIPlugin.PLUGIN_ID + "." + "tools_batchoperation_wizard" );
160         PlatformUI.getWorkbench().getHelpSystem().setHelp( finishPage.getControl(),
161             BrowserUIPlugin.PLUGIN_ID + "." + "tools_batchoperation_wizard" );
162     }
163
164
165     class DummyWizardPage extends WizardPage
166     {
167
168         protected DummyWizardPage()
169         {
170             super( "" );
171             super.setTitle( "No connection selected or connection is closed" );
172             super.setDescription( "In order to use the batch operation wizard please select a opened connection." );
173             // super.setImageDescriptor(BrowserUIPlugin.getDefault().getImageDescriptor(BrowserUIConstants.IMG_ENTRY_WIZARD));
174
super.setPageComplete( true );
175         }
176
177
178         public void createControl( Composite parent )
179         {
180             Composite composite = new Composite( parent, SWT.NONE );
181             GridLayout gl = new GridLayout( 1, false );
182             composite.setLayout( gl );
183             composite.setLayoutData( new GridData( GridData.FILL_BOTH ) );
184
185             setControl( composite );
186         }
187     }
188
189
190     public IWizardPage getNextPage( IWizardPage page )
191     {
192
193         if ( this.applyOnPage != null )
194         {
195
196             if ( page == this.applyOnPage )
197             {
198                 return this.typePage;
199             }
200
201             else if ( page == this.typePage
202                 && this.typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_CREATE_LDIF )
203             {
204                 return this.ldifPage;
205             }
206             else if ( page == this.typePage
207                 && this.typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_MODIFY )
208             {
209                 return this.modifyPage;
210             }
211             else if ( page == this.typePage
212                 && this.typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_DELETE )
213             {
214                 return this.finishPage;
215             }
216
217             else if ( page == this.modifyPage )
218             {
219                 return this.finishPage;
220             }
221             else if ( page == this.ldifPage )
222             {
223                 return this.finishPage;
224             }
225         }
226
227         return null;
228     }
229
230
231     public boolean canFinish()
232     {
233
234         if ( this.applyOnPage != null )
235         {
236             if ( !this.applyOnPage.isPageComplete() )
237             {
238                 return false;
239             }
240             if ( !this.typePage.isPageComplete() )
241             {
242                 return false;
243             }
244
245             if ( this.typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_CREATE_LDIF
246                 && !this.ldifPage.isPageComplete() )
247             {
248                 return false;
249             }
250             if ( this.typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_MODIFY
251                 && !this.modifyPage.isPageComplete() )
252             {
253                 return false;
254             }
255
256             if ( !this.finishPage.isPageComplete() )
257             {
258                 return false;
259             }
260         }
261
262         return true;
263     }
264
265
266     public boolean performCancel()
267     {
268         return true;
269     }
270
271
272     public boolean performFinish()
273     {
274
275         if ( this.applyOnPage != null )
276         {
277
278             this.applyOnPage.saveDialogSettings();
279
280             // get LDIF
281
String JavaDoc ldifFragment = "";
282             if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_CREATE_LDIF )
283             {
284                 ldifFragment = this.ldifPage.getLdifFragment();
285             }
286             else if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_MODIFY )
287             {
288                 ldifFragment = this.modifyPage.getLdifFragment();
289             }
290             if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_DELETE )
291             {
292                 ldifFragment = "changetype: delete" + BrowserCoreConstants.LINE_SEPARATOR;
293             }
294
295             // get DNs
296
DN[] dns = applyOnPage.getApplyOnDns();
297             if ( dns == null )
298             {
299                 if ( applyOnPage.getApplyOnSearch() != null )
300                 {
301                     ISearch search = applyOnPage.getApplyOnSearch();
302                     if ( search.getConnection() != null )
303                     {
304                         SearchJob job = new SearchJob( new ISearch[]
305                             { search } );
306                         RunnableContextJobAdapter.execute( job, getContainer() );
307                         if ( job.getExternalResult().isOK() )
308                         {
309                             ISearchResult[] srs = search.getSearchResults();
310                             dns = new DN[srs.length];
311                             for ( int i = 0; i < srs.length; i++ )
312                             {
313                                 dns[i] = srs[i].getDn();
314                             }
315                         }
316                     }
317                 }
318             }
319
320             if ( dns != null )
321             {
322
323                 StringBuffer JavaDoc ldif = new StringBuffer JavaDoc();
324                 for ( int i = 0; i < dns.length; i++ )
325                 {
326                     ldif.append( "dn: " );
327                     ldif.append( dns[i].toString() );
328                     ldif.append( BrowserCoreConstants.LINE_SEPARATOR );
329                     ldif.append( ldifFragment );
330                     ldif.append( BrowserCoreConstants.LINE_SEPARATOR );
331                 }
332
333                 if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_LDIF )
334                 {
335
336                     IEditorInput input = new NonExistingLdifEditorInput();
337                     String JavaDoc editorId = LdifEditor.getId();
338
339                     try
340                     {
341                         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
342                         IWorkbenchPage page = window.getActivePage();
343                         IEditorPart editor = page.openEditor( input, editorId );
344                         IDocumentProvider documentProvider = ( ( LdifEditor ) editor ).getDocumentProvider();
345                         if ( documentProvider != null && input != null )
346                         {
347                             IDocument document = documentProvider.getDocument( input );
348                             if ( document != null )
349                             {
350                                 document.set( ldif.toString() );
351                             }
352                         }
353
354                     }
355                     catch ( PartInitException e )
356                     {
357                         return false;
358                     }
359                     return true;
360
361                 }
362                 else if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_ONLINE )
363                 {
364                     // TODO
365
}
366             }
367
368             return false;
369         }
370
371         return true;
372     }
373
374
375     public BatchOperationTypeWizardPage getTypePage()
376     {
377         return typePage;
378     }
379
380
381     public IConnection getConnection()
382     {
383         return this.connection;
384     }
385
386 }
387
Popular Tags