]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/jarpackagerfat/FatJarPackageWizard.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / jarpackagerfat / FatJarPackageWizard.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2007, 2012 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 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 83258 [jar exporter] Deploy java application as executable jar
11 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 213638 [jar exporter] create ANT build file for current settings
12 * Ferenc Hechler <ferenc_hechler@users.sourceforge.net> - [jar exporter] export directory entries in "Runnable JAR File" - https://bugs.eclipse.org/bugs/show_bug.cgi?id=243163
13 * Ferenc Hechler, ferenc_hechler@users.sourceforge.net - 219530 [jar application] add Jar-in-Jar ClassLoader option
14 *******************************************************************************/
15package org.eclipse.jdt.internal.ui.jarpackagerfat;
16
17import java.lang.reflect.InvocationTargetException;
18import java.util.HashSet;
19import java.util.Iterator;
20
21import org.eclipse.swt.widgets.Shell;
22
23import org.eclipse.core.runtime.Assert;
24import org.eclipse.core.runtime.IStatus;
25import org.eclipse.core.runtime.MultiStatus;
26
27import org.eclipse.jface.dialogs.ErrorDialog;
28import org.eclipse.jface.dialogs.IDialogConstants;
29import org.eclipse.jface.dialogs.IDialogSettings;
30import org.eclipse.jface.dialogs.MessageDialog;
31import org.eclipse.jface.viewers.ISelection;
32import org.eclipse.jface.viewers.IStructuredSelection;
33import org.eclipse.jface.viewers.StructuredSelection;
34import org.eclipse.jface.window.Window;
35import org.eclipse.jface.wizard.IWizardPage;
36import org.eclipse.jface.wizard.Wizard;
37
38import org.eclipse.ui.IExportWizard;
39import org.eclipse.ui.IWorkbench;
40
41import org.eclipse.jdt.core.IJavaElement;
42import org.eclipse.jdt.core.IJavaProject;
43import org.eclipse.jdt.core.IPackageFragmentRoot;
44
45import org.eclipse.jdt.ui.jarpackager.IJarExportRunnable;
46import org.eclipse.jdt.ui.jarpackager.JarPackageData;
47
48import org.eclipse.jdt.internal.ui.JavaPlugin;
49import org.eclipse.jdt.internal.ui.JavaPluginImages;
50import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog;
51import org.eclipse.jdt.internal.ui.jarpackagerfat.FatJarPackageWizardPage.LibraryHandler;
52import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
53
54
55/**
56 * Wizard for exporting resources from the workspace to a Fat Java Archive (JAR) file.
57 * The exported jar will contain all required libraries.
58 *
59 * @since 3.4
60 */
61public class FatJarPackageWizard extends Wizard implements IExportWizard {
62
63 private static String DIALOG_SETTINGS_KEY= "FatJarPackageWizard"; //$NON-NLS-1$
64
65 private static final class IPIssueWarningDialog extends OptionalMessageDialog {
66
67 private static final String ID= "RunnableJar.export.ipwarning"; //$NON-NLS-1$
68
69 protected IPIssueWarningDialog(Shell parent, String title, String message) {
70 super(ID, parent, title, null, message, MessageDialog.WARNING, new String[] { IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, 0);
71 }
72
73 }
74
75 private boolean fHasNewDialogSettings;
76 private boolean fInitializeFromJarPackage;
77 private JarPackageData fJarPackage;
78 private FatJarPackageWizardPage fJarPackageWizardPage;
79 private IStructuredSelection fSelection;
80
81 /**
82 * Creates a wizard for exporting workspace resources to a JAR file.
83 */
84 public FatJarPackageWizard() {
85 IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings();
86 IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
87 if (section == null)
88 fHasNewDialogSettings= true;
89 else {
90 fHasNewDialogSettings= false;
91 setDialogSettings(section);
92 }
93 }
94
95 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public void addPages() {
100 super.addPages();
101 fJarPackageWizardPage= new FatJarPackageWizardPage(fJarPackage, fSelection);
102 addPage(fJarPackageWizardPage);
103 }
104
105 /**
106 * Exports the JAR package.
107 *
108 * @param op the operation to run
109 * @param wizardPageStatus the status returned by the wizard page
110 * @return a boolean indicating success or failure
111 */
112 protected boolean executeExportOperation(IJarExportRunnable op, IStatus wizardPageStatus) {
113 try {
114 getContainer().run(true, true, op);
115 } catch (InterruptedException e) {
116 return false;
117 } catch (InvocationTargetException ex) {
118 if (ex.getTargetException() != null) {
119 ExceptionHandler.handle(ex, getShell(), FatJarPackagerMessages.JarPackageWizard_jarExportError_title, FatJarPackagerMessages.JarPackageWizard_jarExportError_message);
120 return false;
121 }
122 }
123 IStatus status= op.getStatus();
124 if (!status.isOK()) {
125 if (!wizardPageStatus.isOK()) {
126 if (!(status instanceof MultiStatus))
127 status= new MultiStatus(status.getPlugin(), status.getCode(), status.getMessage(), status.getException());
128
129 ((MultiStatus) status).add(wizardPageStatus);
130 }
131 ErrorDialog.openError(getShell(), FatJarPackagerMessages.JarPackageWizard_jarExport_title, null, status);
132 return !(status.matches(IStatus.ERROR));
133 } else if (!wizardPageStatus.isOK()) {
134 ErrorDialog.openError(getShell(), FatJarPackagerMessages.JarPackageWizard_jarExport_title, null, wizardPageStatus);
135 }
136 return true;
137 }
138
139 @Override
140 public IWizardPage getNextPage(IWizardPage page) {
141 return super.getNextPage(page);
142 }
143
144 @Override
145 public IWizardPage getPreviousPage(IWizardPage page) {
146 return super.getPreviousPage(page);
147 }
148
149 /**
150 * @return all java projects which contain the selected elements in the active workbench window
151 */
152 protected IStructuredSelection getSelectedJavaProjects() {
153 ISelection currentSelection= JavaPlugin.getActiveWorkbenchWindow().getSelectionService().getSelection();
154 if (currentSelection instanceof IStructuredSelection) {
155 IStructuredSelection structuredSelection= (IStructuredSelection) currentSelection;
156 HashSet<IJavaProject> selectedElements= new HashSet<IJavaProject>();
157 Iterator<?> iter= structuredSelection.iterator();
158 while (iter.hasNext()) {
159 Object selectedElement= iter.next();
160 if (selectedElement instanceof IJavaElement) {
161 IJavaProject javaProject= ((IJavaElement) selectedElement).getJavaProject();
162 if (javaProject != null)
163 selectedElements.add(javaProject);
164 }
165 }
166 return new StructuredSelection(selectedElements);
167 } else
168 return StructuredSelection.EMPTY;
169 }
170
171 /**
172 * {@inheritDoc}
173 */
174 public void init(IWorkbench workbench, IStructuredSelection selection) {
175 fSelection= getSelectedJavaProjects();
176 fJarPackage= new JarPackageData();
177 fJarPackage.setIncludeDirectoryEntries(true);
178 setInitializeFromJarPackage(false);
179 setWindowTitle(FatJarPackagerMessages.JarPackageWizard_windowTitle);
180 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_FAT_JAR_PACKAGER);
181 setNeedsProgressMonitor(true);
182 }
183
184 /**
185 * Initializes this wizard from the given JAR package description.
186 *
187 * @param workbench
188 * the workbench which launched this wizard
189 * @param jarPackage
190 * the JAR package description used to initialize this wizard
191 */
192 public void init(IWorkbench workbench, JarPackageData jarPackage) {
193 Assert.isNotNull(workbench);
194 Assert.isNotNull(jarPackage);
195 fJarPackage= jarPackage;
196 setInitializeFromJarPackage(true);
197 setWindowTitle(FatJarPackagerMessages.JarPackageWizard_windowTitle);
198 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_FAT_JAR_PACKAGER);
199 setNeedsProgressMonitor(true);
200 }
201
202 boolean isInitializingFromJarPackage() {
203 return fInitializeFromJarPackage;
204 }
205
206 /**
207 * {@inheritDoc}
208 */
209 @Override
210 public boolean performFinish() {
211 LibraryHandler libraryHandler= fJarPackageWizardPage.getLibraryHandler();
212 fJarPackage.setJarBuilder(libraryHandler.getBuilder(fJarPackage));
213 MultiStatus status= new MultiStatus(JavaPlugin.getPluginId(), IStatus.OK, FatJarPackagerMessages.FatJarPackageWizard_JarExportProblems_message, null);
214 Object[] elements= fJarPackageWizardPage.getSelectedElementsWithoutContainedChildren(status);
215 fJarPackage.setElements(elements);
216
217 if ((libraryHandler.isShowWarning()) && hasArchive(elements)) {
218 if (OptionalMessageDialog.isDialogEnabled(IPIssueWarningDialog.ID)) {
219 IPIssueWarningDialog dialog= new IPIssueWarningDialog(getShell(), FatJarPackagerMessages.FatJarPackageWizard_IPIssueDialog_title,
220 FatJarPackagerMessages.FatJarPackageWizard_IPIssueDialog_message);
221 if (dialog.open() != Window.OK)
222 return false;
223 }
224 }
225
226 fJarPackageWizardPage.exportAntScript(status);
227
228 if (!executeExportOperation(fJarPackage.createJarExportRunnable(getShell()), status))
229 return false;
230
231 // Save the dialog settings
232 if (fHasNewDialogSettings) {
233 IDialogSettings workbenchSettings= JavaPlugin.getDefault().getDialogSettings();
234 IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
235 section= workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY);
236 setDialogSettings(section);
237 }
238
239 fJarPackageWizardPage.finish();
240 return true;
241 }
242
243 private boolean hasArchive(Object[] elements) {
244 for (int i= 0; i < elements.length; i++) {
245 if (elements[i] instanceof IPackageFragmentRoot) {
246 IPackageFragmentRoot root= (IPackageFragmentRoot) elements[i];
247 if (root.isArchive())
248 return true;
249 }
250 }
251 return false;
252 }
253
254 void setInitializeFromJarPackage(boolean state) {
255 fInitializeFromJarPackage= state;
256 }
257}