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 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.ltk.core.refactoring.Change; 21 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 22 23 import org.eclipse.jdt.core.ICompilationUnit; 24 import org.eclipse.jdt.core.JavaModelException; 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 RenameCompilationUnitChange extends AbstractJavaElementRenameChange { 31 32 public RenameCompilationUnitChange(ICompilationUnit unit, String newName) { 33 this(unit.getResource().getFullPath(), unit.getElementName(), newName, IResource.NULL_STAMP); 34 Assert.isTrue(!unit.isReadOnly(), "compilation unit must not be read-only"); } 36 37 private RenameCompilationUnitChange(IPath resourcePath, String oldName, String newName, long stampToRestore) { 38 super(resourcePath, oldName, newName, stampToRestore); 39 } 40 41 protected IPath createNewPath() { 42 final IPath path= getResourcePath(); 43 if (path.getFileExtension() != null) 44 return path.removeFileExtension().removeLastSegments(1).append(getNewName()); 45 else 46 return path.removeLastSegments(1).append(getNewName()); 47 } 48 49 protected Change createUndoChange(long stampToRestore) throws JavaModelException { 50 return new RenameCompilationUnitChange(createNewPath(), getNewName(), getOldName(), stampToRestore); 51 } 52 53 protected void doRename(IProgressMonitor pm) throws CoreException { 54 ICompilationUnit cu= (ICompilationUnit) getModifiedElement(); 55 if (cu != null) 56 cu.rename(getNewName(), false, pm); 57 } 58 59 public String getName() { 60 return Messages.format(RefactoringCoreMessages.RenameCompilationUnitChange_name, new String [] { getOldName(), getNewName()}); 61 } 62 63 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 64 return super.isValid(pm, READ_ONLY | SAVE_IF_DIRTY); 65 } 66 } | Popular Tags |