KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > CharacterIteratorFieldDelegate


1 /*
2  * @(#)CharacterIteratorFieldDelegate.java 1.4 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.text;
8
9 import java.util.ArrayList JavaDoc;
10
11 /**
12  * CharacterIteratorFieldDelegate combines the notifications from a Format
13  * into a resulting <code>AttributedCharacterIterator</code>. The resulting
14  * <code>AttributedCharacterIterator</code> can be retrieved by way of
15  * the <code>getIterator</code> method.
16  *
17  * @version 1.4 12/19/03
18  */

19 class CharacterIteratorFieldDelegate implements Format.FieldDelegate JavaDoc {
20     /**
21      * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
22      * for a region > size, a new instance of AttributedString is added to
23      * attributedStrings. Subsequent invocations of <code>formatted</code>
24      * for existing regions result in invoking addAttribute on the existing
25      * AttributedStrings.
26      */

27     private ArrayList JavaDoc attributedStrings;
28     /**
29      * Running count of the number of characters that have
30      * been encountered.
31      */

32     private int size;
33
34
35     CharacterIteratorFieldDelegate() {
36         attributedStrings = new ArrayList JavaDoc();
37     }
38
39     public void formatted(Format.Field JavaDoc attr, Object JavaDoc value, int start, int end,
40                           StringBuffer JavaDoc buffer) {
41         if (start != end) {
42             if (start < size) {
43                 // Adjust attributes of existing runs
44
int index = size;
45                 int asIndex = attributedStrings.size() - 1;
46
47                 while (start < index) {
48                     AttributedString JavaDoc as = (AttributedString JavaDoc)attributedStrings.
49                                            get(asIndex--);
50                     int newIndex = index - as.length();
51                     int aStart = Math.max(0, start - newIndex);
52
53                     as.addAttribute(attr, value, aStart, Math.min(
54                                     end - start, as.length() - aStart) +
55                                     aStart);
56                     index = newIndex;
57                 }
58             }
59             if (size < start) {
60                 // Pad attributes
61
attributedStrings.add(new AttributedString JavaDoc(
62                                           buffer.substring(size, start)));
63                 size = start;
64             }
65             if (size < end) {
66                 // Add new string
67
int aStart = Math.max(start, size);
68                 AttributedString JavaDoc string = new AttributedString JavaDoc(
69                                    buffer.substring(aStart, end));
70
71                 string.addAttribute(attr, value);
72                 attributedStrings.add(string);
73                 size = end;
74             }
75         }
76     }
77
78     public void formatted(int fieldID, Format.Field JavaDoc attr, Object JavaDoc value,
79                           int start, int end, StringBuffer JavaDoc buffer) {
80         formatted(attr, value, start, end, buffer);
81     }
82
83     /**
84      * Returns an <code>AttributedCharacterIterator</code> that can be used
85      * to iterate over the resulting formatted String.
86      *
87      * @pararm string Result of formatting.
88      */

89     public AttributedCharacterIterator JavaDoc getIterator(String JavaDoc string) {
90         // Add the last AttributedCharacterIterator if necessary
91
// assert(size <= string.length());
92
if (string.length() > size) {
93             attributedStrings.add(new AttributedString JavaDoc(
94                                   string.substring(size)));
95             size = string.length();
96         }
97         int iCount = attributedStrings.size();
98         AttributedCharacterIterator JavaDoc iterators[] = new
99                                     AttributedCharacterIterator JavaDoc[iCount];
100
101         for (int counter = 0; counter < iCount; counter++) {
102             iterators[counter] = ((AttributedString JavaDoc)attributedStrings.
103                                   get(counter)).getIterator();
104         }
105         return new AttributedString JavaDoc(iterators).getIterator();
106     }
107 }
108
Popular Tags