1 11 package org.eclipse.core.filebuffers.manipulation; 12 13 import org.eclipse.core.internal.filebuffers.FileBuffersPlugin; 14 import org.eclipse.core.internal.filebuffers.Progress; 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.OperationCanceledException; 19 import org.eclipse.core.runtime.Status; 20 21 import org.eclipse.core.filebuffers.IFileBufferStatusCodes; 22 import org.eclipse.core.filebuffers.ITextFileBuffer; 23 24 import org.eclipse.text.edits.ReplaceEdit; 25 26 import org.eclipse.jface.text.BadLocationException; 27 import org.eclipse.jface.text.DocumentRewriteSessionType; 28 import org.eclipse.jface.text.IDocument; 29 import org.eclipse.jface.text.IRegion; 30 31 37 public class ConvertLineDelimitersOperation extends TextFileBufferOperation { 38 39 private String fLineDelimiter; 40 41 47 public ConvertLineDelimitersOperation(String lineDelimiter) { 48 super(FileBuffersMessages.ConvertLineDelimitersOperation_name); 49 fLineDelimiter= lineDelimiter; 50 } 51 52 55 protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException { 56 IDocument document= fileBuffer.getDocument(); 57 int lineCount= document.getNumberOfLines(); 58 59 progressMonitor= Progress.getMonitor(progressMonitor); 60 progressMonitor.beginTask(FileBuffersMessages.ConvertLineDelimitersOperation_task_generatingChanges, lineCount); 61 try { 62 63 MultiTextEditWithProgress multiEdit= new MultiTextEditWithProgress(FileBuffersMessages.ConvertLineDelimitersOperation_task_applyingChanges); 64 65 for (int i= 0; i < lineCount; i++) { 66 if (progressMonitor.isCanceled()) 67 throw new OperationCanceledException(); 68 69 final String delimiter= document.getLineDelimiter(i); 70 if (delimiter != null && delimiter.length() > 0 && !delimiter.equals(fLineDelimiter)) { 71 IRegion region= document.getLineInformation(i); 72 multiEdit.addChild(new ReplaceEdit(region.getOffset() + region.getLength(), delimiter.length(), fLineDelimiter)); 73 } 74 progressMonitor.worked(1); 75 } 76 77 return multiEdit.getChildrenSize() <= 0 ? null : multiEdit; 78 79 } catch (BadLocationException x) { 80 throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x)); } finally { 82 progressMonitor.done(); 83 } 84 } 85 86 89 protected DocumentRewriteSessionType getDocumentRewriteSessionType() { 90 return DocumentRewriteSessionType.SEQUENTIAL; 91 } 92 } 93 | Popular Tags |