KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > AttributedString


1 package text;
2
3 import rero.util.*;
4
5 public class AttributedString
6 {
7     public static final char bold = (char)2; /** constant for defacto irc standard bold character */
8     public static final char underline = (char)31; /** constant for defacto irc standard underline character */
9     public static final char color = (char)3; /** constant for defacto irc standard color character (thanks mIRC) */
10     public static final char cancel = (char)15; /** constant for defacto irc standard cancel character */
11     public static final char reverse = (char)22; /** constant for defacto irc standard reverse character */
12     public static final char tab = '\t'; /** constant for beloved tab character */
13
14     protected AttributedText attrs; // linked list of string and its attributes
15
protected TokenizedString tokens; // cached tokenized version of the string (possibly to not be cached)
16
protected String JavaDoc text; // stripped version of the string
17

18 // public static int total_instances = 0;
19

20     /** constructor called for an already parsed attributed string, if you want to create a new object use CreateAttributedString */
21     public AttributedString(String JavaDoc _text, AttributedText _attrs)
22     {
23         this(_text, _attrs, " ");
24     }
25
26     /** constructor called for an already parsed attributed string, if you want to create a new object use CreateAttributedString */
27     public AttributedString(String JavaDoc _text, AttributedText _attrs, String JavaDoc delimeter)
28     {
29         text = _text;
30         attrs = _attrs;
31         tokens = new TokenizedString(text, delimeter);
32 // total_instances++;
33
}
34
35 /* protected void finalize()
36     {
37         total_instances--;
38     } */

39
40     public String JavaDoc getText()
41     {
42         return text;
43     }
44
45     public TokenizedString getTokens()
46     {
47         return tokens;
48     }
49
50     public AttributedText getAttributedText()
51     {
52         return attrs;
53     }
54
55     public AttributedText substring(int start, int end)
56     {
57         return substring(attrs, start, end);
58     }
59
60     public void assignWidths()
61     {
62         AttributedText head = attrs;
63         while (head != null)
64         {
65            head.width = TextSource.fontMetrics.stringWidth(head.text);
66            head = head.next;
67         }
68
69     }
70
71     // adjust start and end as we go...
72

73     public AttributedText substring(AttributedText original, int start, int end)
74     {
75         AttributedText head;
76         AttributedText temp = original;
77
78         while (temp != null)
79         {
80            if (start > temp.start && end <= temp.end)
81            {
82               // special case, string range is just one type of attribute
83
head = temp.copyAttributes();
84               head.text = text.substring(start, end);
85               head.width = TextSource.fontMetrics.stringWidth(head.text);
86               return head;
87            }
88            else if (start <= temp.start && end >= temp.end)
89            {
90               head = temp.copyAttributes();
91               head.text = temp.text;
92               head.width = TextSource.fontMetrics.stringWidth(head.text);
93               head.next = substring(temp.next, start, end);
94               return head;
95            }
96            else if (start <= temp.start && (end <= temp.end && end > temp.start))
97            {
98               head = temp.copyAttributes();
99               head.text = text.substring(temp.start, end);
100               head.width = TextSource.fontMetrics.stringWidth(head.text);
101               return head;
102            }
103            else if (start > temp.start && start <= temp.end && end >= temp.end)
104            {
105               head = temp.copyAttributes();
106               head.text = text.substring(start, temp.end);
107               head.width = TextSource.fontMetrics.stringWidth(head.text);
108               head.next = substring(temp.next, start, end);
109               return head;
110            }
111
112            temp = temp.next;
113         }
114        
115         return null;
116     }
117
118    /** attributed string parser, it just doesn't get to be more fun than this */
119    public static AttributedString CreateAttributedString(String JavaDoc text)
120    {
121       return CreateAttributedString(text, " ");
122    }
123
124    public static AttributedString CreateAttributedString(String JavaDoc text, String JavaDoc delim)
125    {
126       AttributedText current = new AttributedText();
127       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
128       StringBuffer JavaDoc stripped = new StringBuffer JavaDoc();
129
130       AttributedText head = current;
131
132       char[] data = text.toCharArray();
133
134       int fore, back; // foreground and background color indices (temporary variables)
135

136       for (int x = 0; x < data.length; x++)
137       {
138          switch (data[x])
139          {
140             case bold:
141               if (result.length() > 0)
142               {
143                  current.text = result.toString();
144                  current.end = current.start + current.text.length();
145                  result = new StringBuffer JavaDoc();
146           
147                  current.next = current.copyAttributes();
148                  current.next.start = current.end;
149                  current = current.next;
150               }
151               current.isBold = !current.isBold;
152               break;
153             case color:
154               back = current.backIndex; // take a cue from the current background.
155
fore = current.foreIndex; // take a cue from the current foreground.
156

157               if ((x + 1) < data.length && Character.isDigit(data[x+1]))
158               {
159                  if ((x + 2) < data.length && Character.isDigit(data[x+2]))
160                  {
161                     fore = (Character.digit(data[x+1], 10) * 10) + Character.digit(data[x+2], 10);
162                     x+=2;
163                  }
164                  else
165                  {
166                     fore = Character.digit(data[x+1], 10);
167                     x++;
168                  }
169
170                  if ((x + 2) < data.length && data[x+1] == ',' && Character.isDigit(data[x+2]))
171                  {
172                     x++;
173
174                     if ((x + 2) < data.length && Character.isDigit(data[x+2]))
175                     {
176                        back = (Character.digit(data[x+1], 10) * 10) + Character.digit(data[x+2], 10);
177                        x+=2;
178                     }
179                     else
180                     {
181                        back = Character.digit(data[x+1], 10);
182                        x++;
183                     }
184                  }
185               }
186
187               if (fore != current.foreIndex || back != current.backIndex)
188               {
189                  if (result.length() > 0)
190                  {
191                     current.text = result.toString();
192                     current.end = current.start + current.text.length();
193                     result = new StringBuffer JavaDoc();
194
195                     current.next = current.copyAttributes();
196                     current.next.start = current.end;
197                     current = current.next;
198                  }
199
200                  current.foreIndex = fore;
201                  current.backIndex = back;
202               }
203               break;
204             case cancel:
205               if (result.length() > 0)
206               {
207                  current.text = result.toString();
208                  current.end = current.start + current.text.length();
209                  result = new StringBuffer JavaDoc();
210
211                  current.next = (new AttributedText()).copyAttributes();
212                  current.next.foreIndex = current.foreIndex;
213                  current.next.start = current.end;
214                  current = current.next;
215               }
216               else
217               {
218 // current = (new AttributedText()).copyAttributes();
219
current.isBold = false;
220                  current.isUnderline = false;
221                  current.isReverse = false;
222                  current.backIndex = -1;
223                  current.foreIndex = 0;
224               }
225               break;
226             case reverse:
227               if (result.length() > 0)
228               {
229                  current.text = result.toString();
230                  current.end = current.start + current.text.length();
231                  result = new StringBuffer JavaDoc();
232
233                  current.next = current.copyAttributes();
234                  current.next.start = current.end;
235                  current = current.next;
236               }
237               current.isReverse = !current.isReverse;
238               break;
239             case underline:
240               if (result.length() > 0)
241               {
242                  current.text = result.toString();
243                  current.end = current.start + current.text.length();
244                  result = new StringBuffer JavaDoc();
245
246                  current.next = current.copyAttributes();
247                  current.next.start = current.end;
248                  current = current.next;
249               }
250               current.isUnderline = !current.isUnderline;
251               break;
252             case tab:
253               result.append(" ");
254               stripped.append(" ");
255               break;
256             default:
257               result.append(data[x]);
258               stripped.append(data[x]);
259          }
260       }
261       current.text = result.toString();
262       current.end = current.start + current.text.length();
263  
264       return new AttributedString(stripped.toString(), head, delim);
265    }
266 }
267
Popular Tags