1. /*
  2. * @(#)CharacterIteratorFieldDelegate.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.text;
  8. import java.util.ArrayList;
  9. /**
  10. * CharacterIteratorFieldDelegate combines the notifications from a Format
  11. * into a resulting <code>AttributedCharacterIterator</code>. The resulting
  12. * <code>AttributedCharacterIterator</code> can be retrieved by way of
  13. * the <code>getIterator</code> method.
  14. *
  15. * @version 1.3 01/23/03
  16. */
  17. class CharacterIteratorFieldDelegate implements Format.FieldDelegate {
  18. /**
  19. * Array of AttributeStrings. Whenever <code>formatted</code> is invoked
  20. * for a region > size, a new instance of AttributedString is added to
  21. * attributedStrings. Subsequent invocations of <code>formatted</code>
  22. * for existing regions result in invoking addAttribute on the existing
  23. * AttributedStrings.
  24. */
  25. private ArrayList attributedStrings;
  26. /**
  27. * Running count of the number of characters that have
  28. * been encountered.
  29. */
  30. private int size;
  31. CharacterIteratorFieldDelegate() {
  32. attributedStrings = new ArrayList();
  33. }
  34. public void formatted(Format.Field attr, Object value, int start, int end,
  35. StringBuffer buffer) {
  36. if (start != end) {
  37. if (start < size) {
  38. // Adjust attributes of existing runs
  39. int index = size;
  40. int asIndex = attributedStrings.size() - 1;
  41. while (start < index) {
  42. AttributedString as = (AttributedString)attributedStrings.
  43. get(asIndex--);
  44. int newIndex = index - as.length();
  45. int aStart = Math.max(0, start - newIndex);
  46. as.addAttribute(attr, value, aStart, Math.min(
  47. end - start, as.length() - aStart) +
  48. aStart);
  49. index = newIndex;
  50. }
  51. }
  52. if (size < start) {
  53. // Pad attributes
  54. attributedStrings.add(new AttributedString(
  55. buffer.substring(size, start)));
  56. size = start;
  57. }
  58. if (size < end) {
  59. // Add new string
  60. int aStart = Math.max(start, size);
  61. AttributedString string = new AttributedString(
  62. buffer.substring(aStart, end));
  63. string.addAttribute(attr, value);
  64. attributedStrings.add(string);
  65. size = end;
  66. }
  67. }
  68. }
  69. public void formatted(int fieldID, Format.Field attr, Object value,
  70. int start, int end, StringBuffer buffer) {
  71. formatted(attr, value, start, end, buffer);
  72. }
  73. /**
  74. * Returns an <code>AttributedCharacterIterator</code> that can be used
  75. * to iterate over the resulting formatted String.
  76. *
  77. * @pararm string Result of formatting.
  78. */
  79. public AttributedCharacterIterator getIterator(String string) {
  80. // Add the last AttributedCharacterIterator if necessary
  81. // assert(size <= string.length());
  82. if (string.length() > size) {
  83. attributedStrings.add(new AttributedString(
  84. string.substring(size)));
  85. size = string.length();
  86. }
  87. int iCount = attributedStrings.size();
  88. AttributedCharacterIterator iterators[] = new
  89. AttributedCharacterIterator[iCount];
  90. for (int counter = 0; counter < iCount; counter++) {
  91. iterators[counter] = ((AttributedString)attributedStrings.
  92. get(counter)).getIterator();
  93. }
  94. return new AttributedString(iterators).getIterator();
  95. }
  96. }