diff options
-rw-r--r-- | org.eclipse.jface.text/src/org/eclipse/jface/text/DefaultDocumentAdapter.java | 71 | ||||
-rw-r--r-- | org.eclipse.jface.text/src/org/eclipse/jface/text/DocumentClone.java | 72 |
2 files changed, 125 insertions, 18 deletions
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/DefaultDocumentAdapter.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/DefaultDocumentAdapter.java index 2f2ee957df9..f1e73c28eaf 100644 --- a/org.eclipse.jface.text/src/org/eclipse/jface/text/DefaultDocumentAdapter.java +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/DefaultDocumentAdapter.java @@ -29,6 +29,12 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo /** The adapted document. */ private IDocument fDocument; + /** The document clone for the non-forwarding case. */ + private IDocument fDocumentClone; + /** The original content */ + private String fOriginalContent; + /** The original line delimiters */ + private String[] fOriginalLineDelimiters; /** The registered text change listeners */ private List fTextChangeListeners= new ArrayList(1); /** @@ -80,6 +86,12 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo fDocument= document; fLineDelimiter= null; + if (!fIsForwarding) { + fDocumentClone= null; + fOriginalContent= fDocument.get(); + fOriginalLineDelimiters= fDocument.getLegalLineDelimiters(); + } + if (fDocument != null) fDocument.addPrenotifiedDocumentListener(this); } @@ -107,9 +119,9 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @see IRepairableDocument#repairLineInformation() * @since 3.0 */ - private void repairLineInformation() { - if (fDocument instanceof IRepairableDocument) { - IRepairableDocument repairable= (IRepairableDocument) fDocument; + private void repairLineInformation(IDocument document) { + if (document instanceof IRepairableDocument) { + IRepairableDocument repairable= (IRepairableDocument) document; repairable.repairLineInformation(); } } @@ -122,21 +134,36 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @throws BadLocationException if the line number is invalid for the adapted document * @since 3.0 */ - private String doGetLine(int line) throws BadLocationException { - IRegion r= fDocument.getLineInformation(line); - return fDocument.get(r.getOffset(), r.getLength()); + private String doGetLine(IDocument document, int line) throws BadLocationException { + IRegion r= document.getLineInformation(line); + return document.get(r.getOffset(), r.getLength()); + } + + private IDocument getDocumentForRead() { + if (!fIsForwarding) { + if (fDocumentClone == null) { + String content= fOriginalContent == null ? "" : fOriginalContent; + String[] delims= fOriginalLineDelimiters == null ? DefaultLineTracker.DELIMITERS : fOriginalLineDelimiters; + fDocumentClone= new DocumentClone(content, delims); + } + return fDocumentClone; + } + + return fDocument; } /* * @see StyledTextContent#getLine(int) */ public String getLine(int line) { + + IDocument document= getDocumentForRead(); try { - return doGetLine(line); + return doGetLine(document, line); } catch (BadLocationException x) { - repairLineInformation(); + repairLineInformation(document); try { - return doGetLine(line); + return doGetLine(document, line); } catch (BadLocationException x2) { } } @@ -149,12 +176,13 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @see StyledTextContent#getLineAtOffset(int) */ public int getLineAtOffset(int offset) { + IDocument document= getDocumentForRead(); try { - return fDocument.getLineOfOffset(offset); + return document.getLineOfOffset(offset); } catch (BadLocationException x) { - repairLineInformation(); + repairLineInformation(document); try { - return fDocument.getLineOfOffset(offset); + return document.getLineOfOffset(offset); } catch (BadLocationException x2) { } } @@ -167,19 +195,20 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @see StyledTextContent#getLineCount() */ public int getLineCount() { - return fDocument.getNumberOfLines(); + return getDocumentForRead().getNumberOfLines(); } /* * @see StyledTextContent#getOffsetAtLine(int) */ public int getOffsetAtLine(int line) { + IDocument document= getDocumentForRead(); try { - return fDocument.getLineOffset(line); + return document.getLineOffset(line); } catch (BadLocationException x) { - repairLineInformation(); + repairLineInformation(document); try { - return fDocument.getLineOffset(line); + return document.getLineOffset(line); } catch (BadLocationException x2) { } } @@ -193,7 +222,7 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo */ public String getTextRange(int offset, int length) { try { - return fDocument.get(offset, length); + return getDocumentForRead().get(offset, length); } catch (BadLocationException x) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); return null; @@ -222,7 +251,7 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @see StyledTextContent#getCharCount() */ public int getCharCount() { - return fDocument.getLength(); + return getDocumentForRead().getLength(); } /* @@ -362,6 +391,9 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo */ public void resumeForwardingDocumentChanges() { fIsForwarding= true; + fDocumentClone= null; + fOriginalContent= null; + fOriginalLineDelimiters= null; fireTextSet(); } @@ -370,6 +402,9 @@ class DefaultDocumentAdapter implements IDocumentAdapter, IDocumentListener, IDo * @since 2.0 */ public void stopForwardingDocumentChanges() { + fDocumentClone= null; + fOriginalContent= fDocument.get(); + fOriginalLineDelimiters= fDocument.getLegalLineDelimiters(); fIsForwarding= false; } } diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/DocumentClone.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/DocumentClone.java new file mode 100644 index 00000000000..add26238f0c --- /dev/null +++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/DocumentClone.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.jface.text; + +/** + * An {@link org.eclipse.jface.text.IDocument} that is a read-only clone of another document. + * + * @since 3.0 + */ +class DocumentClone extends AbstractDocument { + + private static class StringTextStore implements ITextStore { + + private String fContent; + + public StringTextStore(String content) { + Assert.isNotNull(content); + fContent= content; + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int) + */ + public char get(int offset) { + return fContent.charAt(offset); + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int, int) + */ + public String get(int offset, int length) { + return fContent.substring(offset, offset + length); + } + + /* + * @see org.eclipse.jface.text.ITextStore#getLength() + */ + public int getLength() { + return fContent.length(); + } + + /* + * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String) + */ + public void replace(int offset, int length, String text) { + } + + /* + * @see org.eclipse.jface.text.ITextStore#set(java.lang.String) + */ + public void set(String text) { + } + + } + + public DocumentClone(String content, String[] lineDelimiters) { + super(); + setTextStore(new StringTextStore(content)); + ConfigurableLineTracker tracker= new ConfigurableLineTracker(lineDelimiters); + setLineTracker(tracker); + getTracker().set(content); + completeInitialization(); + } +} |