1 11 package org.eclipse.jdt.internal.corext.refactoring; 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 import org.eclipse.core.resources.ResourcesPlugin; 21 22 import org.eclipse.ltk.core.refactoring.Change; 23 24 import org.eclipse.jdt.core.JavaCore; 25 26 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange; 27 28 public abstract class AbstractJavaElementRenameChange extends JDTChange { 29 30 private final String fNewName; 31 32 private final String fOldName; 33 34 private final IPath fResourcePath; 35 36 private final long fStampToRestore; 37 38 protected AbstractJavaElementRenameChange(IPath resourcePath, String oldName, String newName) { 39 this(resourcePath, oldName, newName, IResource.NULL_STAMP); 40 } 41 42 protected AbstractJavaElementRenameChange(IPath resourcePath, String oldName, String newName, long stampToRestore) { 43 Assert.isNotNull(newName, "new name"); Assert.isNotNull(oldName, "old name"); fResourcePath= resourcePath; 46 fOldName= oldName; 47 fNewName= newName; 48 fStampToRestore= stampToRestore; 49 } 50 51 protected abstract IPath createNewPath(); 52 53 protected abstract Change createUndoChange(long stampToRestore) throws CoreException; 54 55 protected abstract void doRename(IProgressMonitor pm) throws CoreException; 56 57 public Object getModifiedElement() { 58 return JavaCore.create(getResource()); 59 } 60 61 public String getNewName() { 62 return fNewName; 63 } 64 65 public String getOldName() { 66 return fOldName; 67 } 68 69 protected final IResource getResource() { 70 return ResourcesPlugin.getWorkspace().getRoot().findMember(fResourcePath); 71 } 72 73 protected IPath getResourcePath() { 74 return fResourcePath; 75 } 76 77 public final Change perform(IProgressMonitor pm) throws CoreException { 78 try { 79 pm.beginTask(RefactoringCoreMessages.AbstractRenameChange_Renaming, 1); 80 IResource resource= getResource(); 81 IPath newPath= createNewPath(); 82 Change result= createUndoChange(resource.getModificationStamp()); 83 doRename(new SubProgressMonitor(pm, 1)); 84 if (fStampToRestore != IResource.NULL_STAMP) { 85 IResource newResource= ResourcesPlugin.getWorkspace().getRoot().findMember(newPath); 86 newResource.revertModificationStamp(fStampToRestore); 87 } 88 return result; 89 } finally { 90 pm.done(); 91 } 92 } 93 } | Popular Tags |