1 package text; 2 3 import java.awt.Color ; 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; 21 public AttributedText next; 22 public String 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 getText() 113 { 114 StringBuffer rv = new StringBuffer (); 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 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 getRange(int x_start, int x_end) 150 { 151 AttributedText temp = this; 152 StringBuffer rv = new StringBuffer (); 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 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 rv.append(temp.text); 174 } 175 else if (x_start <= start && (x_end <= end && x_end > x_start)) 176 { 177 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 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 toString() 201 { 202 String attribs = ""; 203 if (isBold) { attribs = attribs + "B"; } 204 if (isUnderline) { attribs = attribs + "U"; } 205 if (isReverse) { attribs = attribs + "R"; } 206 207 String 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 |