1:
37: package ;
38:
39: import ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47: import java.text.AttributedCharacterIterator.Attribute;
48:
49:
57: public class AttributedFormatBuffer implements FormatBuffer
58: {
59: private final CPStringBuilder buffer;
60: private final ArrayList<Integer> ranges;
61: private final ArrayList<Map<Attribute,Object>> attributes;
62: private int[] aRanges;
63: private List<Map<Attribute,Object>> aAttributes;
64: private int startingRange;
65: Attribute defaultAttr;
66:
67:
71: public AttributedFormatBuffer(CPStringBuilder buffer)
72: {
73: this.buffer = new CPStringBuilder(buffer);
74: this.ranges = new ArrayList<Integer>();
75: this.attributes = new ArrayList<Map<Attribute,Object>>();
76: this.defaultAttr = null;
77: if (buffer.length() != 0)
78: {
79: this.startingRange = buffer.length();
80: addAttribute(buffer.length(), null);
81: }
82: else
83: this.startingRange = -1;
84: }
85:
86: public AttributedFormatBuffer(int prebuffer)
87: {
88: this(new CPStringBuilder(prebuffer));
89: }
90:
91: public AttributedFormatBuffer()
92: {
93: this(10);
94: }
95:
96:
104: private final void addAttribute(int newRange, Attribute attr)
105: {
106: Map<Attribute,Object> map;
107:
108: if (attr != null)
109: {
110: map = new HashMap<Attribute,Object>();
111: map.put(attr, attr);
112: attributes.add(map);
113: }
114: else
115: attributes.add(null);
116:
117: ranges.add(Integer.valueOf(newRange));
118: }
119:
120: public void append(String s)
121: {
122: if (startingRange < 0)
123: startingRange = 0;
124: buffer.append(s);
125: }
126:
127: public void append(String s, Attribute attr)
128: {
129: setDefaultAttribute(attr);
130: startingRange = buffer.length();
131: append(s);
132: setDefaultAttribute(null);
133: }
134:
135: public void append(String s, int[] ranges, List<Map<Attribute,Object>> attrs)
136: {
137: int curPos = buffer.length();
138:
139: setDefaultAttribute(null);
140: if (ranges != null)
141: {
142: for (int i = 0; i < ranges.length; i++)
143: {
144: this.ranges.add(Integer.valueOf(ranges[i] + curPos));
145: this.attributes.add(attrs.get(i));
146: }
147: }
148: startingRange = buffer.length();
149: buffer.append(s);
150: }
151:
152: public void append(char c)
153: {
154: if (startingRange < 0)
155: startingRange = buffer.length();
156: buffer.append(c);
157: }
158:
159: public void append(char c, Attribute attr)
160: {
161: setDefaultAttribute(attr);
162: buffer.append(c);
163: setDefaultAttribute(null);
164: }
165:
166: public void setDefaultAttribute(Attribute attr)
167: {
168: if (attr == defaultAttr)
169: return;
170:
171: int currentPos = buffer.length();
172:
173: if (startingRange != currentPos && startingRange >= 0)
174: {
175: addAttribute(currentPos, defaultAttr);
176: }
177: defaultAttr = attr;
178: startingRange = currentPos;
179: }
180:
181: public Attribute getDefaultAttribute()
182: {
183: return defaultAttr;
184: }
185:
186: public void cutTail(int length)
187: {
188: buffer.setLength(buffer.length()-length);
189: }
190:
191: public int length()
192: {
193: return buffer.length();
194: }
195:
196: public void clear()
197: {
198: buffer.setLength(0);
199: ranges.clear();
200: attributes.clear();
201: defaultAttr = null;
202: startingRange = -1;
203: }
204:
205:
209: public void sync()
210: {
211: if (startingRange < 0 || startingRange == buffer.length())
212: return;
213:
214: addAttribute(buffer.length(), defaultAttr);
215:
216: aRanges = new int[ranges.size()];
217: for (int i = 0; i < aRanges.length; i++)
218: aRanges[i] = ranges.get (i).intValue();
219:
220: aAttributes = new ArrayList<Map<Attribute,Object>>(attributes);
221: }
222:
223:
229: public CPStringBuilder getBuffer()
230: {
231: return buffer;
232: }
233:
234:
239: public int[] getRanges()
240: {
241: return aRanges;
242: }
243:
244:
250: public List<Map<Attribute,Object>> getAttributes()
251: {
252: return aAttributes;
253: }
254: }