jdk/src/share/classes/java/text/CharacterIteratorFieldDelegate.java
author naoto
Tue, 31 Aug 2010 11:27:10 -0700
changeset 6489 9e7015635425
parent 5506 202f599c92aa
child 12848 da701d422d2c
permissions -rw-r--r--
4700857: RFE: separating user locale and user interface locale Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * CharacterIteratorFieldDelegate combines the notifications from a Format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * into a resulting <code>AttributedCharacterIterator</code>. The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <code>AttributedCharacterIterator</code> can be retrieved by way of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the <code>getIterator</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * for a region > size, a new instance of AttributedString is added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * attributedStrings. Subsequent invocations of <code>formatted</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * for existing regions result in invoking addAttribute on the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * AttributedStrings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private ArrayList attributedStrings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Running count of the number of characters that have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * been encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    CharacterIteratorFieldDelegate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        attributedStrings = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public void formatted(Format.Field attr, Object value, int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                          StringBuffer buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (start != end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if (start < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                // Adjust attributes of existing runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                int index = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                int asIndex = attributedStrings.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                while (start < index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                    AttributedString as = (AttributedString)attributedStrings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                           get(asIndex--);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    int newIndex = index - as.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    int aStart = Math.max(0, start - newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    as.addAttribute(attr, value, aStart, Math.min(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                    end - start, as.length() - aStart) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                    aStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    index = newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (size < start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // Pad attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                attributedStrings.add(new AttributedString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                          buffer.substring(size, start)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                size = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (size < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // Add new string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                int aStart = Math.max(start, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                AttributedString string = new AttributedString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                   buffer.substring(aStart, end));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                string.addAttribute(attr, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                attributedStrings.add(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                size = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public void formatted(int fieldID, Format.Field attr, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                          int start, int end, StringBuffer buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        formatted(attr, value, start, end, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Returns an <code>AttributedCharacterIterator</code> that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * to iterate over the resulting formatted String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @pararm string Result of formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public AttributedCharacterIterator getIterator(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // Add the last AttributedCharacterIterator if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // assert(size <= string.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (string.length() > size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            attributedStrings.add(new AttributedString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                  string.substring(size)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            size = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int iCount = attributedStrings.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        AttributedCharacterIterator iterators[] = new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                    AttributedCharacterIterator[iCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int counter = 0; counter < iCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            iterators[counter] = ((AttributedString)attributedStrings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                  get(counter)).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return new AttributedString(iterators).getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}