1 package text; 2 3 import rero.util.*; 4 5 public class AttributedString 6 { 7 public static final char bold = (char)2; 8 public static final char underline = (char)31; 9 public static final char color = (char)3; 10 public static final char cancel = (char)15; 11 public static final char reverse = (char)22; 12 public static final char tab = '\t'; 13 14 protected AttributedText attrs; protected TokenizedString tokens; protected String text; 18 20 21 public AttributedString(String _text, AttributedText _attrs) 22 { 23 this(_text, _attrs, " "); 24 } 25 26 27 public AttributedString(String _text, AttributedText _attrs, String delimeter) 28 { 29 text = _text; 30 attrs = _attrs; 31 tokens = new TokenizedString(text, delimeter); 32 } 34 35 39 40 public String 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 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 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 119 public static AttributedString CreateAttributedString(String text) 120 { 121 return CreateAttributedString(text, " "); 122 } 123 124 public static AttributedString CreateAttributedString(String text, String delim) 125 { 126 AttributedText current = new AttributedText(); 127 StringBuffer result = new StringBuffer (); 128 StringBuffer stripped = new StringBuffer (); 129 130 AttributedText head = current; 131 132 char[] data = text.toCharArray(); 133 134 int fore, back; 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 (); 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; fore = current.foreIndex; 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 (); 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 (); 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.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 (); 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 (); 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 |