2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 import java.io.BufferedInputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.util.HashMap;
14 import net.sourceforge.phpdt.internal.corext.util.IOCloser;
15 import net.sourceforge.phpdt.internal.ui.PHPStatusConstants;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.text.Document;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.jface.util.Assert;
29 import org.eclipse.ui.part.FileEditorInput;
30 import org.eclipse.ui.texteditor.IDocumentProvider;
32 /* package */ class TextBufferFactory {
34 private IDocumentProvider fDocumentProvider;
35 private Map fFileValueMap;
36 private Map fBufferValueMap;
38 private static class Value {
40 FileEditorInput input;
42 IAnnotationModel annotationModel;
44 public Value(TextBuffer b, FileEditorInput i, IDocument d, IAnnotationModel m) {
52 public TextBufferFactory() {
53 // XXX http://dev.eclipse.org/bugs/show_bug.cgi?id=5170
54 // Need way to map a file to a document without knowing any kind of document provider.
55 this(PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider());
58 public TextBufferFactory(IDocumentProvider provider) {
59 fDocumentProvider= provider;
60 Assert.isNotNull(fDocumentProvider);
61 fFileValueMap= new HashMap(5);
62 fBufferValueMap= new HashMap(5);
65 public TextBuffer acquire(IFile file) throws CoreException {
66 FileEditorInput input= new FileEditorInput(file);
68 Value value= (Value)fFileValueMap.get(input);
74 fDocumentProvider.connect(input);
75 IDocument document= fDocumentProvider.getDocument(input);
76 IAnnotationModel annotationModel= fDocumentProvider.getAnnotationModel(input);
77 annotationModel.connect(document);
78 value= new Value(new TextBuffer(document), input, document, annotationModel);
79 fFileValueMap.put(input, value);
80 fBufferValueMap.put(value.buffer, value);
85 public void release(TextBuffer buffer) {
86 final Value value= (Value)fBufferValueMap.get(buffer);
91 if (value.references == 0) {
93 value.annotationModel.disconnect(value.document);
94 fDocumentProvider.disconnect(value.input);
95 fFileValueMap.remove(value.input);
96 fBufferValueMap.remove(buffer);
100 public void commitChanges(TextBuffer buffer, boolean force, IProgressMonitor pm) throws CoreException {
101 final Value value= (Value)fBufferValueMap.get(buffer);
105 boolean save= force || fDocumentProvider.mustSaveDocument(value.input);
107 IWorkspaceRunnable action= new IWorkspaceRunnable() {
108 public void run(IProgressMonitor pm) throws CoreException {
109 fDocumentProvider.aboutToChange(value.input);
110 fDocumentProvider.saveDocument(pm, value.input, value.document, true);
114 ResourcesPlugin.getWorkspace().run(action, pm);
116 fDocumentProvider.changed(value.input);
121 public TextBuffer create(IFile file) throws CoreException {
122 FileEditorInput input= new FileEditorInput(file);
123 IDocument document= fDocumentProvider.getDocument(input);
124 if (document != null) {
125 return new TextBuffer(new Document(document.get()));
127 return createFromFile(file);
131 private TextBuffer createFromFile(IFile file) throws CoreException {
133 // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=19319
134 InputStream stream= file.getContents();
135 InputStreamReader in= null;
137 document= new Document();
138 in= new InputStreamReader(new BufferedInputStream(stream), ResourcesPlugin.getEncoding());
139 StringBuffer buffer= new StringBuffer();
140 char[] readBuffer= new char[2048];
141 int n= in.read(readBuffer);
143 buffer.append(readBuffer, 0, n);
144 n= in.read(readBuffer);
146 document.set(buffer.toString());
147 return new TextBuffer(document);
148 } catch (IOException x) {
149 IStatus s= new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), PHPStatusConstants.INTERNAL_ERROR, x.getMessage(), x);
150 throw new CoreException(s);
152 IOCloser.perform(in, stream);
156 public TextBuffer create(String content) {
157 return new TextBuffer(new Document(content));
160 public void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException {
161 Value value= (Value)fBufferValueMap.get(buffer);
164 fDocumentProvider.saveDocument(pm, value.input, value.document, true);
167 public void aboutToChange(TextBuffer buffer) throws CoreException {
168 Value value= (Value)fBufferValueMap.get(buffer);
171 fDocumentProvider.aboutToChange(value.input);
174 public void changed(TextBuffer buffer) throws CoreException {
175 Value value= (Value)fBufferValueMap.get(buffer);
178 fDocumentProvider.changed(value.input);
181 private void throwNotManaged() throws CoreException {
182 IStatus s= new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(),
183 PHPStatusConstants.INTERNAL_ERROR, TextManipulationMessages.getString("TextBufferFactory.bufferNotManaged"), null); //$NON-NLS-1$
184 throw new CoreException(s);