1 22 23 27 28 package org.netbeans.lib.terminalemulator; 29 30 class Line { 31 public int glyph_glyph; 32 public int glyph_rendition; 36 private char buf[]; private int attr[]; 39 private int capacity; private int length; 43 44 public Line() { 45 reset(); 46 } 47 48 public void reset() { 49 length = 0; 50 capacity = 32; 51 buf = new char[capacity]; 52 attr = null; 53 glyph_glyph = 0; 54 glyph_rendition = 0; 55 wrapped = false; 56 about_to_wrap = false; 57 } 58 59 60 public int capacity() { 61 return capacity; 62 } 63 64 69 public int length() { 70 return length; 71 } 72 73 public boolean hasAttributes() { 74 return attr != null; 75 } 76 77 public void setWrapped(boolean wrapped) { 78 this.wrapped = wrapped; 79 } 80 public boolean isWrapped() { 81 return wrapped; 82 } 83 private boolean wrapped; 85 86 87 88 public boolean setAboutToWrap(boolean about_to_wrap) { 89 boolean old_about_to_wrap = about_to_wrap; 90 this.about_to_wrap = about_to_wrap; 91 return old_about_to_wrap; 92 } 93 public boolean isAboutToWrap() { 94 return about_to_wrap; 95 } 96 private boolean about_to_wrap; 98 99 100 104 private boolean haveAttributes(int a) { 105 if (attr == null && a != 0) { 106 attr = new int[capacity]; 107 } 108 return attr != null; 109 } 110 111 112 public char [] charArray() { 113 return buf; 114 } 115 116 public int [] attrArray() { 117 return attr; 118 } 119 120 121 public byte width(MyFontMetrics metrics, int at) { 122 if (at >= capacity) 123 return 1; 124 return (byte) metrics.wcwidth(buf[at]); 125 } 126 127 130 public int cellToBuf(MyFontMetrics metrics, int target_col) { 131 if (metrics.isMultiCell()) { 132 int bc = 0; 133 int vc = 0; 134 for(;;) { 135 int w = width(metrics, bc); 136 if (vc + w - 1 >= target_col) 137 break; 138 vc += w; 139 bc++; 140 } 141 return bc; 142 } else { 143 return target_col; 144 } 145 } 146 147 150 public int bufToCell(MyFontMetrics metrics, int target_col) { 151 if (metrics.isMultiCell()) { 152 int vc = 0; 153 for (int bc = 0; bc < target_col; bc++) { 154 vc += width(metrics, bc); 155 } 156 return vc; 157 } else { 158 return target_col; 159 } 160 } 161 162 163 164 public StringBuffer stringBuffer() { 165 StringBuffer sb = new StringBuffer (length); 168 return sb.append(buf, 0, length); 169 } 170 171 174 private void ensureCapacity(Term term, int min_capacity) { 175 176 term.noteColumn(this, min_capacity); 177 178 if (min_capacity <= capacity) 179 return; 181 int new_capacity = (length+1) * 2; 183 if (new_capacity < 0) 184 new_capacity = Integer.MAX_VALUE; 185 else if (min_capacity > new_capacity) 186 new_capacity = min_capacity; 187 188 189 char new_buf[] = new char[new_capacity]; 190 System.arraycopy(buf, 0, new_buf, 0, length); 191 buf = new_buf; 192 193 if (attr != null) { 194 int new_attr[] = new int[new_capacity]; 195 System.arraycopy(attr, 0, new_attr, 0, length); 196 attr = new_attr; 197 } 198 199 capacity = new_capacity; 200 } 201 202 206 public void insertCharAt(Term term, char c, int column, int a) { 207 int new_length = length + 1; 208 209 if (column >= length) { 210 new_length = column+1; 211 ensureCapacity(term, new_length); 212 for (int fx = length; fx < column; fx++) 214 buf[fx] = ' '; 215 } else { 216 ensureCapacity(term, new_length); 217 System.arraycopy(buf, column, buf, column + 1, length - column); 218 if (haveAttributes(a)) 219 System.arraycopy(attr, column, attr, column + 1, length - column); 220 } 221 222 term.checkForMultiCell(c); 223 224 buf[column] = c; 225 if (haveAttributes(a)) 226 attr[column] = a; 227 228 length = new_length; 229 } 230 231 235 public void setCharAt(Term term, char c, int column, int a) { 236 if (column >= length) { 237 ensureCapacity(term, column+1); 238 for (int fx = length; fx < column; fx++) 240 buf[fx] = ' '; 241 length = column+1; 242 } 243 term.checkForMultiCell(c); 244 buf[column] = c; 245 if (haveAttributes(a)) { 246 attr[column] = a; 247 } 248 } 249 250 public void deleteCharAt(int column) { 251 if (column < 0 || column >= length) 252 return; 253 System.arraycopy(buf, column+1, buf, column, length-column-1); 254 buf[length-1] = 0; 255 if (attr != null) { 256 System.arraycopy(attr, column+1, attr, column, length-column-1); 257 attr[length-1] = 0; 258 } 259 length--; 261 } 262 263 public void clearToEndFrom(Term term, int col) { 264 ensureCapacity(term, col+1); 265 266 for (int cx = col; cx < length; cx++) 268 buf[cx] = ' '; 269 if (attr != null) { 270 for (int cx = col; cx < length; cx++) 271 attr[cx] = ' '; 272 } 273 length = col; 274 } 275 276 277 278 282 public String text(int bcol, int ecol) { 283 287 288 289 String newline = ""; 291 300 if (length == 0) 301 return "\n"; 302 303 if (ecol >= length) { 304 ecol = length-1; 306 newline = "\n"; 308 if (bcol >= length) 309 bcol = length-1; 310 } 311 return new String (buf, bcol, ecol-bcol+1) + newline; 312 } 313 314 public void setCharacterAttribute(int bcol, int ecol, 315 int value, boolean on) { 316 if (!haveAttributes(value)) 319 return; 320 321 if (on) { 322 for (int c = bcol; c <= ecol; c++) 323 attr[c] = Attr.setAttribute(attr[c], value); 324 } else { 325 for (int c = bcol; c <= ecol; c++) 326 attr[c] = Attr.unsetAttribute(attr[c], value); 327 } 328 } 329 } 330 | Popular Tags |