1 7 package java.text; 8 9 import java.util.ArrayList ; 10 11 19 class CharacterIteratorFieldDelegate implements Format.FieldDelegate { 20 27 private ArrayList attributedStrings; 28 32 private int size; 33 34 35 CharacterIteratorFieldDelegate() { 36 attributedStrings = new ArrayList (); 37 } 38 39 public void formatted(Format.Field attr, Object value, int start, int end, 40 StringBuffer buffer) { 41 if (start != end) { 42 if (start < size) { 43 int index = size; 45 int asIndex = attributedStrings.size() - 1; 46 47 while (start < index) { 48 AttributedString as = (AttributedString )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 attributedStrings.add(new AttributedString ( 62 buffer.substring(size, start))); 63 size = start; 64 } 65 if (size < end) { 66 int aStart = Math.max(start, size); 68 AttributedString string = new AttributedString ( 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 attr, Object value, 79 int start, int end, StringBuffer buffer) { 80 formatted(attr, value, start, end, buffer); 81 } 82 83 89 public AttributedCharacterIterator getIterator(String string) { 90 if (string.length() > size) { 93 attributedStrings.add(new AttributedString ( 94 string.substring(size))); 95 size = string.length(); 96 } 97 int iCount = attributedStrings.size(); 98 AttributedCharacterIterator iterators[] = new 99 AttributedCharacterIterator [iCount]; 100 101 for (int counter = 0; counter < iCount; counter++) { 102 iterators[counter] = ((AttributedString )attributedStrings. 103 get(counter)).getIterator(); 104 } 105 return new AttributedString (iterators).getIterator(); 106 } 107 } 108 | Popular Tags |