X-Git-Url: http://git.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/textmanipulation/TextBufferFactory.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/textmanipulation/TextBufferFactory.java index 5ce09f9..b78e53a 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/textmanipulation/TextBufferFactory.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/corext/textmanipulation/TextBufferFactory.java @@ -29,85 +29,98 @@ import org.eclipse.jface.util.Assert; import org.eclipse.ui.part.FileEditorInput; import org.eclipse.ui.texteditor.IDocumentProvider; -/* package */ class TextBufferFactory { +/* package */class TextBufferFactory { private IDocumentProvider fDocumentProvider; + private Map fFileValueMap; + private Map fBufferValueMap; - + private static class Value { TextBuffer buffer; + FileEditorInput input; + IDocument document; + IAnnotationModel annotationModel; + int references; - public Value(TextBuffer b, FileEditorInput i, IDocument d, IAnnotationModel m) { - buffer= b; - input= i; - document= d; - annotationModel= m; + + public Value(TextBuffer b, FileEditorInput i, IDocument d, + IAnnotationModel m) { + buffer = b; + input = i; + document = d; + annotationModel = m; } } public TextBufferFactory() { // XXX http://dev.eclipse.org/bugs/show_bug.cgi?id=5170 - // Need way to map a file to a document without knowing any kind of document provider. + // Need way to map a file to a document without knowing any kind of + // document provider. this(PHPeclipsePlugin.getDefault().getCompilationUnitDocumentProvider()); } - + public TextBufferFactory(IDocumentProvider provider) { - fDocumentProvider= provider; + fDocumentProvider = provider; Assert.isNotNull(fDocumentProvider); - fFileValueMap= new HashMap(5); - fBufferValueMap= new HashMap(5); + fFileValueMap = new HashMap(5); + fBufferValueMap = new HashMap(5); } public TextBuffer acquire(IFile file) throws CoreException { - FileEditorInput input= new FileEditorInput(file); - - Value value= (Value)fFileValueMap.get(input); + FileEditorInput input = new FileEditorInput(file); + + Value value = (Value) fFileValueMap.get(input); if (value != null) { value.references++; return value.buffer; } - + fDocumentProvider.connect(input); - IDocument document= fDocumentProvider.getDocument(input); - IAnnotationModel annotationModel= fDocumentProvider.getAnnotationModel(input); + IDocument document = fDocumentProvider.getDocument(input); + IAnnotationModel annotationModel = fDocumentProvider + .getAnnotationModel(input); annotationModel.connect(document); - value= new Value(new TextBuffer(document), input, document, annotationModel); + value = new Value(new TextBuffer(document), input, document, + annotationModel); fFileValueMap.put(input, value); fBufferValueMap.put(value.buffer, value); value.references++; return value.buffer; } - + public void release(TextBuffer buffer) { - final Value value= (Value)fBufferValueMap.get(buffer); + final Value value = (Value) fBufferValueMap.get(buffer); if (value == null) return; - + value.references--; if (value.references == 0) { - buffer.release(); + buffer.release(); value.annotationModel.disconnect(value.document); fDocumentProvider.disconnect(value.input); fFileValueMap.remove(value.input); fBufferValueMap.remove(buffer); } } - - public void commitChanges(TextBuffer buffer, boolean force, IProgressMonitor pm) throws CoreException { - final Value value= (Value)fBufferValueMap.get(buffer); + + public void commitChanges(TextBuffer buffer, boolean force, + IProgressMonitor pm) throws CoreException { + final Value value = (Value) fBufferValueMap.get(buffer); if (value == null) return; - - boolean save= force || fDocumentProvider.mustSaveDocument(value.input); + + boolean save = force || fDocumentProvider.mustSaveDocument(value.input); if (save) { - IWorkspaceRunnable action= new IWorkspaceRunnable() { + IWorkspaceRunnable action = new IWorkspaceRunnable() { public void run(IProgressMonitor pm) throws CoreException { fDocumentProvider.aboutToChange(value.input); - fDocumentProvider.saveDocument(pm, value.input, value.document, true); + fDocumentProvider.saveDocument(pm, value.input, + value.document, true); } }; try { @@ -117,10 +130,10 @@ import org.eclipse.ui.texteditor.IDocumentProvider; } } } - + public TextBuffer create(IFile file) throws CoreException { - FileEditorInput input= new FileEditorInput(file); - IDocument document= fDocumentProvider.getDocument(input); + FileEditorInput input = new FileEditorInput(file); + IDocument document = fDocumentProvider.getDocument(input); if (document != null) { return new TextBuffer(new Document(document.get())); } else { @@ -131,57 +144,61 @@ import org.eclipse.ui.texteditor.IDocumentProvider; private TextBuffer createFromFile(IFile file) throws CoreException { IDocument document; // Fix for http://dev.eclipse.org/bugs/show_bug.cgi?id=19319 - InputStream stream= file.getContents(); - InputStreamReader in= null; - try { - document= new Document(); - in= new InputStreamReader(new BufferedInputStream(stream), ResourcesPlugin.getEncoding()); - StringBuffer buffer= new StringBuffer(); - char[] readBuffer= new char[2048]; - int n= in.read(readBuffer); + InputStream stream = file.getContents(); + InputStreamReader in = null; + try { + document = new Document(); + in = new InputStreamReader(new BufferedInputStream(stream), + ResourcesPlugin.getEncoding()); + StringBuffer buffer = new StringBuffer(); + char[] readBuffer = new char[2048]; + int n = in.read(readBuffer); while (n > 0) { buffer.append(readBuffer, 0, n); - n= in.read(readBuffer); + n = in.read(readBuffer); } document.set(buffer.toString()); return new TextBuffer(document); } catch (IOException x) { - IStatus s= new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), PHPStatusConstants.INTERNAL_ERROR, x.getMessage(), x); + IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin + .getPluginId(), PHPStatusConstants.INTERNAL_ERROR, x + .getMessage(), x); throw new CoreException(s); } finally { IOCloser.perform(in, stream); } } - + public TextBuffer create(String content) { return new TextBuffer(new Document(content)); } - - public void save(TextBuffer buffer, IProgressMonitor pm) throws CoreException { - Value value= (Value)fBufferValueMap.get(buffer); + + public void save(TextBuffer buffer, IProgressMonitor pm) + throws CoreException { + Value value = (Value) fBufferValueMap.get(buffer); if (value == null) throwNotManaged(); fDocumentProvider.saveDocument(pm, value.input, value.document, true); } public void aboutToChange(TextBuffer buffer) throws CoreException { - Value value= (Value)fBufferValueMap.get(buffer); + Value value = (Value) fBufferValueMap.get(buffer); if (value == null) throwNotManaged(); fDocumentProvider.aboutToChange(value.input); } - + public void changed(TextBuffer buffer) throws CoreException { - Value value= (Value)fBufferValueMap.get(buffer); + Value value = (Value) fBufferValueMap.get(buffer); if (value == null) throwNotManaged(); fDocumentProvider.changed(value.input); } - + private void throwNotManaged() throws CoreException { - IStatus s= new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), - PHPStatusConstants.INTERNAL_ERROR, TextManipulationMessages.getString("TextBufferFactory.bufferNotManaged"), null); //$NON-NLS-1$ + IStatus s = new Status(IStatus.ERROR, PHPeclipsePlugin.getPluginId(), + PHPStatusConstants.INTERNAL_ERROR, TextManipulationMessages + .getString("TextBufferFactory.bufferNotManaged"), null); //$NON-NLS-1$ throw new CoreException(s); } } -