1 /*******************************************************************************
2 * Copyright (c) 2005, 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.jarimport;
14 import java.io.IOException;
15 import java.io.InputStream;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipFile;
20 import org.eclipse.core.filesystem.EFS;
21 import org.eclipse.core.filesystem.IFileStore;
22 import org.eclipse.core.filesystem.URIUtil;
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.SubProgressMonitor;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.resources.ResourcesPlugin;
35 import org.eclipse.jface.dialogs.IDialogSettings;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.wizard.IWizardPage;
39 import org.eclipse.jface.wizard.WizardDialog;
41 import org.eclipse.ui.IImportWizard;
42 import org.eclipse.ui.IWorkbench;
43 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ltk.core.refactoring.RefactoringCore;
46 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
47 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
48 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration;
50 import org.eclipse.jdt.core.IClasspathEntry;
51 import org.eclipse.jdt.core.IJavaProject;
52 import org.eclipse.jdt.core.IPackageFragmentRoot;
53 import org.eclipse.jdt.core.JavaCore;
54 import org.eclipse.jdt.core.JavaModelException;
55 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor;
57 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
58 import org.eclipse.jdt.internal.ui.JavaPlugin;
59 import org.eclipse.jdt.internal.ui.JavaPluginImages;
60 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil;
61 import org.eclipse.jdt.internal.ui.refactoring.actions.MigrateJarFileAction;
62 import org.eclipse.jdt.internal.ui.refactoring.binary.BinaryRefactoringHistoryWizard;
65 * Import wizard to import a refactoring-aware Java Archive (JAR) file.
67 * This class may be instantiated and used without further configuration; this
68 * class is not intended to be subclassed.
74 * IWizard wizard= new JarImportWizard();
75 * wizard.init(workbench, selection);
76 * WizardDialog dialog= new WizardDialog(shell, wizard);
80 * During the call to <code>open</code>, the wizard dialog is presented to
81 * the user. When the user hits Finish, the user-selected JAR file is inspected
82 * for associated refactorings, the wizard executes eventual refactorings,
83 * copies the JAR file over its old version, the dialog closes, and the call to
84 * <code>open</code> returns.
89 public final class JarImportWizard extends BinaryRefactoringHistoryWizard implements IImportWizard {
91 /** Proxy which requests the refactoring history from the import data */
92 private final class RefactoringHistoryProxy extends RefactoringHistory {
94 /** The cached refactoring history delta */
95 private RefactoringDescriptorProxy[] fHistoryDelta= null;
101 public RefactoringDescriptorProxy[] getDescriptors() {
102 if (fHistoryDelta != null)
103 return fHistoryDelta;
104 final RefactoringHistory incoming= fImportData.getRefactoringHistory();
105 if (incoming != null) {
106 fHistoryDelta= incoming.getDescriptors();
107 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot();
110 final URI uri= getLocationURI(root.getRawClasspathEntry());
112 final File file= new File(uri);
116 zip= new ZipFile(file, ZipFile.OPEN_READ);
117 ZipEntry entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry());
119 InputStream stream= null;
121 stream= zip.getInputStream(entry);
122 final RefactoringHistory existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING);
123 if (existing != null)
124 fHistoryDelta= incoming.removeAll(existing).getDescriptors();
126 if (stream != null) {
129 } catch (IOException exception) {
135 } catch (IOException exception) {
138 } catch(IOException e){
143 } catch (CoreException exception) {
144 JavaPlugin.log(exception);
147 return fHistoryDelta;
149 return new RefactoringDescriptorProxy[0];
156 public boolean isEmpty() {
157 final RefactoringDescriptorProxy[] proxies= getDescriptors();
159 return proxies.length == 0;
167 public RefactoringHistory removeAll(final RefactoringHistory history) {
168 throw new UnsupportedOperationException();
172 /** The dialog settings key */
173 private static String DIALOG_SETTINGS_KEY= "JarImportWizard"; //$NON-NLS-1$
176 * Is the specified class path entry pointing to a valid location for
180 * the class path entry
181 * @return <code>true</code> if it is a valid package fragment root,
182 * <code>false</code> otherwise
184 public static boolean isValidClassPathEntry(final IClasspathEntry entry) {
185 Assert.isNotNull(entry);
186 final int kind= entry.getEntryKind();
187 if (kind == IClasspathEntry.CPE_LIBRARY)
188 return entry.getContentKind() == IPackageFragmentRoot.K_BINARY;
189 else if (kind == IClasspathEntry.CPE_VARIABLE)
190 return true; // be optimistic
195 * Is the specified java project a valid project for import?
200 * returns <code>true</code> if the project is valid
201 * @throws JavaModelException
204 public static boolean isValidJavaProject(final IJavaProject project) throws JavaModelException {
205 Assert.isNotNull(project);
206 return project.getProject().isAccessible();
209 /** The refactoring history proxy */
210 private final RefactoringHistoryProxy fHistoryProxy;
212 /** The jar import data */
213 final JarImportData fImportData= new JarImportData();
215 /** The jar import page, or <code>null</code> */
216 private JarImportWizardPage fImportPage= null;
218 /** Is the wizard part of an import wizard? */
219 private boolean fImportWizard= true;
221 /** Has the wizard new dialog settings? */
222 private boolean fNewSettings;
225 * Creates a new jar import wizard.
227 public JarImportWizard() {
228 super(JarImportMessages.JarImportWizard_window_title, JarImportMessages.RefactoringImportPreviewPage_title, JarImportMessages.RefactoringImportPreviewPage_description);
229 fImportData.generated_5455369633822607821();
230 fHistoryProxy= new RefactoringHistoryProxy();
231 setInput(fHistoryProxy);
232 final IDialogSettings section= JavaPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS_KEY);
237 setDialogSettings(section);
239 setConfiguration(new RefactoringHistoryControlConfiguration(null, false, false) {
242 public String getProjectPattern() {
243 return JarImportMessages.JarImportWizard_project_pattern;
247 public String getWorkspaceCaption() {
248 return JarImportMessages.JarImportWizard_workspace_caption;
251 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_REPLACE_JAR);
255 * Creates a new jar import wizard.
258 * <code>true</code> if the wizard is part of an import wizard,
259 * <code>false</code> otherwise
261 public JarImportWizard(final boolean wizard) {
263 fImportWizard= wizard;
264 setWindowTitle(JarImportMessages.JarImportWizard_replace_title);
271 protected void addUserDefinedPages() {
272 fImportPage= new JarImportWizardPage(this, fImportWizard);
273 addPage(fImportPage);
280 public boolean canFinish() {
281 return super.canFinish() && fImportData.getPackageFragmentRoot() != null && fImportData.getRefactoringFileLocation() != null;
288 protected boolean deconfigureClasspath(final IClasspathEntry[] entries, final IProgressMonitor monitor) throws CoreException {
289 final boolean rename= fImportData.isRenameJarFile();
290 if (rename && !fCancelled) {
291 final IPackageFragmentRoot root= getPackageFragmentRoot();
293 final IClasspathEntry entry= root.getRawClasspathEntry();
294 for (int index= 0; index < entries.length; index++) {
295 if (entries[index].equals(entry)) {
296 final IPath path= getTargetPath(entries[index]);
298 entries[index]= JavaCore.newLibraryEntry(path, entries[index].getSourceAttachmentPath(), entries[index].getSourceAttachmentRootPath(), entries[index].getAccessRules(), entries[index].getExtraAttributes(), entries[index].isExported());
304 replaceJarFile(new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
309 * Returns the jar import data.
311 * @return the jar import data
313 public JarImportData getImportData() {
321 public IWizardPage getNextPage(final IWizardPage page) {
322 if (page == fImportPage && fImportData.getRefactoringHistory() == null)
324 return super.getNextPage(page);
331 protected IPackageFragmentRoot getPackageFragmentRoot() {
332 return fImportData.getPackageFragmentRoot();
339 protected RefactoringHistory getRefactoringHistory() {
340 return fHistoryProxy;
344 * Returns the target path to be used for the updated classpath entry.
347 * the classpath entry
348 * @return the target path, or <code>null</code>
349 * @throws CoreException
352 private IPath getTargetPath(final IClasspathEntry entry) throws CoreException {
353 final URI location= getLocationURI(entry);
354 if (location != null) {
355 final URI target= getTargetURI(location);
356 if (target != null) {
357 IPath path= URIUtil.toPath(target);
359 final IPath workspace= ResourcesPlugin.getWorkspace().getRoot().getLocation();
360 if (workspace.isPrefixOf(path)) {
361 path= path.removeFirstSegments(workspace.segmentCount());
362 path= path.setDevice(null);
363 path= path.makeAbsolute();
373 * Returns the target uri taking any renaming of the jar file into account.
377 * @return the target uri
378 * @throws CoreException
381 private URI getTargetURI(final URI uri) throws CoreException {
382 final IFileStore parent= EFS.getStore(uri).getParent();
383 if (parent != null) {
384 final URI location= fImportData.getRefactoringFileLocation();
385 if (location != null)
386 return parent.getChild(EFS.getStore(location).getName()).toURI();
394 public void init(final IWorkbench workbench, final IStructuredSelection selection) {
395 if (selection != null && selection.size() == 1) {
396 final Object element= selection.getFirstElement();
397 if (element instanceof IPackageFragmentRoot) {
398 final IPackageFragmentRoot root= (IPackageFragmentRoot) element;
400 final IClasspathEntry entry= root.getRawClasspathEntry();
401 if (isValidClassPathEntry(entry)
402 && root.getResolvedClasspathEntry().getReferencingEntry() == null)
403 fImportData.setPackageFragmentRoot(root);
404 } catch (JavaModelException exception) {
405 JavaPlugin.log(exception);
415 public boolean performFinish() {
417 final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings();
418 IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY);
419 section= settings.addNewSection(DIALOG_SETTINGS_KEY);
420 setDialogSettings(section);
422 fImportPage.performFinish();
423 return super.performFinish();
427 * Replaces the old jar file with the new one.
430 * the progress monitor to use
431 * @throws CoreException
434 private void replaceJarFile(final IProgressMonitor monitor) throws CoreException {
436 monitor.beginTask(JarImportMessages.JarImportWizard_cleanup_import, 250);
437 final URI location= fImportData.getRefactoringFileLocation();
438 if (location != null) {
439 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot();
441 final URI uri= getLocationURI(root.getRawClasspathEntry());
443 final IFileStore store= EFS.getStore(location);
444 if (fImportData.isRenameJarFile()) {
445 final URI target= getTargetURI(uri);
446 store.copy(EFS.getStore(target), EFS.OVERWRITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
447 if (!uri.equals(target))
448 EFS.getStore(uri).delete(EFS.NONE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
450 store.copy(EFS.getStore(uri), EFS.OVERWRITE, new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
451 if (fJavaProject != null)
452 fJavaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
457 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, JarImportMessages.JarImportWizard_error_copying_jar, null));
463 public WizardDialog generated_7221379870203952480(MigrateJarFileAction migratejarfileaction) {
464 final ISelection selection= migratejarfileaction.fWindow.getSelectionService().getSelection();
465 if (selection instanceof IStructuredSelection)
466 init(migratejarfileaction.fWindow.getWorkbench(), (IStructuredSelection) selection);
467 final WizardDialog dialog= new WizardDialog(migratejarfileaction.fWindow.getShell(), this);
469 dialog.getShell().setSize(Math.max(MigrateJarFileAction.SIZING_WIZARD_WIDTH, dialog.getShell().getSize().x), MigrateJarFileAction.SIZING_WIZARD_HEIGHT);
470 PlatformUI.getWorkbench().getHelpSystem().setHelp(dialog.getShell(), IJavaHelpContextIds.JARIMPORT_WIZARD_PAGE);