diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java index c6fd7a81dfd..076e1270c25 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/CFileElementWorkingCopy.java @@ -4,32 +4,200 @@ package org.eclipse.cdt.internal.ui; * (c) Copyright IBM Corp. 2000, 2001. * All Rights Reserved. */ - -import java.io.IOException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.IBuffer; +import org.eclipse.cdt.core.model.IBufferChangedListener; +import org.eclipse.cdt.core.model.IOpenable; +import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.internal.core.model.WorkingCopy; +import org.eclipse.cdt.internal.ui.editor.CDocumentProvider; +import org.eclipse.cdt.internal.ui.editor.DocumentAdapter; +import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DefaultLineTracker; import org.eclipse.jface.text.IDocument; import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IStorageEditorInput; import org.eclipse.ui.texteditor.IDocumentProvider; public class CFileElementWorkingCopy extends WorkingCopy { - - private IDocumentProvider fProvider; - //private IFileEditorInput input; - private IEditorInput input; - + + IDocumentProvider fProvider; + IEditorInput input; + IBuffer buffer; + /** - * Creates a working copy of this element + * Internal IBuffer implementation very simple, must cases will use DocumentAdapter. + * */ - public CFileElementWorkingCopy(IFileEditorInput fileInput, IDocumentProvider provider) throws CoreException { - super(null, fileInput.getFile(), null); - input= fileInput; - fProvider= provider; + class Buffer implements IBuffer { + + CFileElementWorkingCopy owner; + + public Buffer(CFileElementWorkingCopy o) { + owner = o; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#addBufferChangedListener(org.eclipse.cdt.core.model.IBufferChangedListener) + */ + public void addBufferChangedListener(IBufferChangedListener listener) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#append(char[]) + */ + public void append(char[] text) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#append(java.lang.String) + */ + public void append(String text) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#close() + */ + public void close() { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getChar(int) + */ + public char getChar(int position) { + IDocument doc = fProvider.getDocument(input); + if (doc != null) { + try { + return doc.getChar(position); + } catch (BadLocationException e) { + } + } + return 0; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getCharacters() + */ + public char[] getCharacters() { + return getContents().toCharArray(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getContents() + */ + public String getContents() { + IDocument doc = fProvider.getDocument(input); + if (doc != null) { + return doc.get(); + } + return new String(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getLength() + */ + public int getLength() { + IDocument doc = fProvider.getDocument(input); + if (doc != null) { + return doc.getLength(); + } + return 0; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getOwner() + */ + public IOpenable getOwner() { + return owner; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getText(int, int) + */ + public String getText(int offset, int length) { + IDocument doc = fProvider.getDocument(input); + if (doc != null) { + try { + return doc.get(offset, length); + } catch (BadLocationException e) { + } + } + return new String(); + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#getUnderlyingResource() + */ + public IResource getUnderlyingResource() { + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#hasUnsavedChanges() + */ + public boolean hasUnsavedChanges() { + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#isClosed() + */ + public boolean isClosed() { + return false; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#isReadOnly() + */ + public boolean isReadOnly() { + return true; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#removeBufferChangedListener(org.eclipse.cdt.core.model.IBufferChangedListener) + */ + public void removeBufferChangedListener(IBufferChangedListener listener) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#replace(int, int, char[]) + */ + public void replace(int position, int length, char[] text) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#replace(int, int, java.lang.String) + */ + public void replace(int position, int length, String text) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#save(org.eclipse.core.runtime.IProgressMonitor, boolean) + */ + public void save(IProgressMonitor progress, boolean force) throws CModelException { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#setContents(char[]) + */ + public void setContents(char[] contents) { + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IBuffer#setContents(java.lang.String) + */ + public void setContents(String contents) { + } + } /** @@ -43,18 +211,44 @@ public class CFileElementWorkingCopy extends WorkingCopy { super.setLocation(storage.getFullPath()); } - /** - * @see CFileElement#update + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ITranslationUnit#parse() */ - public void update() throws CoreException { - IDocument doc= fProvider.getDocument(input); + public Map parse() { + IDocument doc = fProvider.getDocument(input); if (doc != null) { - DocumentInputStream dis= new DocumentInputStream(doc); + DocumentInputStream dis = new DocumentInputStream(doc); try { - parse(dis); + return parse(dis); } finally { - try { dis.close(); } catch (IOException e) {} + try { + dis.close(); + } catch (IOException e) { + } } } + return new HashMap(); } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.IOpenable#getBuffer() + */ + public IBuffer getBuffer() throws CModelException { + if (buffer == null) { + if (fProvider instanceof CDocumentProvider) { + buffer = new DocumentAdapter(this, fProvider.getDocument(input), new DefaultLineTracker(), (CDocumentProvider)fProvider, input); + } else { + buffer = new Buffer(this); + } + } + return buffer; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.internal.core.model.IWorkingCopy#getOriginalElement() + */ + public ITranslationUnit getOriginalElement() { + return this; + } + }