]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ArrayList; | |
15 | import java.util.Iterator; | |
16 | import java.util.List; | |
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 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<Object> 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 (!isInArchiveOrExternal(je)) | |
111 | selectedElements.add(je); | |
112 | } else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) { | |
113 | if (!isInArchiveOrExternal(je)) | |
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 && !isInArchiveOrExternal(je)) | |
120 | selectedElements.add(openable); | |
121 | } | |
122 | } | |
123 | ||
124 | private static boolean isInArchiveOrExternal(IJavaElement element) { | |
125 | IPackageFragmentRoot root= JavaModelUtil.getPackageFragmentRoot(element); | |
126 | return root != null && (root.isArchive() || root.isExternal()); | |
127 | } | |
128 | ||
129 | /** | |
130 | * {@inheritDoc} | |
131 | */ | |
132 | @Override | |
133 | public void addPages() { | |
134 | super.addPages(); | |
135 | fJarPackageWizardPage= new JarPackageWizardPage(fJarPackage, fSelection); | |
136 | addPage(fJarPackageWizardPage); | |
137 | fJarOptionsWizardPage= new JarOptionsPage(fJarPackage); | |
138 | addPage(fJarOptionsWizardPage); | |
139 | addPage(new JarManifestWizardPage(fJarPackage)); | |
140 | } | |
141 | ||
142 | private void addProject(List<Object> selectedElements, IProject project) { | |
143 | try { | |
144 | if (project.hasNature(JavaCore.NATURE_ID)) | |
145 | selectedElements.add(JavaCore.create(project)); | |
146 | } catch (CoreException ex) { | |
147 | // ignore selected element | |
148 | } | |
149 | } | |
150 | ||
151 | private void addResource(List<Object> selectedElements, IResource resource) { | |
152 | IJavaElement je= JavaCore.create(resource); | |
153 | if (je != null && je.exists() && je.getElementType() == IJavaElement.COMPILATION_UNIT) | |
154 | selectedElements.add(je); | |
155 | else | |
156 | selectedElements.add(resource); | |
157 | } | |
158 | ||
159 | /** | |
160 | * Exports the JAR package. | |
161 | * | |
162 | * @param op the op | |
163 | * @return a boolean indicating success or failure | |
164 | */ | |
165 | protected boolean executeExportOperation(IJarExportRunnable op) { | |
166 | try { | |
167 | getContainer().run(true, true, op); | |
168 | } catch (InterruptedException e) { | |
169 | return false; | |
170 | } catch (InvocationTargetException ex) { | |
171 | if (ex.getTargetException() != null) { | |
172 | ExceptionHandler.handle(ex, getShell(), JarPackagerMessages.JarPackageWizard_jarExportError_title, JarPackagerMessages.JarPackageWizard_jarExportError_message); | |
173 | return false; | |
174 | } | |
175 | } | |
176 | IStatus status= op.getStatus(); | |
177 | if (!status.isOK()) { | |
178 | ErrorDialog.openError(getShell(), JarPackagerMessages.JarPackageWizard_jarExport_title, null, status); | |
179 | return !(status.matches(IStatus.ERROR)); | |
180 | } | |
181 | return true; | |
182 | } | |
183 | ||
184 | @Override | |
185 | public IWizardPage getNextPage(IWizardPage page) { | |
186 | if (page == fJarPackageWizardPage && !fJarPackage.isRefactoringAware()) | |
187 | return fJarOptionsWizardPage; | |
188 | return super.getNextPage(page); | |
189 | } | |
190 | ||
191 | @Override | |
192 | public IWizardPage getPreviousPage(IWizardPage page) { | |
193 | if (page == fJarOptionsWizardPage && !fJarPackage.isRefactoringAware()) | |
194 | return fJarPackageWizardPage; | |
195 | return super.getPreviousPage(page); | |
196 | } | |
197 | ||
198 | /** | |
199 | * Gets the current workspace page selection and converts it to a valid | |
200 | * selection for this wizard: - resources and projects are OK - CUs are OK - | |
201 | * Java projects are OK - Source package fragments and source packages | |
202 | * fragement roots are ok - Java elements below a CU are converted to their | |
203 | * CU - all other input elements are ignored | |
204 | * | |
205 | * @return a valid structured selection based on the current selection | |
206 | */ | |
207 | protected IStructuredSelection getValidSelection() { | |
208 | ISelection currentSelection= JavaPlugin.getActiveWorkbenchWindow().getSelectionService().getSelection(); | |
209 | if (currentSelection instanceof IStructuredSelection) { | |
210 | IStructuredSelection structuredSelection= (IStructuredSelection) currentSelection; | |
211 | List<Object> selectedElements= new ArrayList<Object>(structuredSelection.size()); | |
212 | Iterator<?> iter= structuredSelection.iterator(); | |
213 | while (iter.hasNext()) { | |
214 | Object selectedElement= iter.next(); | |
215 | if (selectedElement instanceof IProject) | |
216 | addProject(selectedElements, (IProject) selectedElement); | |
217 | else if (selectedElement instanceof IResource) | |
218 | addResource(selectedElements, (IResource) selectedElement); | |
219 | else if (selectedElement instanceof IJavaElement) | |
220 | addJavaElement(selectedElements, (IJavaElement) selectedElement); | |
221 | } | |
222 | return new StructuredSelection(selectedElements); | |
223 | } else | |
224 | return StructuredSelection.EMPTY; | |
225 | } | |
226 | ||
227 | /** | |
228 | * {@inheritDoc} | |
229 | */ | |
230 | public void init(IWorkbench workbench, IStructuredSelection selection) { | |
231 | // ignore the selection argument since the main export wizard changed it | |
232 | fSelection= getValidSelection(); | |
233 | fJarPackage= new JarPackageData(); | |
234 | setInitializeFromJarPackage(false); | |
235 | setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle); | |
236 | setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER); | |
237 | setNeedsProgressMonitor(true); | |
238 | } | |
239 | ||
240 | /** | |
241 | * Initializes this wizard from the given JAR package description. | |
242 | * | |
243 | * @param workbench | |
244 | * the workbench which launched this wizard | |
245 | * @param jarPackage | |
246 | * the JAR package description used to initialize this wizard | |
247 | */ | |
248 | public void init(IWorkbench workbench, JarPackageData jarPackage) { | |
249 | Assert.isNotNull(workbench); | |
250 | Assert.isNotNull(jarPackage); | |
251 | fJarPackage= jarPackage; | |
252 | setInitializeFromJarPackage(true); | |
253 | fSelection= new StructuredSelection(fJarPackage.getElements()); | |
254 | setWindowTitle(JarPackagerMessages.JarPackageWizard_windowTitle); | |
255 | setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_JAR_PACKAGER); | |
256 | setNeedsProgressMonitor(true); | |
257 | } | |
258 | ||
259 | boolean isInitializingFromJarPackage() { | |
260 | return fInitializeFromJarPackage; | |
261 | } | |
262 | ||
263 | /** | |
264 | * {@inheritDoc} | |
265 | */ | |
266 | @Override | |
267 | public boolean performFinish() { | |
268 | fJarPackage.setElements(fJarPackageWizardPage.getSelectedElementsWithoutContainedChildren()); | |
269 | ||
270 | if (!executeExportOperation(fJarPackage.createJarExportRunnable(getShell()))) | |
271 | return false; | |
272 | ||
273 | // Save the dialog settings | |
274 | if (fHasNewDialogSettings) { | |
275 | IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings(); | |
276 | IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY); | |
277 | section= workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY); | |
278 | setDialogSettings(section); | |
279 | } | |
280 | IWizardPage[] pages= getPages(); | |
281 | for (int i= 0; i < getPageCount(); i++) { | |
282 | IWizardPage page= pages[i]; | |
283 | if (page instanceof IJarPackageWizardPage) | |
284 | ((IJarPackageWizardPage) page).finish(); | |
285 | } | |
286 | return true; | |
287 | } | |
288 | ||
289 | void setInitializeFromJarPackage(boolean state) { | |
290 | fInitializeFromJarPackage= state; | |
291 | } | |
292 | } |