1 11 package org.eclipse.jdt.internal.ui.jarimport; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.URI ; 17 import java.util.zip.ZipEntry ; 18 import java.util.zip.ZipFile ; 19 20 import org.eclipse.core.filesystem.EFS; 21 import org.eclipse.core.filesystem.IFileStore; 22 import org.eclipse.core.filesystem.URIUtil; 23 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; 31 32 import org.eclipse.core.resources.IResource; 33 import org.eclipse.core.resources.ResourcesPlugin; 34 35 import org.eclipse.jface.dialogs.IDialogSettings; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.wizard.IWizardPage; 38 39 import org.eclipse.ui.IImportWizard; 40 import org.eclipse.ui.IWorkbench; 41 42 import org.eclipse.ltk.core.refactoring.RefactoringCore; 43 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 44 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory; 45 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration; 46 47 import org.eclipse.jdt.core.IClasspathEntry; 48 import org.eclipse.jdt.core.IJavaProject; 49 import org.eclipse.jdt.core.IPackageFragmentRoot; 50 import org.eclipse.jdt.core.JavaCore; 51 import org.eclipse.jdt.core.JavaModelException; 52 import org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor; 53 54 55 import org.eclipse.jdt.internal.ui.JavaPlugin; 56 import org.eclipse.jdt.internal.ui.JavaPluginImages; 57 import org.eclipse.jdt.internal.ui.jarpackager.JarPackagerUtil; 58 import org.eclipse.jdt.internal.ui.refactoring.binary.BinaryRefactoringHistoryWizard; 59 60 85 public final class JarImportWizard extends BinaryRefactoringHistoryWizard implements IImportWizard { 86 87 88 private final class RefactoringHistoryProxy extends RefactoringHistory { 89 90 91 private RefactoringDescriptorProxy[] fHistoryDelta= null; 92 93 96 public RefactoringDescriptorProxy[] getDescriptors() { 97 if (fHistoryDelta != null) 98 return fHistoryDelta; 99 final RefactoringHistory incoming= fImportData.getRefactoringHistory(); 100 if (incoming != null) { 101 fHistoryDelta= incoming.getDescriptors(); 102 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot(); 103 if (root != null) { 104 try { 105 final URI uri= getLocationURI(root.getRawClasspathEntry()); 106 if (uri != null) { 107 final File file= new File (uri); 108 if (file.exists()) { 109 ZipFile zip= null; 110 try { 111 zip= new ZipFile (file, ZipFile.OPEN_READ); 112 ZipEntry entry= zip.getEntry(JarPackagerUtil.getRefactoringsEntry()); 113 if (entry != null) { 114 InputStream stream= null; 115 try { 116 stream= zip.getInputStream(entry); 117 final RefactoringHistory existing= RefactoringCore.getHistoryService().readRefactoringHistory(stream, JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING); 118 if (existing != null) 119 fHistoryDelta= incoming.removeAll(existing).getDescriptors(); 120 } finally { 121 if (stream != null) { 122 try { 123 stream.close(); 124 } catch (IOException exception) { 125 } 127 } 128 } 129 } 130 } catch (IOException exception) { 131 } 133 } 134 } 135 } catch (CoreException exception) { 136 JavaPlugin.log(exception); 137 } 138 } 139 return fHistoryDelta; 140 } 141 return new RefactoringDescriptorProxy[0]; 142 } 143 144 147 public boolean isEmpty() { 148 final RefactoringDescriptorProxy[] proxies= getDescriptors(); 149 if (proxies != null) 150 return proxies.length == 0; 151 return true; 152 } 153 154 157 public RefactoringHistory removeAll(final RefactoringHistory history) { 158 throw new UnsupportedOperationException (); 159 } 160 } 161 162 163 private static String DIALOG_SETTINGS_KEY= "JarImportWizard"; 165 174 public static boolean isValidClassPathEntry(final IClasspathEntry entry) { 175 Assert.isNotNull(entry); 176 final int kind= entry.getEntryKind(); 177 if (kind == IClasspathEntry.CPE_LIBRARY) 178 return entry.getContentKind() == IPackageFragmentRoot.K_BINARY; 179 else if (kind == IClasspathEntry.CPE_VARIABLE) 180 return true; return false; 182 } 183 184 192 public static boolean isValidJavaProject(final IJavaProject project) throws JavaModelException { 193 Assert.isNotNull(project); 194 return project.getProject().isAccessible(); 195 } 196 197 198 private final RefactoringHistoryProxy fHistoryProxy; 199 200 201 private final JarImportData fImportData= new JarImportData(); 202 203 204 private JarImportWizardPage fImportPage= null; 205 206 207 private boolean fImportWizard= true; 208 209 210 private boolean fNewSettings; 211 212 215 public JarImportWizard() { 216 super(JarImportMessages.JarImportWizard_window_title, JarImportMessages.RefactoringImportPreviewPage_title, JarImportMessages.RefactoringImportPreviewPage_description); 217 fImportData.setRefactoringAware(true); 218 fImportData.setIncludeDirectoryEntries(true); 219 fHistoryProxy= new RefactoringHistoryProxy(); 220 setInput(fHistoryProxy); 221 final IDialogSettings section= JavaPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS_KEY); 222 if (section == null) 223 fNewSettings= true; 224 else { 225 fNewSettings= false; 226 setDialogSettings(section); 227 } 228 setConfiguration(new RefactoringHistoryControlConfiguration(null, false, false) { 229 230 public String getProjectPattern() { 231 return JarImportMessages.JarImportWizard_project_pattern; 232 } 233 234 public String getWorkspaceCaption() { 235 return JarImportMessages.JarImportWizard_workspace_caption; 236 } 237 }); 238 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_REPLACE_JAR); 239 } 240 241 248 public JarImportWizard(final boolean wizard) { 249 this(); 250 fImportWizard= wizard; 251 setWindowTitle(JarImportMessages.JarImportWizard_replace_title); 252 } 253 254 257 protected void addUserDefinedPages() { 258 fImportPage= new JarImportWizardPage(this, fImportWizard); 259 addPage(fImportPage); 260 } 261 262 265 public boolean canFinish() { 266 return super.canFinish() && fImportData.getPackageFragmentRoot() != null && fImportData.getRefactoringFileLocation() != null; 267 } 268 269 272 protected boolean deconfigureClasspath(final IClasspathEntry[] entries, final IProgressMonitor monitor) throws CoreException { 273 final boolean rename= fImportData.isRenameJarFile(); 274 if (rename && !fCancelled) { 275 final IPackageFragmentRoot root= getPackageFragmentRoot(); 276 if (root != null) { 277 final IClasspathEntry entry= root.getRawClasspathEntry(); 278 for (int index= 0; index < entries.length; index++) { 279 if (entries[index].equals(entry)) { 280 final IPath path= getTargetPath(entries[index]); 281 if (path != null) 282 entries[index]= JavaCore.newLibraryEntry(path, entries[index].getSourceAttachmentPath(), entries[index].getSourceAttachmentRootPath(), entries[index].getAccessRules(), entries[index].getExtraAttributes(), entries[index].isExported()); 283 } 284 } 285 } 286 } 287 if (!fCancelled) 288 replaceJarFile(new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 289 return rename; 290 } 291 292 297 public JarImportData getImportData() { 298 return fImportData; 299 } 300 301 304 public IWizardPage getNextPage(final IWizardPage page) { 305 if (page == fImportPage && fImportData.getRefactoringHistory() == null) 306 return null; 307 return super.getNextPage(page); 308 } 309 310 313 protected IPackageFragmentRoot getPackageFragmentRoot() { 314 return fImportData.getPackageFragmentRoot(); 315 } 316 317 320 protected RefactoringHistory getRefactoringHistory() { 321 return fHistoryProxy; 322 } 323 324 333 private IPath getTargetPath(final IClasspathEntry entry) throws CoreException { 334 final URI location= getLocationURI(entry); 335 if (location != null) { 336 final URI target= getTargetURI(location); 337 if (target != null) { 338 IPath path= URIUtil.toPath(target); 339 if (path != null) { 340 final IPath workspace= ResourcesPlugin.getWorkspace().getRoot().getLocation(); 341 if (workspace.isPrefixOf(path)) { 342 path= path.removeFirstSegments(workspace.segmentCount()); 343 path= path.setDevice(null); 344 path= path.makeAbsolute(); 345 } 346 } 347 return path; 348 } 349 } 350 return null; 351 } 352 353 362 private URI getTargetURI(final URI uri) throws CoreException { 363 final IFileStore parent= EFS.getStore(uri).getParent(); 364 if (parent != null) { 365 final URI location= fImportData.getRefactoringFileLocation(); 366 if (location != null) 367 return parent.getChild(EFS.getStore(location).getName()).toURI(); 368 } 369 return uri; 370 } 371 372 375 public void init(final IWorkbench workbench, final IStructuredSelection selection) { 376 if (selection != null && selection.size() == 1) { 377 final Object element= selection.getFirstElement(); 378 if (element instanceof IPackageFragmentRoot) { 379 final IPackageFragmentRoot root= (IPackageFragmentRoot) element; 380 try { 381 final IClasspathEntry entry= root.getRawClasspathEntry(); 382 if (isValidClassPathEntry(entry)) 383 fImportData.setPackageFragmentRoot(root); 384 } catch (JavaModelException exception) { 385 JavaPlugin.log(exception); 386 } 387 } 388 } 389 } 390 391 394 public boolean performFinish() { 395 if (fNewSettings) { 396 final IDialogSettings settings= JavaPlugin.getDefault().getDialogSettings(); 397 IDialogSettings section= settings.getSection(DIALOG_SETTINGS_KEY); 398 section= settings.addNewSection(DIALOG_SETTINGS_KEY); 399 setDialogSettings(section); 400 } 401 fImportPage.performFinish(); 402 return super.performFinish(); 403 } 404 405 413 private void replaceJarFile(final IProgressMonitor monitor) throws CoreException { 414 try { 415 monitor.beginTask(JarImportMessages.JarImportWizard_cleanup_import, 250); 416 final URI location= fImportData.getRefactoringFileLocation(); 417 if (location != null) { 418 final IPackageFragmentRoot root= fImportData.getPackageFragmentRoot(); 419 if (root != null) { 420 final URI uri= getLocationURI(root.getRawClasspathEntry()); 421 if (uri != null) { 422 final IFileStore store= EFS.getStore(location); 423 if (fImportData.isRenameJarFile()) { 424 final URI target= getTargetURI(uri); 425 store.copy(EFS.getStore(target), EFS.OVERWRITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 426 if (!uri.equals(target)) 427 EFS.getStore(uri).delete(EFS.NONE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 428 } else 429 store.copy(EFS.getStore(uri), EFS.OVERWRITE, new SubProgressMonitor(monitor, 100, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 430 if (fJavaProject != null) 431 fJavaProject.getResource().refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 50, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)); 432 return; 433 } 434 } 435 } 436 throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, JarImportMessages.JarImportWizard_error_copying_jar, null)); 437 } finally { 438 monitor.done(); 439 } 440 } 441 } 442 | Popular Tags |