KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > AttributedText


1 package text;
2
3 import java.awt.Color JavaDoc;
4
5 public class AttributedText
6 {
7    public static int INSTANCES = 0;
8
9    public int foreIndex = 0;
10    public int backIndex = -1;
11
12    public boolean isUnderline = false;
13    public boolean isBold = false;
14    public boolean isReverse = false;
15
16    public int start = 0;
17    public int end = 0;
18
19    public int width; // we're going to cache the width of the string... string width is an expensive operation.
20

21    public AttributedText next;
22    public String JavaDoc text;
23
24    public AttributedText()
25    {
26       INSTANCES++;
27    }
28
29    public void finalize()
30    {
31       INSTANCES--;
32    }
33
34    public AttributedText copyAttributes()
35    {
36       AttributedText temp = new AttributedText();
37       temp.foreIndex = foreIndex;
38       temp.backIndex = backIndex;
39       temp.isUnderline = isUnderline;
40       temp.isBold = isBold;
41       temp.isReverse = isReverse;
42
43       return temp;
44    }
45
46    public AttributedText cloneAttributedText()
47    {
48       AttributedText temp = copyAttributes();
49       temp.width = width;
50       temp.text = text;
51
52       return temp;
53    }
54
55    public AttributedText cloneList()
56    {
57       AttributedText head = cloneAttributedText();
58       AttributedText tail = head;
59       AttributedText temp = this.next;
60
61       while (temp != null)
62       {
63          tail.next = temp.cloneAttributedText();
64
65          tail = tail.next;
66          temp = temp.next;
67       }
68
69       return head;
70    }
71
72    public void setIndent()
73    {
74       start = Integer.MAX_VALUE;
75       end = Integer.MIN_VALUE;
76    }
77
78    public boolean isIndent()
79    {
80       return (start == Integer.MAX_VALUE && end == Integer.MIN_VALUE);
81    }
82
83    public int getWidth()
84    {
85       int width = 0;
86
87       AttributedText temp = this;
88       while (temp != null)
89       {
90          width += temp.width;
91          temp = temp.next;
92       }
93
94       return width;
95    }
96
97    public AttributedText getAttributesAt(int location)
98    {
99       if (location < width)
100       {
101          return this;
102       }
103
104       if (next != null)
105       {
106          return next.getAttributesAt(location - width);
107       }
108
109       return null;
110    }
111
112    public String JavaDoc getText()
113    {
114       StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
115
116       AttributedText temp = this;
117       while (temp != null)
118       {
119          rv.append(temp.text);
120          temp = temp.next;
121       }
122
123       return rv.toString();
124    }
125
126    private static int findIndex(String JavaDoc text, int max_size, int start, int width)
127    {
128       int x = start + 1;
129       int twidth = 0;
130
131       while ((x + x) < text.length() && twidth < max_size)
132       {
133          x += x;
134          twidth = TextSource.fontMetrics.stringWidth(text.substring(0, x)) + width;
135       }
136
137       x = x / 2;
138       twidth = 0;
139
140       while (x < text.length() && twidth < max_size)
141       {
142          twidth = TextSource.fontMetrics.stringWidth(text.substring(0, x)) + width;
143          x++;
144       }
145
146       return x - 1;
147    }
148  
149    public String JavaDoc getRange(int x_start, int x_end)
150    {
151       AttributedText temp = this;
152       StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
153
154       int start, end;
155       int width = 0;
156
157       while (temp != null)
158       {
159          start = width;
160          end = width + temp.width;
161
162          if (x_start > start && x_end <= end)
163          {
164             // special case, string range is just one type of attribute
165
int x = findIndex(temp.text, x_start, 0, width);
166             int y = findIndex(temp.text, x_end, x, width);
167
168             return temp.text.substring(x, y);
169          }
170          else if (x_start <= start && x_end >= end)
171          {
172             // get the whole string...
173
rv.append(temp.text);
174          }
175          else if (x_start <= start && (x_end <= end && x_end > x_start))
176          {
177             // get the substring from 0 .. the end
178
int x = findIndex(temp.text, x_end, 0, width);
179
180             if (x > 1)
181             {
182                rv.append(temp.text.substring(0, x));
183             }
184          }
185          else if (x_start > start && x_start <= end && x_end >= end)
186          {
187             // get the substring from n .. the end
188
int x = findIndex(temp.text, x_end, 0, width);
189
190             rv.append(temp.text.substring(x, temp.text.length()));
191          }
192
193          width += temp.width;
194          temp = temp.next;
195       }
196
197       return rv.toString();
198    }
199
200    public String JavaDoc toString()
201    {
202       String JavaDoc attribs = "";
203       if (isBold) { attribs = attribs + "B"; }
204       if (isUnderline) { attribs = attribs + "U"; }
205       if (isReverse) { attribs = attribs + "R"; }
206
207       String JavaDoc addon = "|";
208
209       if (next != null)
210       {
211          addon = next.toString();
212       }
213
214       if (start == 0 && end == 0)
215       {
216           return "[\"" + text + "\"," + width + "px:" + attribs + ":" + foreIndex + "," + backIndex + "]->" + addon;
217       }
218     
219       return "[\"" + text + "\"," + start + "-" + end + ":" + attribs + ":" + foreIndex + "," + backIndex + "]->" + addon;
220    }
221 }
222
Popular Tags