1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.SubProgressMonitor; 18 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.ltk.core.refactoring.Change; 22 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 23 24 import org.eclipse.jdt.core.IPackageFragmentRoot; 25 26 import org.eclipse.jdt.internal.corext.refactoring.AbstractJavaElementRenameChange; 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 28 import org.eclipse.jdt.internal.corext.util.Messages; 29 30 public final class RenameSourceFolderChange extends AbstractJavaElementRenameChange { 31 32 private static RefactoringStatus checkIfModifiable(IPackageFragmentRoot root, IProgressMonitor pm) throws CoreException { 33 RefactoringStatus result= new RefactoringStatus(); 34 checkExistence(result, root); 35 if (result.hasFatalError()) 36 return result; 37 38 if (root.isArchive()) { 39 result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_archive, root.getElementName())); 40 return result; 41 } 42 43 if (root.isExternal()) { 44 result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_external, root.getElementName())); 45 return result; 46 } 47 48 checkExistence(result, root.getCorrespondingResource()); 49 if (result.hasFatalError()) 50 return result; 51 52 if (root.getCorrespondingResource().isLinked()) { 53 result.addFatalError(Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename_linked, root.getElementName())); 54 return result; 55 } 56 57 return result; 58 } 59 60 public RenameSourceFolderChange(IPackageFragmentRoot sourceFolder, String newName) { 61 this(sourceFolder.getPath(), sourceFolder.getElementName(), newName, IResource.NULL_STAMP); 62 Assert.isTrue(!sourceFolder.isReadOnly(), "should not be read only"); Assert.isTrue(!sourceFolder.isArchive(), "should not be an archive"); } 65 66 private RenameSourceFolderChange(IPath resourcePath, String oldName, String newName, long stampToRestore) { 67 super(resourcePath, oldName, newName); 68 } 69 70 protected IPath createNewPath() { 71 return getResourcePath().removeLastSegments(1).append(getNewName()); 72 } 73 74 protected Change createUndoChange(long stampToRestore) { 75 return new RenameSourceFolderChange(createNewPath(), getNewName(), getOldName(), stampToRestore); 76 } 77 78 protected void doRename(IProgressMonitor pm) throws CoreException { 79 IPackageFragmentRoot sourceFolder= getSourceFolder(); 80 if (sourceFolder != null) 81 sourceFolder.move(getNewPath(), getCoreMoveFlags(), getJavaModelUpdateFlags(), null, pm); 82 } 83 84 private int getCoreMoveFlags() { 85 if (getResource().isLinked()) 86 return IResource.SHALLOW; 87 else 88 return IResource.NONE; 89 } 90 91 private int getJavaModelUpdateFlags() { 92 return IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH | IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH | IPackageFragmentRoot.REPLACE; 93 } 94 95 public String getName() { 96 return Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_rename, new String [] { getOldName(), getNewName()}); 97 } 98 99 private IPath getNewPath() { 100 return getResource().getFullPath().removeLastSegments(1).append(getNewName()); 101 } 102 103 private IPackageFragmentRoot getSourceFolder() { 104 return (IPackageFragmentRoot) getModifiedElement(); 105 } 106 107 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 108 RefactoringStatus result= new RefactoringStatus(); 109 pm.beginTask("", 2); result.merge(isValid(new SubProgressMonitor(pm, 1), DIRTY)); 111 if (result.hasFatalError()) 112 return result; 113 IPackageFragmentRoot sourceFolder= getSourceFolder(); 114 result.merge(checkIfModifiable(sourceFolder, new SubProgressMonitor(pm, 1))); 115 116 return result; 117 } 118 } 119 | Popular Tags |