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