1 22 23 27 28 package org.netbeans.lib.terminalemulator; 29 30 import java.util.Vector ; 31 32 41 42 43 class Buffer { 44 45 49 static class OurVector extends Vector { 50 public void removeRange(int fromIndex, int toIndex) { 51 super.removeRange(fromIndex, toIndex); 52 } 53 } 54 55 private OurVector lines = new OurVector(); 57 public int nlines; 60 private int visible_cols; private int extra_cols; 64 public int visibleCols() { 65 return visible_cols; 66 } 67 68 public int totalCols() { 69 return visible_cols + extra_cols; 70 } 71 72 public Buffer(int visible_cols) { 73 this.visible_cols = visible_cols; 74 } 75 76 public void setVisibleCols(int visible_cols) { 77 int delta = visible_cols - this.visible_cols; 78 this.visible_cols = visible_cols; 79 extra_cols -= delta; 80 if (extra_cols < 0) 81 extra_cols = 0; 82 } 83 84 88 public void noteColumn(int col) { 89 int new_extra = col - visible_cols; 90 if (new_extra > extra_cols) { 91 extra_cols = new_extra; 92 } 94 } 95 96 107 108 Line lineAt(int brow) { 109 try { 110 return (Line) lines.elementAt(brow); 111 } catch(ArrayIndexOutOfBoundsException x) { 112 117 121 return null; 122 } 123 } 124 125 Line bottom() { 126 return lineAt(nlines); 127 } 128 129 public Line appendLine() { 130 Line l = new Line(); 132 lines.add(l); 133 nlines++; 134 return l; 135 } 136 137 public Line addLineAt(int row) { 138 Line l = new Line(); 140 lines.add(row, l); 141 nlines++; 142 return l; 143 } 144 145 149 public int removeLinesAt(int row, int n) { 150 int nchars = 0; 152 for (int r = row; r < row+n; r++) 153 nchars += lineAt(r).length() + 1; 154 155 lines.removeRange(row, row+n); 156 nlines -= n; 157 158 return nchars; 159 } 160 161 public void removeLineAt(int row) { 162 lines.remove(row); 164 nlines--; 165 } 166 167 public Line moveLineFromTo(int from, int to) { 168 Line l = (Line) lines.remove(from); 170 lines.add(to, l); 171 return l; 172 } 173 174 183 void visitLines(BCoord begin, BCoord end, boolean newlines, 184 LineVisitor visitor) { 185 186 190 Line l; 191 if (begin.row == end.row) { 192 l = lineAt(begin.row); 194 visitor.visit(l, begin.row, begin.col, end.col); 195 196 } else { 197 boolean cont = false; 198 199 l = lineAt(begin.row); 201 if (newlines && !l.isWrapped()) 202 cont = visitor.visit(l, begin.row, begin.col, totalCols()); 203 else 204 cont = visitor.visit(l, begin.row, begin.col, l.length()-1); 205 if (!cont) 206 return; 207 208 for (int r = begin.row+1; r < end.row; r++) { 209 l = lineAt(r); 210 if (newlines && !l.isWrapped()) 211 cont = visitor.visit(l, r, 0, totalCols()); 212 else 213 cont = visitor.visit(l, r, 0, l.length()-1); 214 if (!cont) 215 return; 216 } 217 218 l = lineAt(end.row); 219 cont = visitor.visit(l, end.row, 0, end.col); 220 if (!cont) 221 return; 222 } 223 } 224 225 230 void reverseVisitLines(BCoord begin, BCoord end, boolean newlines, 231 LineVisitor visitor) { 232 233 235 Line l; 236 if (begin.row == end.row) { 237 l = lineAt(begin.row); 239 visitor.visit(l, begin.row, begin.col, end.col); 240 241 } else { 242 boolean cont = false; 243 244 l = lineAt(end.row); 246 cont = visitor.visit(l, end.row, 0, end.col); 247 if (!cont) 248 return; 249 250 for (int r = end.row-1; r > begin.row; r--) { 251 l = lineAt(r); 252 if (newlines && !l.isWrapped()) 253 cont = visitor.visit(l, r, 0, totalCols()); 254 else 255 cont = visitor.visit(l, r, 0, l.length()-1); 256 if (!cont) 257 return; 258 } 259 260 l = lineAt(begin.row); 261 if (newlines && !l.isWrapped()) 262 cont = visitor.visit(l, begin.row, begin.col, totalCols()); 263 else 264 cont = visitor.visit(l, begin.row, begin.col, l.length()-1); 265 if (!cont) 266 return; 267 } 268 } 269 270 public BExtent find_word(WordDelineator word_delineator, BCoord coord) { 271 274 275 Line l = lineAt(coord.row); 276 277 if (coord.col >= l.length()) 278 return new BExtent(coord, coord); 279 280 int lx = word_delineator.findLeft(l.stringBuffer(), coord.col); 281 int rx = word_delineator.findRight(l.stringBuffer(), coord.col); 282 283 return new BExtent(new BCoord(coord.row, lx), 284 new BCoord(coord.row, rx)); 285 } 286 287 294 295 public BCoord backup(BCoord c) { 296 if (c.col > 0) 297 return new BCoord(c.row, c.col-1); 299 for (int prevrow = c.row-1; prevrow >= 0; prevrow--) { 303 Line l = lineAt(prevrow); 304 if (l.length() != 0) 305 return new BCoord(prevrow, l.length()-1); 306 } 307 308 return null; 310 } 311 312 319 public BCoord advance(BCoord c) { 320 int row = c.row; 321 int col = c.col; 322 323 col++; 324 Line l = lineAt(row); 325 if (col < l.length()) 326 return new BCoord(row, col); 327 328 while (++row < nlines) { 332 l = lineAt(row); 333 if (l.length() != 0) 334 return new BCoord(row, 0); 335 } 336 return null; 337 } 338 339 342 public void printStats() { 343 int nchars = 0; 344 int ncapacity = 0; 345 for (int lx = 0; lx < nlines; lx++) { 346 Line l = lineAt(lx); 347 ncapacity += l.capacity(); 348 nchars += l.length(); 349 } 350 System.out.println( 351 " nlines " + nlines + " nchars " + nchars + " ncapacity " + ncapacity ); } 355 } 356 | Popular Tags |