KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Map JavaDoc;
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 /**
38  * Abstraction for a file buffer operation that works on text file buffers.
39  * Subclasses have to override the <code>computeTextEdits</code> method in
40  * order to provide a sequence of {@link org.eclipse.text.edits.TextEdit}
41  * objects.
42  *
43  * @since 3.1
44  */

45 public abstract class TextFileBufferOperation implements IFileBufferOperation {
46
47
48     /**
49      * Computes and returns a text edit. Subclasses have to provide that method.
50      *
51      * @param textFileBuffer the text file buffer to manipulate
52      * @param progressMonitor the progress monitor
53      * @return the text edits describing the content manipulation
54      * @throws CoreException in case the computation failed
55      * @throws OperationCanceledException in case the progress monitor has been set to canceled
56      */

57     protected abstract MultiTextEditWithProgress computeTextEdit(ITextFileBuffer textFileBuffer, IProgressMonitor progressMonitor) throws CoreException, OperationCanceledException;
58
59     /**
60      * Returns the rewrite session type that corresponds to the text edit sequence.
61      *
62      * @return the rewrite session type
63      */

64     protected abstract DocumentRewriteSessionType getDocumentRewriteSessionType();
65
66
67     private String JavaDoc fOperationName;
68     private DocumentRewriteSession fDocumentRewriteSession;
69
70     /**
71      * Creates a new operation with the given name.
72      *
73      * @param operationName the name of the operation
74      */

75     protected TextFileBufferOperation(String JavaDoc operationName) {
76         fOperationName= operationName;
77     }
78
79     /*
80      * @see org.eclipse.core.internal.filebuffers.textmanipulation.IFileBufferOperation#getOperationName()
81      */

82     public String JavaDoc getOperationName() {
83         return fOperationName;
84     }
85
86     /*
87      * @see org.eclipse.core.internal.filebuffers.textmanipulation.IFileBufferOperation#run(org.eclipse.core.filebuffers.IFileBuffer, org.eclipse.core.runtime.IProgressMonitor)
88      */

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 JavaDoc 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 JavaDoc 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 JavaDoc startRewriteSession(ITextFileBuffer fileBuffer) {
118         Object JavaDoc 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 JavaDoc 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 JavaDoc)
137             TextUtilities.addDocumentPartitioners(document, (Map JavaDoc) 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)); //$NON-NLS-1$
145
}
146     }
147 }
148
Popular Tags