1:
37: package ;
38:
39: import ;
40:
41: import ;
42: import ;
43: import ;
44:
45:
53: public class AttributedFormatBuffer implements FormatBuffer
54: {
55: private final CPStringBuilder buffer;
56: private final ArrayList ranges;
57: private final ArrayList attributes;
58: private int[] a_ranges;
59: private HashMap[] a_attributes;
60: private int startingRange;
61: AttributedCharacterIterator.Attribute defaultAttr;
62:
63:
67: public AttributedFormatBuffer(CPStringBuilder buffer)
68: {
69: this.buffer = new CPStringBuilder(buffer);
70: this.ranges = new ArrayList();
71: this.attributes = new ArrayList();
72: this.defaultAttr = null;
73: if (buffer.length() != 0)
74: {
75: this.startingRange = buffer.length();
76: addAttribute(buffer.length(), null);
77: }
78: else
79: this.startingRange = -1;
80: }
81:
82: public AttributedFormatBuffer(int prebuffer)
83: {
84: this(new CPStringBuilder(prebuffer));
85: }
86:
87: public AttributedFormatBuffer()
88: {
89: this(10);
90: }
91:
92:
100: private final void addAttribute(int new_range, AttributedCharacterIterator.Attribute attr)
101: {
102: HashMap map;
103:
104: if (attr != null)
105: {
106: map = new HashMap();
107: map.put(attr, attr);
108: attributes.add(map);
109: }
110: else
111: attributes.add(null);
112:
113: ranges.add(new Integer(new_range));
114: }
115:
116: public void append(String s)
117: {
118: if (startingRange < 0)
119: startingRange = 0;
120: buffer.append(s);
121: }
122:
123: public void append(String s, AttributedCharacterIterator.Attribute attr)
124: {
125: setDefaultAttribute(attr);
126: startingRange = buffer.length();
127: append(s);
128: setDefaultAttribute(null);
129: }
130:
131: public void append(String s, int[] ranges, HashMap[] attrs)
132: {
133: int curPos = buffer.length();
134:
135: setDefaultAttribute(null);
136: if (ranges != null)
137: {
138: for (int i = 0; i < ranges.length; i++)
139: {
140: this.ranges.add(new Integer(ranges[i] + curPos));
141: this.attributes.add(attrs[i]);
142: }
143: }
144: startingRange = buffer.length();
145: buffer.append(s);
146: }
147:
148: public void append(char c)
149: {
150: if (startingRange < 0)
151: startingRange = buffer.length();
152: buffer.append(c);
153: }
154:
155: public void append(char c, AttributedCharacterIterator.Attribute attr)
156: {
157: setDefaultAttribute(attr);
158: buffer.append(c);
159: setDefaultAttribute(null);
160: }
161:
162: public void setDefaultAttribute(AttributedCharacterIterator.Attribute attr)
163: {
164: if (attr == defaultAttr)
165: return;
166:
167: int currentPos = buffer.length();
168:
169: if (startingRange != currentPos && startingRange >= 0)
170: {
171: addAttribute(currentPos, defaultAttr);
172: }
173: defaultAttr = attr;
174: startingRange = currentPos;
175: }
176:
177: public AttributedCharacterIterator.Attribute getDefaultAttribute()
178: {
179: return defaultAttr;
180: }
181:
182: public void cutTail(int length)
183: {
184: buffer.setLength(buffer.length()-length);
185: }
186:
187: public int length()
188: {
189: return buffer.length();
190: }
191:
192: public void clear()
193: {
194: buffer.setLength(0);
195: ranges.clear();
196: attributes.clear();
197: defaultAttr = null;
198: startingRange = -1;
199: }
200:
201:
205: public void sync()
206: {
207: if (startingRange < 0 || startingRange == buffer.length())
208: return;
209:
210: addAttribute(buffer.length(), defaultAttr);
211:
212: a_ranges = new int[ranges.size()];
213: for (int i = 0; i < a_ranges.length; i++)
214: a_ranges[i] = ((Integer)(ranges.get (i))).intValue();
215:
216: a_attributes = new HashMap[attributes.size()];
217: System.arraycopy(attributes.toArray(), 0, a_attributes, 0, a_attributes.length);
218: }
219:
220:
226: public CPStringBuilder getBuffer()
227: {
228: return buffer;
229: }
230:
231:
236: public int[] getRanges()
237: {
238: return a_ranges;
239: }
240:
241:
247: public HashMap[] getAttributes()
248: {
249: return a_attributes;
250: }
251: }