KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > actions > ImportExportAction


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.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
25 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
26 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
27 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
28 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
29 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
30 import org.apache.directory.ldapstudio.browser.ui.wizards.ExportCsvWizard;
31 import org.apache.directory.ldapstudio.browser.ui.wizards.ExportDsmlWizard;
32 import org.apache.directory.ldapstudio.browser.ui.wizards.ExportExcelWizard;
33 import org.apache.directory.ldapstudio.browser.ui.wizards.ExportLdifWizard;
34 import org.apache.directory.ldapstudio.browser.ui.wizards.ImportDsmlWizard;
35 import org.apache.directory.ldapstudio.browser.ui.wizards.ImportLdifWizard;
36
37 import org.eclipse.jface.resource.ImageDescriptor;
38 import org.eclipse.jface.wizard.IWizard;
39 import org.eclipse.jface.wizard.WizardDialog;
40
41
42 /**
43  * This class implements Import/Export Actions for LDIF, CSV, EXCEL and DSML.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public class ImportExportAction extends BrowserAction
49 {
50     /**
51      * LDIF Import Type
52      */

53     public static final int TYPE_IMPORT_LDIF = 0;
54
55     /**
56      * LDIF Export Type
57      */

58     public static final int TYPE_EXPORT_LDIF = 1;
59
60     /**
61      * CSV Export Type
62      */

63     public static final int TYPE_EXPORT_CSV = 2;
64
65     /**
66      * EXCEL Export Type
67      */

68     public static final int TYPE_EXPORT_EXCEL = 3;
69
70     /**
71      * DSML Import Type
72      */

73     public static final int TYPE_IMPORT_DSML = 4;
74
75     /**
76      * DSML Export Type
77      */

78     public static final int TYPE_EXPORT_DSML = 5;
79
80     private int type;
81
82
83     /**
84      * Creates a new instance of ImportExportAction.
85      *
86      * @param type
87      * the type of Import/Export
88      */

89     public ImportExportAction( int type )
90     {
91         super();
92         this.type = type;
93     }
94
95
96     /**
97      * {@inheritDoc}
98      */

99     public String JavaDoc getText()
100     {
101         if ( this.type == TYPE_IMPORT_LDIF )
102         {
103             return "LDIF Import...";
104         }
105         else if ( this.type == TYPE_EXPORT_LDIF )
106         {
107             return "LDIF Export...";
108         }
109         else if ( this.type == TYPE_EXPORT_CSV )
110         {
111             return "CSV Export...";
112         }
113         else if ( this.type == TYPE_EXPORT_EXCEL )
114         {
115             return "Excel Export...";
116         }
117         else if ( this.type == TYPE_IMPORT_DSML )
118         {
119             return "DSML Import...";
120         }
121         else if ( this.type == TYPE_EXPORT_DSML )
122         {
123             return "DSML Export...";
124         }
125         else
126         {
127             return "Export...";
128         }
129     }
130
131
132     /**
133      * {@inheritDoc}
134      */

135     public ImageDescriptor getImageDescriptor()
136     {
137         if ( this.type == TYPE_IMPORT_LDIF )
138         {
139             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_IMPORT_LDIF );
140         }
141         else if ( this.type == TYPE_EXPORT_LDIF )
142         {
143             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_EXPORT_LDIF );
144         }
145         else if ( this.type == TYPE_EXPORT_CSV )
146         {
147             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_EXPORT_CSV );
148         }
149         else if ( this.type == TYPE_EXPORT_EXCEL )
150         {
151             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_EXPORT_XLS );
152         }
153         else if ( this.type == TYPE_IMPORT_DSML )
154         {
155             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_IMPORT_DSML );
156         }
157         else if ( this.type == TYPE_EXPORT_DSML )
158         {
159             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_EXPORT_DSML );
160         }
161         else
162         {
163             return null;
164         }
165     }
166
167
168     /**
169      * {@inheritDoc}
170      */

171     public String JavaDoc getCommandId()
172     {
173         return null;
174     }
175
176
177     /**
178      * {@inheritDoc}
179      */

180     public boolean isEnabled()
181     {
182         return getEntry() != null || getConnection() != null || getSearch() != null || getConnectionInput() != null;
183
184     }
185
186
187     /**
188      * {@inheritDoc}
189      */

190     public void run()
191     {
192         IWizard wizard = null;
193
194         if ( this.type == TYPE_IMPORT_LDIF )
195         {
196             if ( getEntry() != null )
197             {
198                 wizard = new ImportLdifWizard( getEntry().getConnection() );
199             }
200             else if ( getSearch() != null )
201             {
202                 wizard = new ImportLdifWizard( getSearch().getConnection() );
203             }
204             else if ( getConnectionInput() != null )
205             {
206                 wizard = new ImportLdifWizard( getConnectionInput() );
207             }
208             else if ( getConnection() != null )
209             {
210                 wizard = new ImportLdifWizard( getConnection() );
211             }
212         }
213         else if ( this.type == TYPE_IMPORT_DSML )
214         {
215             if ( getEntry() != null )
216             {
217                 wizard = new ImportDsmlWizard( getEntry().getConnection() );
218             }
219             else if ( getSearch() != null )
220             {
221                 wizard = new ImportDsmlWizard( getSearch().getConnection() );
222             }
223             else if ( getConnectionInput() != null )
224             {
225                 wizard = new ImportDsmlWizard( getConnectionInput() );
226             }
227             else if ( getConnection() != null )
228             {
229                 wizard = new ImportDsmlWizard( getConnection() );
230             }
231         }
232         else if ( this.type == TYPE_EXPORT_LDIF )
233         {
234             wizard = new ExportLdifWizard();
235         }
236         else if ( this.type == TYPE_EXPORT_CSV )
237         {
238             wizard = new ExportCsvWizard();
239         }
240         else if ( this.type == TYPE_EXPORT_EXCEL )
241         {
242             wizard = new ExportExcelWizard();
243         }
244         else if ( this.type == TYPE_EXPORT_DSML )
245         {
246             wizard = new ExportDsmlWizard();
247         }
248
249         if ( wizard != null )
250         {
251             WizardDialog dialog = new WizardDialog( getShell(), wizard );
252             dialog.setBlockOnOpen( true );
253             dialog.create();
254             dialog.open();
255         }
256
257     }
258
259
260     /**
261      * Gets the selected Entry.
262      *
263      * @return
264      * the selected Entry
265      */

266     protected IEntry getEntry()
267     {
268         IEntry entry = null;
269         if ( getSelectedEntries().length > 0 )
270         {
271             entry = getSelectedEntries()[0];
272         }
273         else if ( getSelectedSearchResults().length > 0 )
274         {
275             entry = getSelectedSearchResults()[0].getEntry();
276         }
277         else if ( getSelectedBookmarks().length > 0 )
278         {
279             entry = getSelectedBookmarks()[0].getEntry();
280         }
281
282         return entry != null && entry.getConnection().isOpened() ? entry : null;
283     }
284
285
286     /**
287      * Gets the Connection.
288      *
289      * @return
290      * the Connection
291      */

292     protected IConnection getConnection()
293     {
294         return getSelectedConnections().length > 0 && getSelectedConnections()[0].isOpened() ? getSelectedConnections()[0]
295             : null;
296     }
297
298
299     /**
300      * Gets the Search.
301      *
302      * @return
303      * the Search
304      */

305     protected ISearch getSearch()
306     {
307         return getSelectedSearches().length > 0 && getSelectedSearches()[0].getConnection().isOpened() ? getSelectedSearches()[0]
308             : null;
309     }
310
311
312     /**
313      * Gets the Connection Input.
314      *
315      * @return
316      * the Connection Input
317      */

318     protected IConnection getConnectionInput()
319     {
320
321         if ( getInput() != null && ( getInput() instanceof IConnection ) && ( ( IConnection ) getInput() ).isOpened() )
322         {
323             return ( IConnection ) getInput();
324         }
325         else
326         {
327             return null;
328         }
329     }
330 }
331
Popular Tags