49 |
/** |
/** |
50 |
* This class models a <code>String</code> with attributes over various |
* This class models a <code>String</code> with attributes over various |
51 |
* subranges of the string. It allows applications to access this |
* subranges of the string. It allows applications to access this |
52 |
* information via the <code>AttributedCharcterIterator</code> interface. |
* information via the <code>AttributedCharacterIterator</code> interface. |
53 |
* |
* |
54 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn (arenn@urbanophile.com) |
55 |
*/ |
*/ |
166 |
* |
* |
167 |
* @param aci The <code>AttributedCharacterIterator</code> containing the |
* @param aci The <code>AttributedCharacterIterator</code> containing the |
168 |
* text and attribute information. |
* text and attribute information. |
169 |
* @param begin_index The beginning index of the text subrange. |
* @param begin The beginning index of the text subrange. |
170 |
* @param end_index The ending index of the text subrange. |
* @param end The ending index of the text subrange. |
171 |
* @param attributes A list of attributes to include from the iterator, or |
* @param attributes A list of attributes to include from the iterator, or |
172 |
* <code>null</code> to include all attributes. |
* <code>null</code> to include all attributes. |
173 |
*/ |
*/ |
174 |
public AttributedString(AttributedCharacterIterator aci, int begin_index, |
public AttributedString(AttributedCharacterIterator aci, int begin, int end, |
175 |
int end_index, AttributedCharacterIterator.Attribute[] attributes) |
AttributedCharacterIterator.Attribute[] attributes) |
176 |
{ |
{ |
177 |
// Validate some arguments |
// Validate some arguments |
178 |
if ((begin_index < 0) || (end_index < begin_index)) |
if ((begin < 0) || (end < begin) || end > aci.getEndIndex()) |
179 |
throw new IllegalArgumentException("Bad index values"); |
throw new IllegalArgumentException("Bad index values"); |
180 |
|
|
181 |
StringBuffer sb = new StringBuffer(""); |
StringBuffer sb = new StringBuffer(""); |
186 |
all_attribs.retainAll(Arrays.asList(attributes)); |
all_attribs.retainAll(Arrays.asList(attributes)); |
187 |
|
|
188 |
// Loop through and extract the attributes |
// Loop through and extract the attributes |
189 |
char c = aci.setIndex(begin_index); |
char c = aci.setIndex(begin); |
190 |
|
|
191 |
ArrayList accum = new ArrayList(); |
ArrayList accum = new ArrayList(); |
192 |
do |
do |
209 |
int rl = aci.getRunLimit(attrib); |
int rl = aci.getRunLimit(attrib); |
210 |
if (rl == -1) |
if (rl == -1) |
211 |
continue; |
continue; |
212 |
if (rl > end_index) |
if (rl > end) |
213 |
rl = end_index; |
rl = end; |
214 |
rl -= begin_index; |
rl -= begin; |
215 |
|
|
216 |
// Check to see if we already processed this one |
// Check to see if we already processed this one |
217 |
int rs = aci.getRunStart(attrib); |
int rs = aci.getRunStart(attrib); |
218 |
if ((rs < aci.getIndex()) && (aci.getIndex() != begin_index)) |
if ((rs < aci.getIndex()) && (aci.getIndex() != begin)) |
219 |
continue; |
continue; |
220 |
|
|
221 |
// If the attribute run starts before the beginning index, we |
// If the attribute run starts before the beginning index, we |
222 |
// need to junk it if it is an Annotation. |
// need to junk it if it is an Annotation. |
223 |
Object attrib_obj = aci.getAttribute(attrib); |
Object attrib_obj = aci.getAttribute(attrib); |
224 |
if (rs < begin_index) |
if (rs < begin) |
225 |
{ |
{ |
226 |
if (attrib_obj instanceof Annotation) |
if (attrib_obj instanceof Annotation) |
227 |
continue; |
continue; |
228 |
|
|
229 |
rs = begin_index; |
rs = begin; |
230 |
} |
} |
231 |
else |
else |
232 |
{ |
{ |
233 |
rs -= begin_index; |
rs -= begin; |
234 |
} |
} |
235 |
|
|
236 |
// Create a map object. Yes this will only contain one attribute |
// Create a map object. Yes this will only contain one attribute |
269 |
* |
* |
270 |
* @param attrib The attribute to add. |
* @param attrib The attribute to add. |
271 |
* @param value The value of the attribute, which may be <code>null</code>. |
* @param value The value of the attribute, which may be <code>null</code>. |
272 |
* @param begin_index The beginning index of the subrange. |
* @param begin The beginning index of the subrange. |
273 |
* @param end_index The ending index of the subrange. |
* @param end The ending index of the subrange. |
274 |
* |
* |
275 |
* @exception IllegalArgumentException If attribute is <code>null</code> or |
* @exception IllegalArgumentException If attribute is <code>null</code> or |
276 |
* the subrange is not valid. |
* the subrange is not valid. |
277 |
*/ |
*/ |
278 |
public void addAttribute(AttributedCharacterIterator.Attribute attrib, |
public void addAttribute(AttributedCharacterIterator.Attribute attrib, |
279 |
Object value, int begin_index, int end_index) |
Object value, int begin, int end) |
280 |
{ |
{ |
281 |
if (attrib == null) |
if (attrib == null) |
282 |
throw new IllegalArgumentException("null attribute"); |
throw new IllegalArgumentException("null attribute"); |
283 |
|
if (end <= begin) |
284 |
|
throw new IllegalArgumentException("Requires end > begin"); |
285 |
HashMap hm = new HashMap(); |
HashMap hm = new HashMap(); |
286 |
hm.put(attrib, value); |
hm.put(attrib, value); |
287 |
|
|
288 |
addAttributes(hm, begin_index, end_index); |
addAttributes(hm, begin, end); |
289 |
} |
} |
290 |
|
|
291 |
/** |
/** |
296 |
* @param begin_index The beginning index. |
* @param begin_index The beginning index. |
297 |
* @param end_index The ending index |
* @param end_index The ending index |
298 |
* |
* |
299 |
* @throws IllegalArgumentException If the list is <code>null</code> or the |
* @throws NullPointerException if <code>attributes</code> is |
300 |
* subrange is not valid. |
* <code>null</code>. |
301 |
|
* @throws IllegalArgumentException if the subrange is not valid. |
302 |
*/ |
*/ |
303 |
public void addAttributes(Map attributes, int begin_index, int end_index) |
public void addAttributes(Map attributes, int begin_index, int end_index) |
304 |
{ |
{ |
305 |
if (attributes == null) |
if (attributes == null) |
306 |
throw new IllegalArgumentException("null attribute"); |
throw new NullPointerException("null attribute"); |
307 |
|
|
308 |
if ((begin_index < 0) || (end_index > sci.getEndIndex()) || |
if ((begin_index < 0) || (end_index > sci.getEndIndex()) || |
309 |
(end_index < begin_index)) |
(end_index <= begin_index)) |
310 |
throw new IllegalArgumentException("bad range"); |
throw new IllegalArgumentException("bad range"); |
311 |
|
|
312 |
AttributeRange[] new_list = new AttributeRange[attribs.length + 1]; |
AttributeRange[] new_list = new AttributeRange[attribs.length + 1]; |