KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > text > AttributedCharacterSpanIterator


1 /*
2
3    Copyright 2000-2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.gvt.text;
19
20 import java.text.AttributedCharacterIterator JavaDoc;
21 import java.text.CharacterIterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * AttributedCharacterSpanIterator
27  *
28  * Used to provide ACI functionality to a "substring" of an AttributedString.
29  * In this way a TextLayout can be created which only uses a substring of
30  * AttributedString.
31  *
32  * @author <a HREF="mailto:bill.haneman@ireland.sun.com">Bill Haneman</a>
33  * @version $Id: AttributedCharacterSpanIterator.java,v 1.9 2004/08/18 07:14:40 vhardy Exp $
34  */

35
36 public class AttributedCharacterSpanIterator implements
37                                    AttributedCharacterIterator JavaDoc {
38
39     private AttributedCharacterIterator JavaDoc aci;
40     private int begin;
41     private int end;
42
43     /**
44      * Construct a AttributedCharacterSpanIterator from a subinterval of
45      * an existing AttributedCharacterIterator.
46      * @param aci The source AttributedCharacterIterator
47      * @param start the first index of the subinterval
48      * @param stop the index of the first character after the subinterval
49      */

50     public AttributedCharacterSpanIterator(AttributedCharacterIterator JavaDoc aci,
51                                            int start, int stop) {
52         this.aci = aci;
53         end = Math.min(aci.getEndIndex(), stop);
54         begin = Math.max(aci.getBeginIndex(), start);
55         this.aci.setIndex(begin);
56     }
57
58     //From java.text.AttributedCharacterIterator
59

60     /**
61      * Get the keys of all attributes defined on the iterator's text range.
62      */

63     public Set JavaDoc getAllAttributeKeys() {
64         return aci.getAllAttributeKeys();
65         // FIXME: not if there are atts outside the substring!
66
}
67
68     /**
69      * Get the value of the named attribute for the current
70      * character.
71      */

72     public Object JavaDoc getAttribute(AttributedCharacterIterator.Attribute JavaDoc attribute) {
73         return aci.getAttribute(attribute);
74     }
75
76     /**
77      * Returns a map with the attributes defined on the current
78      * character.
79      */

80     public Map JavaDoc getAttributes() {
81         return aci.getAttributes();
82     }
83
84     /**
85      * Get the index of the first character following the
86      * run with respect to all attributes containing the current
87      * character.
88      */

89     public int getRunLimit() {
90         return Math.min(aci.getRunLimit(), end);
91     }
92
93     /**
94      * Get the index of the first character following the
95      * run with respect to the given attribute containing the current
96      * character.
97      */

98     public int getRunLimit(AttributedCharacterIterator.Attribute JavaDoc attribute) {
99         return Math.min(aci.getRunLimit(attribute), end);
100     }
101
102     /**
103      * Get the index of the first character following the
104      * run with respect to the given attributes containing the current
105      * character.
106      */

107     public int getRunLimit(Set JavaDoc attributes) {
108         return Math.min(aci.getRunLimit(attributes), end);
109     }
110
111     /**
112      * Get the index of the first character of the run with
113      * respect to all attributes containing the current character.
114      */

115     public int getRunStart() {
116         return Math.max(aci.getRunStart(), begin);
117     }
118
119     /**
120      * Get the index of the first character of the run with
121      * respect to the given attribute containing the current character.
122      * @param attribute The attribute for whose appearance the first offset
123      * is requested.
124      */

125     public int getRunStart(AttributedCharacterIterator.Attribute JavaDoc attribute) {
126         return Math.max(aci.getRunStart(attribute), begin);
127     }
128
129     /**
130      * Get the index of the first character of the run with respect to
131      * the given attributes containing the current character.
132      * @param attributes the Set of attributes which begins at the
133      * returned index.
134      */

135     public int getRunStart(Set JavaDoc attributes) {
136         return Math.max(aci.getRunStart(attributes), begin);
137     }
138
139     //From CharacterIterator
140

141     /**
142      * Create a copy of this iterator
143      */

144     public Object JavaDoc clone() {
145         return new AttributedCharacterSpanIterator(
146                       (AttributedCharacterIterator JavaDoc) aci.clone(), begin, end);
147     }
148
149     /**
150      * Get the character at the current position (as returned
151      * by getIndex()).
152      * <br><b>Specified by:</b> java.text.CharacterIterator.
153      */

154     public char current() {
155         return aci.current();
156     }
157
158     /**
159      * Sets the position to getBeginIndex().
160      * @return the character at the start index of the text.
161      * <br><b>Specified by:</b> java.text.CharacterIterator.
162      */

163     public char first() {
164         return aci.setIndex(begin);
165     }
166
167     /**
168      * Get the start index of the text.
169      * <br><b>Specified by:</b> java.text.CharacterIterator.
170      */

171     public int getBeginIndex() {
172         return begin;
173     }
174
175     /**
176      * Get the end index of the text.
177      * <br><b>Specified by:</b> java.text.CharacterIterator.
178      */

179     public int getEndIndex() {
180         return end;
181     }
182
183     /**
184      * Get the current index.
185      * <br><b>Specified by:</b> java.text.CharacterIterator.
186      */

187     public int getIndex() {
188         return aci.getIndex();
189     }
190
191     /**
192      * Sets the position to getEndIndex()-1 (getEndIndex() if
193      * the text is empty) and returns the character at that position.
194      * <br><b>Specified by:</b> java.text.CharacterIterator.
195      */

196     public char last() {
197         return setIndex(end-1);
198     }
199
200     /**
201      * Increments the iterator's index by one, returning the next character.
202      * @return the character at the new index.
203      * <br><b>Specified by:</b> java.text.CharacterIterator.
204      */

205     public char next() {
206         if (getIndex() < end-1 ) {
207             return aci.next();
208         } else {
209             return setIndex(end);
210         }
211     }
212
213     /**
214      * Decrements the iterator's index by one and returns
215      * the character at the new index.
216      * <br><b>Specified by:</b> java.text.CharacterIterator.
217      */

218     public char previous() {
219         if (getIndex() > begin) {
220             return aci.previous();
221         } else {
222             return CharacterIterator.DONE;
223         }
224     }
225
226     /**
227      * Sets the position to the specified position in the text.
228      * @param position The new (current) index into the text.
229      * @return the character at new index <em>position</em>.
230      * <br><b>Specified by:</b> java.text.CharacterIterator.
231      */

232     public char setIndex(int position) {
233         int ndx = Math.max(position, begin);
234         ndx = Math.min(ndx, end);
235         char c = aci.setIndex(ndx);
236         if (ndx == end) {
237             c = CharacterIterator.DONE;
238         }
239         return c;
240     }
241 }
242
243
244
245
246
247
248
Popular Tags