2 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
27 import java.util.ArrayList;
30 * CharacterIteratorFieldDelegate combines the notifications from a Format
31 * into a resulting <code>AttributedCharacterIterator</code>. The resulting
32 * <code>AttributedCharacterIterator</code> can be retrieved by way of
33 * the <code>getIterator</code> method.
36 class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
38 * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
39 * for a region > size, a new instance of AttributedString is added to
40 * attributedStrings. Subsequent invocations of <code>formatted</code>
41 * for existing regions result in invoking addAttribute on the existing
44 private ArrayList attributedStrings;
46 * Running count of the number of characters that have
52 CharacterIteratorFieldDelegate() {
53 attributedStrings = new ArrayList();
56 public void formatted(Format.Field attr, Object value, int start, int end,
57 StringBuffer buffer) {
60 // Adjust attributes of existing runs
62 int asIndex = attributedStrings.size() - 1;
64 while (start < index) {
65 AttributedString as = (AttributedString)attributedStrings.
67 int newIndex = index - as.length();
68 int aStart = Math.max(0, start - newIndex);
70 as.addAttribute(attr, value, aStart, Math.min(
71 end - start, as.length() - aStart) +
78 attributedStrings.add(new AttributedString(
79 buffer.substring(size, start)));
84 int aStart = Math.max(start, size);
85 AttributedString string = new AttributedString(
86 buffer.substring(aStart, end));
88 string.addAttribute(attr, value);
89 attributedStrings.add(string);
95 public void formatted(int fieldID, Format.Field attr, Object value,
96 int start, int end, StringBuffer buffer) {
97 formatted(attr, value, start, end, buffer);
101 * Returns an <code>AttributedCharacterIterator</code> that can be used
102 * to iterate over the resulting formatted String.
104 * @pararm string Result of formatting.
106 public AttributedCharacterIterator getIterator(String string) {
107 // Add the last AttributedCharacterIterator if necessary
108 // assert(size <= string.length());
109 if (string.length() > size) {
110 attributedStrings.add(new AttributedString(
111 string.substring(size)));
112 size = string.length();
114 int iCount = attributedStrings.size();
115 AttributedCharacterIterator iterators[] = new
116 AttributedCharacterIterator[iCount];
118 for (int counter = 0; counter < iCount; counter++) {
119 iterators[counter] = ((AttributedString)attributedStrings.
120 get(counter)).getIterator();
122 return new AttributedString(iterators).getIterator();