KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filebuffers > manipulation > ConvertLineDelimitersOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
32  * A text file buffer operation that changes the line delimiters to a specified
33  * line delimiter.
34  *
35  * @since 3.1
36  */

37 public class ConvertLineDelimitersOperation extends TextFileBufferOperation {
38
39     private String JavaDoc fLineDelimiter;
40
41     /**
42      * Creates a new line delimiter conversion operation for the given target
43      * delimiter.
44      *
45      * @param lineDelimiter the target line delimiter
46      */

47     public ConvertLineDelimitersOperation(String JavaDoc lineDelimiter) {
48         super(FileBuffersMessages.ConvertLineDelimitersOperation_name);
49         fLineDelimiter= lineDelimiter;
50     }
51
52     /*
53      * @see org.eclipse.core.internal.filebuffers.textmanipulation.TextFileBufferOperation#computeTextEdit(org.eclipse.core.filebuffers.ITextFileBuffer, org.eclipse.core.runtime.IProgressMonitor)
54      */

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 JavaDoc 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)); //$NON-NLS-1$
81
} finally {
82             progressMonitor.done();
83         }
84     }
85
86     /*
87      * @see org.eclipse.core.filebuffers.manipulation.TextFileBufferOperation#getDocumentRewriteSessionType()
88      */

89     protected DocumentRewriteSessionType getDocumentRewriteSessionType() {
90         return DocumentRewriteSessionType.SEQUENTIAL;
91     }
92 }
93
Popular Tags