KickJava   Java API By Example, From Geeks To Geeks.

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


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.DeleteEdit;
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 removes all trailing whitespace.
33  *
34  * @since 3.1
35  */

36 public class RemoveTrailingWhitespaceOperation extends TextFileBufferOperation {
37
38     /**
39      * Creates a remove trailing whitespace operation.
40      */

41     public RemoveTrailingWhitespaceOperation() {
42         super(FileBuffersMessages.RemoveTrailingWhitespaceOperation_name);
43     }
44
45     /*
46      * @see org.eclipse.core.internal.filebuffers.textmanipulation.TextFileBufferOperation#computeTextEdit(org.eclipse.core.filebuffers.ITextFileBuffer, org.eclipse.core.runtime.IProgressMonitor)
47      */

48     protected MultiTextEditWithProgress computeTextEdit(ITextFileBuffer fileBuffer, IProgressMonitor progressMonitor) throws CoreException {
49         IDocument document= fileBuffer.getDocument();
50         int lineCount= document.getNumberOfLines();
51
52         progressMonitor= Progress.getMonitor(progressMonitor);
53         progressMonitor.beginTask(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_generatingChanges, lineCount);
54         try {
55
56             MultiTextEditWithProgress multiEdit= new MultiTextEditWithProgress(FileBuffersMessages.RemoveTrailingWhitespaceOperation_task_applyingChanges);
57
58             for (int i= 0; i < lineCount; i++) {
59                 if (progressMonitor.isCanceled())
60                     throw new OperationCanceledException();
61
62                 IRegion region= document.getLineInformation(i);
63                 if (region.getLength() == 0)
64                     continue;
65
66                 int lineStart= region.getOffset();
67                 int lineExclusiveEnd= lineStart + region.getLength();
68                 int j= lineExclusiveEnd -1;
69                 while (j >= lineStart && Character.isWhitespace(document.getChar(j))) --j;
70                 ++j;
71                 if (j < lineExclusiveEnd)
72                     multiEdit.addChild(new DeleteEdit(j, lineExclusiveEnd - j));
73                 progressMonitor.worked(1);
74             }
75
76             return multiEdit.getChildrenSize() <= 0 ? null : multiEdit;
77
78         } catch (BadLocationException x) {
79             throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IFileBufferStatusCodes.CONTENT_CHANGE_FAILED, "", x)); //$NON-NLS-1$
80
} finally {
81             progressMonitor.done();
82         }
83     }
84
85     /*
86      * @see org.eclipse.core.filebuffers.manipulation.TextFileBufferOperation#getDocumentRewriteSessionType()
87      */

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