* Added browser like links (Ctrl+Mouseclick on identifier; same as F3 shortcut)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / DocumentCharacterIterator.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.text.CharacterIterator;
14
15 import org.eclipse.jface.text.Assert;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
18
19
20 /**
21  * An <code>IDocument</code> based implementation of <code>CharacterIterator</code>.
22  * 
23  * @since 3.0
24  */
25 public class DocumentCharacterIterator implements CharacterIterator, CharSequence {
26
27         private int fIndex= -1;
28         private final IDocument fDocument;
29         private final int fFirst;
30         private final int fLast;
31         
32         private void invariant() {
33                 Assert.isTrue(fIndex >= fFirst);
34                 Assert.isTrue(fIndex <= fLast);
35         }
36         
37         /**
38          * Creates an iterator for the entire sequence.
39          * 
40          * @param sequence the sequence backing this iterator
41          */
42         public DocumentCharacterIterator(IDocument sequence) {
43                 this(sequence, 0);
44         }
45         
46         /**
47          * Creates an iterator.
48          * 
49          * @param sequence the sequence backing this iterator
50          * @param first the first character to consider
51          * @throws IllegalArgumentException if the indices are out of bounds
52          */
53         public DocumentCharacterIterator(IDocument sequence, int first) throws IllegalArgumentException {
54                 this(sequence, first, sequence.getLength());
55         }
56
57         /**
58          * Creates an iterator.
59          * 
60          * @param sequence the sequence backing this iterator
61          * @param first the first character to consider
62          * @param last the last character index to consider
63          * @throws IllegalArgumentException if the indices are out of bounds
64          */
65         public DocumentCharacterIterator(IDocument sequence, int first, int last) throws IllegalArgumentException {
66                 if (sequence == null)
67                         throw new NullPointerException();
68                 if (first < 0 || first > last)
69                         throw new IllegalArgumentException();
70                 if (last > sequence.getLength())
71                         throw new IllegalArgumentException();
72                 fDocument= sequence;
73                 fFirst= first;
74                 fLast= last;
75                 fIndex= first;
76                 invariant();
77         }
78
79         /*
80          * @see java.text.CharacterIterator#first()
81          */
82         public char first() {
83                 return setIndex(getBeginIndex());
84         }
85
86         /*
87          * @see java.text.CharacterIterator#last()
88          */
89         public char last() {
90                 if (fFirst == fLast)
91                         return setIndex(getEndIndex());
92                 else
93                         return setIndex(getEndIndex() - 1);
94         }
95
96         /*
97          * @see java.text.CharacterIterator#current()
98          */
99         public char current() {
100                 if (fIndex >= fFirst && fIndex < fLast)
101                         try {
102                                 return fDocument.getChar(fIndex);
103                         } catch (BadLocationException e) {
104                                 // ignore
105                         }
106                 return DONE;
107         }
108
109         /*
110          * @see java.text.CharacterIterator#next()
111          */
112         public char next() {
113                 return setIndex(Math.min(fIndex + 1, getEndIndex()));
114         }
115
116         /*
117          * @see java.text.CharacterIterator#previous()
118          */
119         public char previous() {
120                 if (fIndex > getBeginIndex()) {
121                         return setIndex(fIndex - 1);
122                 } else {
123                         return DONE;
124                 }
125         }
126
127         /*
128          * @see java.text.CharacterIterator#setIndex(int)
129          */
130         public char setIndex(int position) {
131                 if (position >= getBeginIndex() && position <= getEndIndex())
132                         fIndex= position;
133                 else
134                         throw new IllegalArgumentException();
135                 
136                 invariant();
137                 return current();
138         }
139
140         /*
141          * @see java.text.CharacterIterator#getBeginIndex()
142          */
143         public int getBeginIndex() {
144                 return fFirst;
145         }
146
147         /*
148          * @see java.text.CharacterIterator#getEndIndex()
149          */
150         public int getEndIndex() {
151                 return fLast;
152         }
153
154         /*
155          * @see java.text.CharacterIterator#getIndex()
156          */
157         public int getIndex() {
158                 return fIndex;
159         }
160
161         /*
162          * @see java.text.CharacterIterator#clone()
163          */
164         public Object clone() {
165                 try {
166                         return super.clone();
167                 } catch (CloneNotSupportedException e) {
168                         throw new InternalError();
169                 }
170         }
171
172         /*
173          * @see java.lang.CharSequence#length()
174          */
175         public int length() {
176                 return getEndIndex() - getBeginIndex();
177         }
178
179         /*
180          * @see java.lang.CharSequence#charAt(int)
181          */
182         public char charAt(int index) {
183                 if (index >= getBeginIndex() && index <= getEndIndex())
184                         try {
185                                 return fDocument.getChar(index);
186                         } catch (BadLocationException e) {
187                                 // ignore and return DONE
188                                 return DONE;
189                         }
190                 else
191                         throw new IllegalArgumentException();
192         }
193
194         /*
195          * @see java.lang.CharSequence#subSequence(int, int)
196          */
197         public CharSequence subSequence(int start, int end) {
198                 return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + start + end);
199         }
200 }