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.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.SubProgressMonitor; 19 20 import org.eclipse.core.resources.IFile; 21 22 import org.eclipse.jface.text.IDocument; 23 24 import org.eclipse.ltk.core.refactoring.Change; 25 import org.eclipse.ltk.core.refactoring.ChangeDescriptor; 26 import org.eclipse.ltk.core.refactoring.ContentStamp; 27 import org.eclipse.ltk.core.refactoring.TextFileChange; 28 29 import org.eclipse.jdt.core.ICompilationUnit; 30 31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 32 33 import org.eclipse.jdt.internal.ui.JavaPlugin; 34 35 public class CompilationUnitChange extends TextFileChange { 36 37 private final ICompilationUnit fCUnit; 38 39 40 private ChangeDescriptor fDescriptor; 41 42 48 public CompilationUnitChange(String name, ICompilationUnit cunit) { 49 super(name, getFile(cunit)); 50 Assert.isNotNull(cunit); 51 fCUnit= cunit; 52 setTextType("java"); } 54 55 private static IFile getFile(ICompilationUnit cunit) { 56 return (IFile) cunit.getResource(); 57 } 58 59 62 public Object getModifiedElement(){ 63 return fCUnit; 64 } 65 66 71 public ICompilationUnit getCompilationUnit() { 72 return fCUnit; 73 } 74 75 78 protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException { 79 pm.beginTask("", 2); fCUnit.becomeWorkingCopy(null, new SubProgressMonitor(pm, 1)); 81 return super.acquireDocument(new SubProgressMonitor(pm, 1)); 82 } 83 84 87 protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException { 88 boolean isModified= isDocumentModified(); 89 super.releaseDocument(document, pm); 90 try { 91 fCUnit.discardWorkingCopy(); 92 } finally { 93 if (isModified && !isDocumentAcquired()) { 94 if (fCUnit.isWorkingCopy()) 95 JavaModelUtil.reconcile(fCUnit); 96 else 97 fCUnit.makeConsistent(pm); 98 } 99 } 100 } 101 102 105 protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) { 106 try { 107 return new UndoCompilationUnitChange(getName(), fCUnit, edit, stampToRestore, getSaveMode()); 108 } catch (CoreException e) { 109 JavaPlugin.log(e); 110 return null; 111 } 112 } 113 114 117 public Object getAdapter(Class adapter) { 118 if (ICompilationUnit.class.equals(adapter)) 119 return fCUnit; 120 return super.getAdapter(adapter); 121 } 122 123 128 public void setDescriptor(ChangeDescriptor descriptor) { 129 fDescriptor= descriptor; 130 } 131 132 135 public ChangeDescriptor getDescriptor() { 136 return fDescriptor; 137 } 138 } 139 140 | Popular Tags |