1 11 package org.eclipse.ltk.internal.core.refactoring; 12 13 import java.util.LinkedList ; 14 15 import org.eclipse.text.edits.TextEdit; 16 import org.eclipse.text.edits.UndoEdit; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.NullProgressMonitor; 22 import org.eclipse.core.runtime.SubProgressMonitor; 23 24 import org.eclipse.core.filebuffers.FileBuffers; 25 import org.eclipse.core.filebuffers.ITextFileBuffer; 26 import org.eclipse.core.filebuffers.ITextFileBufferManager; 27 import org.eclipse.core.filebuffers.LocationKind; 28 29 import org.eclipse.core.resources.IFile; 30 31 import org.eclipse.jface.text.BadLocationException; 32 import org.eclipse.jface.text.IDocument; 33 34 import org.eclipse.ltk.core.refactoring.Change; 35 import org.eclipse.ltk.core.refactoring.ContentStamp; 36 import org.eclipse.ltk.core.refactoring.NullChange; 37 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 38 import org.eclipse.ltk.core.refactoring.TextFileChange; 39 40 54 public class MultiStateUndoChange extends Change { 55 56 private ContentStamp fContentStampToRestore; 57 58 private boolean fDirty; 59 60 private IFile fFile; 61 62 private String fName; 63 64 private int fSaveMode; 65 66 private UndoEdit[] fUndos; 67 68 private BufferValidationState fValidationState; 69 70 88 public MultiStateUndoChange(String name, IFile file, UndoEdit[] undos, ContentStamp stamp, int saveMode) { 89 Assert.isNotNull(name); 90 Assert.isNotNull(file); 91 Assert.isNotNull(undos); 92 fName= name; 93 fFile= file; 94 fUndos= undos; 95 fContentStampToRestore= stamp; 96 fSaveMode= saveMode; 97 } 98 99 117 protected Change createUndoChange(UndoEdit[] edits, ContentStamp stampToRestore) throws CoreException { 118 return new MultiStateUndoChange(getName(), fFile, edits, stampToRestore, fSaveMode); 119 } 120 121 124 public void dispose() { 125 fValidationState.dispose(); 126 } 127 128 131 public final Object [] getAffectedObjects() { 132 Object modifiedElement= getModifiedElement(); 133 if (modifiedElement == null) 134 return null; 135 return new Object [] { modifiedElement}; 136 } 137 138 141 public final Object getModifiedElement() { 142 return fFile; 143 } 144 145 148 public final String getName() { 149 return fName; 150 } 151 152 161 public final int getSaveMode() { 162 return fSaveMode; 163 } 164 165 168 public void initializeValidationData(IProgressMonitor pm) { 169 if (pm == null) 170 pm= new NullProgressMonitor(); 171 pm.beginTask("", 1); fValidationState= BufferValidationState.create(fFile); 173 pm.worked(1); 174 } 175 176 179 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 180 if (pm == null) 181 pm= new NullProgressMonitor(); 182 pm.beginTask("", 1); ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE); 184 fDirty= buffer != null && buffer.isDirty(); 185 RefactoringStatus result= fValidationState.isValid(needsSaving(), true); 186 pm.worked(1); 187 return result; 188 } 189 190 public final boolean needsSaving() { 191 return (fSaveMode & TextFileChange.FORCE_SAVE) != 0 || (!fDirty && (fSaveMode & TextFileChange.KEEP_SAVE_STATE) != 0); 192 } 193 194 197 public Change perform(IProgressMonitor pm) throws CoreException { 198 if (fValidationState.isValid(needsSaving(), false).hasFatalError()) 199 return new NullChange(); 200 if (pm == null) 201 pm= new NullProgressMonitor(); 202 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 203 pm.beginTask("", 2); ITextFileBuffer buffer= null; 205 try { 206 manager.connect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1)); 207 buffer= manager.getTextFileBuffer(fFile.getFullPath(), LocationKind.IFILE); 208 IDocument document= buffer.getDocument(); 209 ContentStamp currentStamp= ContentStamps.get(fFile, document); 210 LinkedList list= new LinkedList (); 212 for (int index= 0; index < fUndos.length; index++) { 213 UndoEdit edit= fUndos[index]; 214 UndoEdit redo= edit.apply(document, TextEdit.CREATE_UNDO); 215 list.addFirst(redo); 216 217 } 218 219 boolean success= ContentStamps.set(document, fContentStampToRestore); 221 if (needsSaving()) { 222 buffer.commit(pm, false); 223 if (!success) { 224 ContentStamps.set(fFile, fContentStampToRestore); 227 } 228 } 229 return createUndoChange((UndoEdit[]) list.toArray(new UndoEdit[list.size()]), currentStamp); 230 } catch (BadLocationException e) { 231 throw Changes.asCoreException(e); 232 } finally { 233 if (buffer != null) 234 manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, new SubProgressMonitor(pm, 1)); 235 } 236 } 237 } 238 | Popular Tags |