1 11 package org.eclipse.ltk.core.refactoring; 12 13 import org.eclipse.text.edits.MalformedTreeException; 14 import org.eclipse.text.edits.TextEdit; 15 import org.eclipse.text.edits.UndoEdit; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 import org.eclipse.core.runtime.SubProgressMonitor; 22 23 import org.eclipse.core.filebuffers.FileBuffers; 24 import org.eclipse.core.filebuffers.ITextFileBuffer; 25 import org.eclipse.core.filebuffers.ITextFileBufferManager; 26 import org.eclipse.core.filebuffers.LocationKind; 27 28 import org.eclipse.core.resources.IFile; 29 30 import org.eclipse.jface.text.BadLocationException; 31 import org.eclipse.jface.text.IDocument; 32 33 import org.eclipse.ltk.internal.core.refactoring.BufferValidationState; 34 import org.eclipse.ltk.internal.core.refactoring.Changes; 35 import org.eclipse.ltk.internal.core.refactoring.ContentStamps; 36 37 49 public class UndoTextFileChange extends Change { 50 51 private String fName; 52 private UndoEdit fUndo; 53 private IFile fFile; 54 private ContentStamp fContentStampToRestore; 55 private int fSaveMode; 56 57 private boolean fDirty; 58 private BufferValidationState fValidationState; 59 60 73 protected UndoTextFileChange(String name, IFile file, UndoEdit undo, ContentStamp stamp, int saveMode) { 74 Assert.isNotNull(name); 75 Assert.isNotNull(file); 76 Assert.isNotNull(undo); 77 fName= name; 78 fFile= file; 79 fUndo= undo; 80 fContentStampToRestore= stamp; 81 fSaveMode= saveMode; 82 } 83 84 93 public int getSaveMode() { 94 return fSaveMode; 95 } 96 97 100 public String getName() { 101 return fName; 102 } 103 104 119 protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) throws CoreException { 120 return new UndoTextFileChange(getName(), fFile, edit, stampToRestore, fSaveMode); 121 } 122 123 126 public Object getModifiedElement() { 127 return fFile; 128 } 129 130 133 public Object [] getAffectedObjects() { 134 Object modifiedElement= getModifiedElement(); 135 if (modifiedElement == null) 136 return null; 137 return new Object [] { modifiedElement }; 138 } 139 140 143 public void initializeValidationData(IProgressMonitor pm) { 144 if (pm == null) 145 pm= new NullProgressMonitor(); 146 pm.beginTask("", 1); fValidationState= BufferValidationState.create(fFile); 148 pm.worked(1); 149 } 150 151 154 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 155 if (pm == null) 156 pm= new NullProgressMonitor(); 157 pm.beginTask("", 1); ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE); 159 fDirty= buffer != null && buffer.isDirty(); 160 RefactoringStatus result= fValidationState.isValid(needsSaving(), true); 161 pm.worked(1); 162 return result; 163 } 164 165 168 public Change perform(IProgressMonitor pm) throws CoreException { 169 if (pm == null) 170 pm= new NullProgressMonitor(); 171 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 172 pm.beginTask("", 2); ITextFileBuffer buffer= null; 174 try { 175 manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1)); 176 buffer= manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE); 177 IDocument document= buffer.getDocument(); 178 ContentStamp currentStamp= ContentStamps.get(fFile, document); 179 UndoEdit redo= fUndo.apply(document, TextEdit.CREATE_UNDO); 181 boolean success= ContentStamps.set(document, fContentStampToRestore); 183 if (needsSaving()) { 184 buffer.commit(pm, false); 185 if (!success) { 186 ContentStamps.set(fFile, fContentStampToRestore); 189 } 190 } 191 return createUndoChange(redo, currentStamp); 192 } catch (BadLocationException e) { 193 if (! fValidationState.wasDerived()) 194 throw Changes.asCoreException(e); 195 else 196 return new NullChange(); 197 } catch (MalformedTreeException e) { 198 if (! fValidationState.wasDerived()) 199 throw Changes.asCoreException(e); 200 else 201 return new NullChange(); 202 } catch (CoreException e) { 203 if (! fValidationState.wasDerived()) 204 throw e; 205 else 206 return new NullChange(); 207 } finally { 208 if (buffer != null) 209 manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1)); 210 } 211 } 212 213 216 public void dispose() { 217 fValidationState.dispose(); 218 } 219 220 private boolean needsSaving() { 221 return (fSaveMode & TextFileChange.FORCE_SAVE) != 0 || (!fDirty && (fSaveMode & TextFileChange.KEEP_SAVE_STATE) != 0); 222 } 223 } 224 | Popular Tags |