1 package org.simantics.scl.ui.editor2.iterator;
3 import java.text.CharacterIterator;
5 import org.eclipse.core.runtime.Assert;
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.IDocument;
12 * An <code>IDocument</code> based implementation of
13 * <code>CharacterIterator</code> and <code>CharSequence</code>. Note that
14 * the supplied document is not copied; if the document is modified during the
15 * lifetime of a <code>DocumentCharacterIterator</code>, the methods
16 * returning document content may not always return the same values. Also, if
17 * accessing the document fails with a {@link BadLocationException}, any of
18 * <code>CharacterIterator</code> methods as well as <code>charAt</code>may
19 * return {@link CharacterIterator#DONE}.
23 public class DocumentCharacterIterator implements CharacterIterator, CharSequence {
25 private int fIndex= -1;
26 private final IDocument fDocument;
27 private final int fFirst;
28 private final int fLast;
30 private void invariant() {
31 Assert.isTrue(fIndex >= fFirst);
32 Assert.isTrue(fIndex <= fLast);
36 * Creates an iterator for the entire document.
38 * @param document the document backing this iterator
39 * @throws BadLocationException if the indices are out of bounds
41 public DocumentCharacterIterator(IDocument document) throws BadLocationException {
46 * Creates an iterator, starting at offset <code>first</code>.
48 * @param document the document backing this iterator
49 * @param first the first character to consider
50 * @throws BadLocationException if the indices are out of bounds
52 public DocumentCharacterIterator(IDocument document, int first) throws BadLocationException {
53 this(document, first, document.getLength());
57 * Creates an iterator for the document contents from <code>first</code> (inclusive) to
58 * <code>last</code> (exclusive).
60 * @param document the document backing this iterator
61 * @param first the first character to consider
62 * @param last the last character index to consider
63 * @throws BadLocationException if the indices are out of bounds
65 public DocumentCharacterIterator(IDocument document, int first, int last) throws BadLocationException {
67 throw new NullPointerException();
68 if (first < 0 || first > last)
69 throw new BadLocationException();
70 if (last > document.getLength()) {
71 throw new BadLocationException();
81 * @see java.text.CharacterIterator#first()
84 return setIndex(getBeginIndex());
88 * @see java.text.CharacterIterator#last()
92 return setIndex(getEndIndex());
94 return setIndex(getEndIndex() - 1);
98 * @see java.text.CharacterIterator#current()
100 public char current() {
101 if (fIndex >= fFirst && fIndex < fLast)
103 return fDocument.getChar(fIndex);
104 } catch (BadLocationException e) {
111 * @see java.text.CharacterIterator#next()
114 return setIndex(Math.min(fIndex + 1, getEndIndex()));
118 * @see java.text.CharacterIterator#previous()
120 public char previous() {
121 if (fIndex > getBeginIndex()) {
122 return setIndex(fIndex - 1);
129 * @see java.text.CharacterIterator#setIndex(int)
131 public char setIndex(int position) {
132 if (position >= getBeginIndex() && position <= getEndIndex())
135 throw new IllegalArgumentException();
142 * @see java.text.CharacterIterator#getBeginIndex()
144 public int getBeginIndex() {
149 * @see java.text.CharacterIterator#getEndIndex()
151 public int getEndIndex() {
156 * @see java.text.CharacterIterator#getIndex()
158 public int getIndex() {
163 * @see java.text.CharacterIterator#clone()
166 public Object clone() {
168 return super.clone();
169 } catch (CloneNotSupportedException e) {
170 throw new InternalError();
175 * @see java.lang.CharSequence#length()
177 public int length() {
178 return getEndIndex() - getBeginIndex();
184 * Note that, if the document is modified concurrently, this method may
185 * return {@link CharacterIterator#DONE} if a {@link BadLocationException}
186 * was thrown when accessing the backing document.
189 * @param index {@inheritDoc}
190 * @return {@inheritDoc}
192 public char charAt(int index) {
193 if (index >= 0 && index < length())
195 return fDocument.getChar(getBeginIndex() + index);
196 } catch (BadLocationException e) {
197 // ignore and return DONE
201 throw new IndexOutOfBoundsException();
205 * @see java.lang.CharSequence#subSequence(int, int)
207 public CharSequence subSequence(int start, int end) {
209 throw new IndexOutOfBoundsException();
211 throw new IndexOutOfBoundsException();
213 throw new IndexOutOfBoundsException();
215 return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + end);
216 } catch (BadLocationException ex) {
217 throw new IndexOutOfBoundsException();