KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > jarpackager > JarPackageWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.jarpackager;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24
25 import org.eclipse.jface.dialogs.ErrorDialog;
26 import org.eclipse.jface.dialogs.IDialogSettings;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.wizard.IWizardPage;
31 import org.eclipse.jface.wizard.Wizard;
32
33 import org.eclipse.ui.IExportWizard;
34 import org.eclipse.ui.IWorkbench;
35
36 import org.eclipse.jdt.core.IClassFile;
37 import org.eclipse.jdt.core.ICompilationUnit;
38 import org.eclipse.jdt.core.IJavaElement;
39 import org.eclipse.jdt.core.IOpenable;
40 import org.eclipse.jdt.core.IPackageFragmentRoot;
41 import org.eclipse.jdt.core.JavaCore;
42
43 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44
45 import org.eclipse.jdt.ui.jarpackager.IJarExportRunnable;
46 import org.eclipse.jdt.ui.jarpackager.JarPackageData;
47
48 import org.eclipse.jdt.internal.ui.JavaPlugin;
49 import org.eclipse.jdt.internal.ui.JavaPluginImages;
50 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
51
52 /**
53  * Wizard for exporting resources from the workspace to a Java Archive (JAR) file.
54  * <p>
55  * This class may be instantiated and used without further configuration;
56  * this class is not intended to be subclassed.
57  * </p>
58  * <p>
59  * Example:
60  * <pre>
61  * IWizard wizard = new JarPackageWizard();
62  * wizard.init(workbench, selection);
63  * WizardDialog dialog = new WizardDialog(shell, wizard);
64  * dialog.open();
65  * </pre>
66  * During the call to <code>open</code>, the wizard dialog is presented to the
67  * user. When the user hits Finish, the user-selected workspace resources
68  * are exported to the user-specified zip file, the dialog closes, and the call
69  * to <code>open</code> returns.
70  * </p>
71  */

72 public class JarPackageWizard extends Wizard implements IExportWizard {
73
74     private static String JavaDoc DIALOG_SETTINGS_KEY= "JarPackageWizard"; //$NON-NLS-1$
75

76     private boolean fHasNewDialogSettings;
77
78     private boolean fInitializeFromJarPackage;
79
80     private JarOptionsPage fJarOptionsWizardPage;
81
82     private JarPackageData fJarPackage;
83
84     private JarPackageWizardPage fJarPackageWizardPage;
85
86     private IStructuredSelection fSelection;
87
88     /**
89      * Creates a wizard for exporting workspace resources to a JAR file.
90      */

91     public JarPackageWizard() {
92         IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings();
93         IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
94         if (section == null)
95             fHasNewDialogSettings= true;
96         else {
97             fHasNewDialogSettings= false;
98             setDialogSettings(section);
99         }
100     }
101
102     private void addJavaElement(List JavaDoc selectedElements, IJavaElement je) {
103         if (je.getElementType() == IJavaElement.COMPILATION_UNIT)
104             selectedElements.add(je);
105         else if (je.getElementType() == IJavaElement.CLASS_FILE)
106             selectedElements.add(je);
107         else if (je.getElementType() == IJavaElement.JAVA_PROJECT)
108             selectedElements.add(je);
109         else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
110             if (!JavaModelUtil.getPackageFragmentRoot(je).isArchive())
111                 selectedElements.add(je);
112         } else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
113             if (!((IPackageFragmentRoot) je).isArchive())
114                 selectedElements.add(je);
115         } else {
116             IOpenable openable= je.getOpenable();
117             if (openable instanceof ICompilationUnit)
118                 selectedElements.add(((ICompilationUnit) openable).getPrimary());
119             else if (openable instanceof IClassFile && !JavaModelUtil.getPackageFragmentRoot(je).isArchive())
120                 selectedElements.add(openable);
121         }
122     }
123
124     /**
125      * {@inheritDoc}
126      */

127     public void addPages() {
128         super.addPages();
129         fJarPackageWizardPage= new JarPackageWizardPage(fJarPackage, fSelection);
130         addPage(fJarPackageWizardPage);
131         fJarOptionsWizardPage= new JarOptionsPage(fJarPackage);
132         addPage(fJarOptionsWizardPage);
133         addPage(new JarManifestWizardPage(fJarPackage));
134     }
135
136     private void addProject(List JavaDoc selectedElements, IProject project) {
137         try {
138             if (project.hasNature(JavaCore.NATURE_ID))
139                 selectedElements.add(JavaCore.create(project));
140         } catch (CoreException ex) {
141             // ignore selected element
142
}
143     }
144
145     private void addResource(List JavaDoc selectedElements, IResource resource) {
146         IJavaElement je= JavaCore.create(resource);
147         if (je != null && je.exists() && je.getElementType() == IJavaElement.COMPILATION_UNIT)
148             selectedElements.add(je);
149         else
150             selectedElements.add(resource);
151     }
152
153     /**
154      * Exports the JAR package.
155      *
156      * @return a boolean indicating success or failure
157      */

158     protected boolean executeExportOperation(IJarExportRunnable op) {
159         try {
160             getContainer().run(true, true, op);
161         } catch (InterruptedException JavaDoc e) {
162             return false;
163         } catch (InvocationTargetException JavaDoc ex) {
164             if (ex.getTargetException() != null) {
165                 ExceptionHandler.handle(ex, getShell(), JarPackagerMessages.JarPackageWizard_jarExportError_title, JarPackagerMessages.JarPackageWizard_jarExportError_message);
166                 return false;
167             }
168         }
169         IStatus status= op.getStatus();
170         if (!status.isOK()) {
171             ErrorDialog.openError(getShell(), JarPackagerMessages.JarPackageWizard_jarExport_title, null, status);
172             return !(status.matches(IStatus.ERROR));
173         }
174         return true;
175     }
176
177     public IWizardPage getNextPage(IWizardPage page) {
178         if (page == fJarPackageWizardPage && !fJarPackage.isRefactoringAware())
179             return fJarOptionsWizardPage;
180         return super.getNextPage(page);
181     }
182
183     public IWizardPage getPreviousPage(IWizardPage page) {
184         if (page == fJarOptionsWizardPage && !fJarPackage.isRefactoringAware())
185             return fJarPackageWizardPage;
186         return super.getPreviousPage(page);
187     }
188
189     /**
190      * Gets the current workspace page selection and converts it to a valid
191      * selection for this wizard: - resources and projects are OK - CUs are OK -
192      * Java projects are OK - Source package fragments and source packages
193      * fragement roots are ok - Java elements below a CU are converted to their
194      * CU - all other input elements are ignored
195      *
196      * @return a valid structured selection based on the current selection
197      */

198     protected IStructuredSelection getValidSelection() {
199         ISelection currentSelection= JavaPlugin.getActiveWorkbenchWindow().getSelectionService().getSelection();
200         if (currentSelection instanceof IStructuredSelection) {
201             IStructuredSelection structuredSelection= (IStructuredSelection) currentSelection;
202             List JavaDoc selectedElements= new ArrayList JavaDoc(structuredSelection.size());
203             Iterator JavaDoc iter= structuredSelection.iterator();
204             while (iter.hasNext()) {
205                 Object JavaDoc selectedElement= iter.next();
206                 if (selectedElement instanceof IProject)
207                     addProject(selectedElements, (IProject) selectedElement);
208                 else if (selectedElement instanceof IResource)
209                     addResource(selectedElements, (IResource) selectedElement);
210                 else if (selectedElement instanceof IJavaElement)
211                     addJavaElement(selectedElements, (IJavaElement) selectedElement);
212             }
213             return new StructuredSelection(selectedElements);
214         } else
215             return StructuredSelection.EMPTY;
216     }
217
218     /**
219      * {@inheritDoc}
220      */

221     public void init(IWorkbench workbench, IStructuredSelection selection) {
222         // ignore the selection argument since the main export wizard changed it
223
fSelection= getValidSelection();
224         fJarPackage= new JarPackageData();
225         setInitializeFromJarPackage(false);
226         setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle);
227         setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER);
228         setNeedsProgressMonitor(true);
229     }
230
231     /**
232      * Initializes this wizard from the given JAR package description.
233      *
234      * @param workbench
235      * the workbench which launched this wizard
236      * @param jarPackage
237      * the JAR package description used to initialize this wizard
238      */

239     public void init(IWorkbench workbench, JarPackageData jarPackage) {
240         Assert.isNotNull(workbench);
241         Assert.isNotNull(jarPackage);
242         fJarPackage= jarPackage;
243         setInitializeFromJarPackage(true);
244         fSelection= new StructuredSelection(fJarPackage.getElements());
245         setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle);
246         setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER);
247         setNeedsProgressMonitor(true);
248     }
249
250     boolean isInitializingFromJarPackage() {
251         return fInitializeFromJarPackage;
252     }
253
254     /**
255      * {@inheritDoc}
256      */

257     public boolean performFinish() {
258         fJarPackage.setElements(fJarPackageWizardPage.getSelectedElementsWithoutContainedChildren());
259
260         if (!executeExportOperation(fJarPackage.createJarExportRunnable(getShell())))
261             return false;
262
263         // Save the dialog settings
264
if (fHasNewDialogSettings) {
265             IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings();
266             IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
267             section= workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY);
268             setDialogSettings(section);
269         }
270         IWizardPage[] pages= getPages();
271         for (int i= 0; i < getPageCount(); i++) {
272             IWizardPage page= pages[i];
273             if (page instanceof IJarPackageWizardPage)
274                 ((IJarPackageWizardPage) page).finish();
275         }
276         return true;
277     }
278
279     void setInitializeFromJarPackage(boolean state) {
280         fInitializeFromJarPackage= state;
281     }
282 }
Popular Tags