1 11 package org.eclipse.core.filebuffers.manipulation; 12 13 import java.util.Map ; 14 15 import org.eclipse.core.internal.filebuffers.FileBuffersPlugin; 16 import org.eclipse.core.internal.filebuffers.Progress; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.OperationCanceledException; 22 import org.eclipse.core.runtime.Status; 23 24 import org.eclipse.core.filebuffers.IFileBuffer; 25 import org.eclipse.core.filebuffers.IFileBufferStatusCodes; 26 import org.eclipse.core.filebuffers.ITextFileBuffer; 27 28 import org.eclipse.text.edits.TextEdit; 29 30 import org.eclipse.jface.text.BadLocationException; 31 import org.eclipse.jface.text.DocumentRewriteSession; 32 import org.eclipse.jface.text.DocumentRewriteSessionType; 33 import org.eclipse.jface.text.IDocument; 34 import org.eclipse.jface.text.IDocumentExtension4; 35 import org.eclipse.jface.text.TextUtilities; 36 37 45 public abstract class TextFileBufferOperation implements IFileBufferOperation { 46 47 48 57 protected abstract MultiTextEditWithProgress computeTextEdit(ITextFileBuffer textFileBuffer, IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException; 58 59 64 protected abstract DocumentRewriteSessionType getDocumentRewriteSessionType(); 65 66 67 private String fOperationName; 68 private DocumentRewriteSession fDocumentRewriteSession; 69 70 75 protected TextFileBufferOperation(String operationName) { 76 fOperationName= operationName; 77 } 78 79 82 public String getOperationName() { 83 return fOperationName; 84 } 85 86 89 public void run(IFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException { 90 91 if (fileBuffer instanceof ITextFileBuffer) { 92 ITextFileBuffer textFileBuffer= (ITextFileBuffer) fileBuffer; 93 IPath path= textFileBuffer.getLocation(); 94 String taskName= path == null ? getOperationName() : path.lastSegment(); 95 progressMonitor= Progress.getMonitor(progressMonitor); 96 progressMonitor.beginTask(taskName, 100); 97 try { 98 IProgressMonitor subMonitor= Progress.getSubMonitor(progressMonitor, 10); 99 MultiTextEditWithProgress edit= computeTextEdit(textFileBuffer, subMonitor); 100 subMonitor.done(); 101 if (edit != null) { 102 Object stateData= startRewriteSession(textFileBuffer); 103 try { 104 subMonitor= Progress.getSubMonitor(progressMonitor, 90); 105 applyTextEdit(textFileBuffer, edit, subMonitor); 106 subMonitor.done(); 107 } finally { 108 stopRewriteSession(textFileBuffer, stateData); 109 } 110 } 111 } finally { 112 progressMonitor.done(); 113 } 114 } 115 } 116 117 private Object startRewriteSession(ITextFileBuffer fileBuffer) { 118 Object stateData= null; 119 120 IDocument document= fileBuffer.getDocument(); 121 if (document instanceof IDocumentExtension4) { 122 IDocumentExtension4 extension= (IDocumentExtension4) document; 123 fDocumentRewriteSession= extension.startRewriteSession(getDocumentRewriteSessionType()); 124 } else 125 stateData= TextUtilities.removeDocumentPartitioners(document); 126 127 return stateData; 128 } 129 130 private void stopRewriteSession(ITextFileBuffer fileBuffer, Object stateData) { 131 IDocument document= fileBuffer.getDocument(); 132 if (document instanceof IDocumentExtension4) { 133 IDocumentExtension4 extension= (IDocumentExtension4) document; 134 extension.stopRewriteSession(fDocumentRewriteSession); 135 fDocumentRewriteSession= null; 136 } else if (stateData instanceof Map ) 137 TextUtilities.addDocumentPartitioners(document, (Map ) stateData); 138 } 139 140 private void applyTextEdit(ITextFileBuffer fileBuffer, MultiTextEditWithProgress textEdit, IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException { 141 try { 142 textEdit.apply(fileBuffer.getDocument(), TextEdit.NONE, progressMonitor); 143 } catch (BadLocationException x) { 144 throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x)); } 146 } 147 } 148 | Popular Tags |