1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.text.edits.UndoEdit; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.core.runtime.SubProgressMonitor; 20 21 import org.eclipse.core.resources.IFile; 22 23 import org.eclipse.jdt.core.ICompilationUnit; 24 25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 26 import org.eclipse.jdt.internal.corext.util.Messages; 27 28 import org.eclipse.jdt.internal.ui.JavaPlugin; 29 30 import org.eclipse.ltk.core.refactoring.Change; 31 import org.eclipse.ltk.core.refactoring.ContentStamp; 32 import org.eclipse.ltk.core.refactoring.UndoTextFileChange; 33 34 class UndoCompilationUnitChange extends UndoTextFileChange { 35 36 private ICompilationUnit fCUnit; 37 38 public UndoCompilationUnitChange(String name, ICompilationUnit unit, UndoEdit undo, ContentStamp stampToRestore, int saveMode) throws CoreException { 39 super(name, getFile(unit), undo, stampToRestore, saveMode); 40 fCUnit= unit; 41 } 42 43 private static IFile getFile(ICompilationUnit cunit) throws CoreException { 44 IFile file= (IFile)cunit.getResource(); 45 if (file == null) 46 throw new CoreException(new Status( 47 IStatus.ERROR, 48 JavaPlugin.getPluginId(), 49 IStatus.ERROR, 50 Messages.format( 51 RefactoringCoreMessages.UndoCompilationUnitChange_no_resource, 52 cunit.getElementName()), 53 null) 54 ); 55 return file; 56 } 57 58 61 public Object getModifiedElement() { 62 return fCUnit; 63 } 64 65 68 protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) throws CoreException { 69 return new UndoCompilationUnitChange(getName(), fCUnit, edit, stampToRestore, getSaveMode()); 70 } 71 72 75 public Change perform(IProgressMonitor pm) throws CoreException { 76 pm.beginTask("", 2); fCUnit.becomeWorkingCopy(null, new SubProgressMonitor(pm,1)); 78 try { 79 return super.perform(new SubProgressMonitor(pm,1)); 80 } finally { 81 fCUnit.discardWorkingCopy(); 82 } 83 } 84 } 85 | Popular Tags |