KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > textmanipulation > TextBufferFactory


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.jdt.internal.corext.textmanipulation;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRunnable;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28
29 import org.eclipse.jface.util.Assert;
30
31 import org.eclipse.jface.text.Document;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.source.IAnnotationModel;
34
35 import org.eclipse.ui.part.FileEditorInput;
36 import org.eclipse.ui.texteditor.IDocumentProvider;
37
38 import org.eclipse.jdt.internal.corext.ValidateEditException;
39 import org.eclipse.jdt.internal.corext.util.IOCloser;
40 import org.eclipse.jdt.internal.corext.util.Resources;
41
42 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
43 import org.eclipse.jdt.internal.ui.JavaPlugin;
44
45 /**
46  * @deprecated Use file buffers instead
47  */

48 /* package */ class TextBufferFactory {
49
50     private IDocumentProvider fDocumentProvider;
51     private Map JavaDoc fFileValueMap;
52     private Map JavaDoc fBufferValueMap;
53     
54     private static class Value {
55         TextBuffer buffer;
56         FileEditorInput input;
57         IDocument document;
58         IAnnotationModel annotationModel;
59         int references;
60         public Value(TextBuffer b, FileEditorInput i, IDocument d, IAnnotationModel m) {
61             buffer= b;
62             input= i;
63             document= d;
64             annotationModel= m;
65         }
66     }
67
68     public TextBufferFactory() {
69         // XXX http://dev.eclipse.org/bugs/show_bug.cgi?id=5170
70
// Need way to map a file to a document without knowing any kind of document provider.
71
this(JavaPlugin.getDefault().getCompilationUnitDocumentProvider());
72     }
73     
74     public TextBufferFactory(IDocumentProvider provider) {
75         fDocumentProvider= provider;
76         Assert.isNotNull(fDocumentProvider);
77         fFileValueMap= new HashMap JavaDoc(5);
78         fBufferValueMap= new HashMap JavaDoc(5);
79     }
80
81     public TextBuffer acquire(IFile file) throws CoreException {
82         FileEditorInput input= new FileEditorInput(file);
83         
84         Value value= (Value)fFileValueMap.get(input);
85         if (value != null) {
86             value.references++;
87             return value.buffer;
88         }
89         
90         fDocumentProvider.connect(input);
91         IDocument document= fDocumentProvider.getDocument(input);
92         IAnnotationModel annotationModel= fDocumentProvider.getAnnotationModel(input);
93         if (annotationModel != null)
94             annotationModel.connect(document);
95         value= new Value(new TextBuffer(document), input, document, annotationModel);
96         fFileValueMap.put(input, value);
97         fBufferValueMap.put(value.buffer, value);
98         value.references++;
99         return value.buffer;
100     }
101     
102     public void release(TextBuffer buffer) {
103         Assert.isNotNull(buffer);
104         final Value value= (Value)fBufferValueMap.get(buffer);
105         if (value == null)
106             return;
107                         
108         value.references--;
109         if (value.references == 0) {
110             buffer.release();
111             if (value.annotationModel != null)
112                 value.annotationModel.disconnect(value.document);
113             fDocumentProvider.disconnect(value.input);
114             fFileValueMap.remove(value.input);
115             fBufferValueMap.remove(buffer);
116         }
117     }
118     
119     public void commitChanges(final TextBuffer buffer, boolean force, IProgressMonitor monitor) throws CoreException {
120         final Value value= (Value)fBufferValueMap.get(buffer);
121         if (value == null)
122             return;
123         
124         boolean save= force || fDocumentProvider.mustSaveDocument(value.input);
125         if (save) {
126             IWorkspaceRunnable action= new IWorkspaceRunnable() {
127                 public void run(IProgressMonitor pm) throws CoreException {
128                     IStatus status= makeCommittable(value, null);
129                     if (!status.isOK())
130                         throw new ValidateEditException(status);
131                     fDocumentProvider.aboutToChange(value.input);
132                     fDocumentProvider.saveDocument(pm, value.input, value.document, true);
133                 }
134             };
135             try {
136                 IResource schedulingRule= ResourcesPlugin.getWorkspace().getRoot();
137                 if (value.input != null) {
138                     schedulingRule= value.input.getFile();
139                     if (schedulingRule != null)
140                         schedulingRule= schedulingRule.getParent();
141                 }
142                 // FIXME: waiting for J Core to support scheduling rules (see bug 46332)
143
// ResourcesPlugin.getWorkspace().run(action, schedulingRule, IWorkspace.AVOID_UPDATE, monitor);
144
ResourcesPlugin.getWorkspace().run(action, monitor);
145             } finally {
146                 fDocumentProvider.changed(value.input);
147             }
148         }
149     }
150     
151     public TextBuffer create(IFile file) throws CoreException {
152         FileEditorInput input= new FileEditorInput(file);
153         IDocument document= fDocumentProvider.getDocument(input);
154         if (document != null) {
155             return new TextBuffer(new Document(document.get()));
156         } else {
157             return createFromFile(file);
158         }
159     }
160
161     private TextBuffer createFromFile(IFile file) throws CoreException {
162         IDocument document;
163         // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=19319
164
InputStream JavaDoc stream= file.getContents();
165         InputStreamReader JavaDoc in= null;
166         try {
167             document= new Document();
168             in= new InputStreamReader JavaDoc(new BufferedInputStream JavaDoc(stream), file.getCharset());
169             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
170             char[] readBuffer= new char[2048];
171             int n= in.read(readBuffer);
172             while (n > 0) {
173                 buffer.append(readBuffer, 0, n);
174                 n= in.read(readBuffer);
175             }
176             document.set(buffer.toString());
177             return new TextBuffer(document);
178         } catch (IOException JavaDoc x) {
179             String JavaDoc message= (x != null ? x.getMessage() : ""); //$NON-NLS-1$
180
IStatus s= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, message, x);
181             throw new CoreException(s);
182         } finally {
183             IOCloser.perform(in, stream);
184         }
185     }
186     
187     public TextBuffer create(String JavaDoc content) {
188         return new TextBuffer(new Document(content));
189     }
190     
191     public void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException {
192         Value value= (Value)fBufferValueMap.get(buffer);
193         if (value == null)
194             throwNotManaged();
195         fDocumentProvider.saveDocument(pm, value.input, value.document, true);
196     }
197
198     public void aboutToChange(TextBuffer buffer) throws CoreException {
199         Value value= (Value)fBufferValueMap.get(buffer);
200         if (value == null)
201             throwNotManaged();
202         fDocumentProvider.aboutToChange(value.input);
203     }
204         
205     public void changed(TextBuffer buffer) throws CoreException {
206         Value value= (Value)fBufferValueMap.get(buffer);
207         if (value == null)
208             throwNotManaged();
209         fDocumentProvider.changed(value.input);
210     }
211     
212     private void throwNotManaged() throws CoreException {
213         IStatus s= new Status(IStatus.ERROR, JavaPlugin.getPluginId(),
214             IJavaStatusConstants.INTERNAL_ERROR, TextManipulationMessages.TextBufferFactory_bufferNotManaged, null);
215         throw new CoreException(s);
216     }
217     
218     public IStatus makeCommittable(TextBuffer buffer, Object JavaDoc context) {
219         Value value= (Value)fBufferValueMap.get(buffer);
220         if (value == null) {
221             // The buffer is not managed. So it can be modified
222
return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$;
223
}
224         return makeCommittable(value, context);
225     }
226
227     private IStatus makeCommittable(Value value, Object JavaDoc context) {
228         IFile file= value.input.getFile();
229         return Resources.makeCommittable(file, context);
230     }
231 }
232
233
Popular Tags