1 11 package org.eclipse.ltk.core.refactoring; 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.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 20 import org.eclipse.core.filebuffers.FileBuffers; 21 import org.eclipse.core.filebuffers.ITextFileBuffer; 22 import org.eclipse.core.filebuffers.ITextFileBufferManager; 23 import org.eclipse.core.filebuffers.LocationKind; 24 25 import org.eclipse.core.resources.IFile; 26 27 import org.eclipse.jface.text.IDocument; 28 29 import org.eclipse.ltk.internal.core.refactoring.BufferValidationState; 30 import org.eclipse.ltk.internal.core.refactoring.Changes; 31 import org.eclipse.ltk.internal.core.refactoring.ContentStamps; 32 33 47 public class TextFileChange extends TextChange { 48 49 54 public static final int KEEP_SAVE_STATE= 1 << 0; 55 56 59 public static final int FORCE_SAVE= 1 << 1; 60 61 64 public static final int LEAVE_DIRTY= 1 << 2; 65 66 67 private IFile fFile; 69 private int fSaveMode= KEEP_SAVE_STATE; 70 71 private int fAcquireCount; 73 private ITextFileBuffer fBuffer; 74 private BufferValidationState fValidationState; 75 private ContentStamp fContentStamp; 76 77 83 public TextFileChange(String name, IFile file) { 84 super(name); 85 Assert.isNotNull(file); 86 fFile= file; 87 } 88 89 96 public void setSaveMode(int saveMode) { 97 fSaveMode= saveMode; 98 } 99 100 105 public int getSaveMode() { 106 return fSaveMode; 107 } 108 109 114 public IFile getFile() { 115 return fFile; 116 } 117 118 133 protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) { 134 return new UndoTextFileChange(getName(), fFile, edit, stampToRestore, fSaveMode); 135 } 136 137 140 public Object getModifiedElement(){ 141 return fFile; 142 } 143 144 public Object [] getAffectedObjects() { 145 Object modifiedElement= getModifiedElement(); 146 if (modifiedElement == null) 147 return null; 148 return new Object [] { modifiedElement }; 149 } 150 151 154 public void initializeValidationData(IProgressMonitor monitor) { 155 try { 156 monitor.beginTask("", 1); fValidationState= BufferValidationState.create(fFile); 158 } finally { 159 monitor.done(); 160 } 161 } 162 163 166 public RefactoringStatus isValid(IProgressMonitor monitor) throws CoreException { 167 try { 168 monitor.beginTask("", 1); boolean needsSaving= needsSaving(); 170 RefactoringStatus result= fValidationState.isValid(needsSaving); 171 if (needsSaving) { 172 result.merge(Changes.validateModifiesFiles(new IFile[] { fFile})); 173 } else { 174 result.merge(Changes.checkInSync(new IFile[] { fFile})); 176 } 177 return result; 178 } finally { 179 monitor.done(); 180 } 181 } 182 183 186 public void dispose() { 187 fValidationState.dispose(); 188 } 189 190 193 protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException { 194 fAcquireCount++; 195 if (fAcquireCount > 1) 196 return fBuffer.getDocument(); 197 198 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 199 IPath path= fFile.getFullPath(); 200 manager.connect(path, LocationKind.IFILE, pm); 201 fBuffer= manager.getTextFileBuffer(path, LocationKind.IFILE); 202 IDocument result= fBuffer.getDocument(); 203 fContentStamp= ContentStamps.get(fFile, result); 204 return result; 205 } 206 207 214 protected void commit(IDocument document, IProgressMonitor pm) throws CoreException { 215 if (needsSaving()) { 216 fBuffer.commit(pm, false); 217 } 218 } 219 220 223 protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException { 224 Assert.isTrue(fAcquireCount > 0); 225 if (fAcquireCount == 1) { 226 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 227 manager.disconnect(fFile.getFullPath(), LocationKind.IFILE, pm); 228 } 229 fAcquireCount--; 230 } 231 232 235 protected final Change createUndoChange(UndoEdit edit) { 236 return createUndoChange(edit, fContentStamp); 237 } 238 239 246 protected boolean isDocumentAcquired() { 247 return fAcquireCount > 0; 248 } 249 250 259 protected boolean isDocumentModified() { 260 if (fAcquireCount > 0) { 261 ContentStamp currentStamp= ContentStamps.get(fFile, fBuffer.getDocument()); 262 return !currentStamp.equals(fContentStamp); 263 } 264 return false; 265 } 266 267 279 protected boolean needsSaving() { 280 if ((fSaveMode & FORCE_SAVE) != 0) { 281 return true; 282 } 283 if ((fSaveMode & KEEP_SAVE_STATE) != 0) { 284 return fValidationState == null || !fValidationState.wasDirty(); 285 } 286 return false; 287 } 288 } 289 | Popular Tags |