1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.OperationCanceledException; 16 17 import org.eclipse.core.resources.IResource; 18 19 import org.eclipse.ltk.core.refactoring.Change; 20 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 21 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IPackageFragment; 24 25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 26 import org.eclipse.jdt.internal.corext.util.Messages; 27 28 public class MoveCompilationUnitChange extends CompilationUnitReorgChange { 29 30 private boolean fUndoable; 31 private long fStampToRestore; 32 33 public MoveCompilationUnitChange(ICompilationUnit cu, IPackageFragment newPackage){ 34 super(cu, newPackage); 35 fStampToRestore= IResource.NULL_STAMP; 36 } 37 38 private MoveCompilationUnitChange(IPackageFragment oldPackage, String cuName, IPackageFragment newPackage, long stampToRestore) { 39 super(oldPackage.getHandleIdentifier(), newPackage.getHandleIdentifier(), oldPackage.getCompilationUnit(cuName).getHandleIdentifier()); 40 fStampToRestore= stampToRestore; 41 } 42 43 public String getName() { 44 return Messages.format(RefactoringCoreMessages.MoveCompilationUnitChange_name, 45 new String []{getCu().getElementName(), getPackageName(getDestinationPackage())}); 46 } 47 48 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 49 return super.isValid(pm, READ_ONLY | SAVE_IF_DIRTY); 50 } 51 52 Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException { 53 String name; 54 String newName= getNewName(); 55 if (newName == null) 56 name= getCu().getElementName(); 57 else 58 name= newName; 59 60 long currentStamp= IResource.NULL_STAMP; 62 IResource resource= getCu().getResource(); 63 if (resource != null) { 64 currentStamp= resource.getModificationStamp(); 65 } 66 67 fUndoable= ! getDestinationPackage().getCompilationUnit(name).exists(); 68 69 getCu().move(getDestinationPackage(), null, newName, true, pm); 71 if (fStampToRestore != IResource.NULL_STAMP) { 72 ICompilationUnit moved= getDestinationPackage().getCompilationUnit(name); 73 IResource movedResource= moved.getResource(); 74 if (movedResource != null) { 75 movedResource.revertModificationStamp(fStampToRestore); 76 } 77 } 78 79 if (fUndoable) { 80 return new MoveCompilationUnitChange(getDestinationPackage(), getCu().getElementName(), getOldPackage(), currentStamp); 81 } else { 82 return null; 83 } 84 } 85 } 86 | Popular Tags |