001: /*
002: * @(#)CharacterIteratorFieldDelegate.java 1.6 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package java.text;
027:
028: import java.util.ArrayList;
029:
030: /**
031: * CharacterIteratorFieldDelegate combines the notifications from a Format
032: * into a resulting <code>AttributedCharacterIterator</code>. The resulting
033: * <code>AttributedCharacterIterator</code> can be retrieved by way of
034: * the <code>getIterator</code> method.
035: *
036: * @version 1.6 10/10/06
037: */
038: class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
039: /**
040: * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
041: * for a region > size, a new instance of AttributedString is added to
042: * attributedStrings. Subsequent invocations of <code>formatted</code>
043: * for existing regions result in invoking addAttribute on the existing
044: * AttributedStrings.
045: */
046: private ArrayList attributedStrings;
047: /**
048: * Running count of the number of characters that have
049: * been encountered.
050: */
051: private int size;
052:
053: CharacterIteratorFieldDelegate() {
054: attributedStrings = new ArrayList();
055: }
056:
057: public void formatted(Format.Field attr, Object value, int start,
058: int end, StringBuffer buffer) {
059: if (start != end) {
060: if (start < size) {
061: // Adjust attributes of existing runs
062: int index = size;
063: int asIndex = attributedStrings.size() - 1;
064:
065: while (start < index) {
066: AttributedString as = (AttributedString) attributedStrings
067: .get(asIndex--);
068: int newIndex = index - as.length();
069: int aStart = Math.max(0, start - newIndex);
070:
071: as.addAttribute(attr, value, aStart, Math.min(end
072: - start, as.length() - aStart)
073: + aStart);
074: index = newIndex;
075: }
076: }
077: if (size < start) {
078: // Pad attributes
079: attributedStrings.add(new AttributedString(buffer
080: .substring(size, start)));
081: size = start;
082: }
083: if (size < end) {
084: // Add new string
085: int aStart = Math.max(start, size);
086: AttributedString string = new AttributedString(buffer
087: .substring(aStart, end));
088:
089: string.addAttribute(attr, value);
090: attributedStrings.add(string);
091: size = end;
092: }
093: }
094: }
095:
096: public void formatted(int fieldID, Format.Field attr, Object value,
097: int start, int end, StringBuffer buffer) {
098: formatted(attr, value, start, end, buffer);
099: }
100:
101: /**
102: * Returns an <code>AttributedCharacterIterator</code> that can be used
103: * to iterate over the resulting formatted String.
104: *
105: * @pararm string Result of formatting.
106: */
107: public AttributedCharacterIterator getIterator(String string) {
108: // Add the last AttributedCharacterIterator if necessary
109: // assert(size <= string.length());
110: if (string.length() > size) {
111: attributedStrings.add(new AttributedString(string
112: .substring(size)));
113: size = string.length();
114: }
115: int iCount = attributedStrings.size();
116: AttributedCharacterIterator iterators[] = new AttributedCharacterIterator[iCount];
117:
118: for (int counter = 0; counter < iCount; counter++) {
119: iterators[counter] = ((AttributedString) attributedStrings
120: .get(counter)).getIterator();
121: }
122: return new AttributedString(iterators).getIterator();
123: }
124: }
|