1 19 package org.openide.text; 20 21 import org.openide.util.Lookup; 22 import org.openide.util.lookup.Lookups; 23 24 import java.io.*; 25 26 import java.lang.ref.Reference ; 27 import java.lang.ref.WeakReference ; 28 29 import java.util.Date ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.WeakHashMap ; 33 34 35 42 public abstract class Line extends Annotatable implements Serializable { 43 44 static final long serialVersionUID = 9113186289600795476L; 45 46 47 public static final String PROP_LINE_NUMBER = "lineNumber"; 49 52 public final static int SHOW_TRY_SHOW = 0; 53 54 57 public final static int SHOW_SHOW = 1; 58 59 62 public final static int SHOW_GOTO = 2; 63 64 70 public final static int SHOW_TOFRONT = 3; 71 72 79 public final static int SHOW_REUSE = 4; 80 81 86 public final static int SHOW_REUSE_NEW = 5; 87 88 89 static final private Line.Part nullPart = new Line.NullPart(); 90 91 92 private org.openide.util.Lookup dataObject; 93 94 103 public Line(Lookup context) { 104 if (context == null) { 105 throw new NullPointerException (); 106 } 107 108 dataObject = context; 109 } 110 111 116 public Line(Object source) { 117 this((source instanceof Lookup) ? (Lookup) source : Lookups.singleton(source)); 118 119 if (source == null) { 120 throw new NullPointerException (); 121 } 122 } 123 124 130 public String getDisplayName() { 131 return getClass().getName() + ":" + getLineNumber(); } 133 134 146 public final org.openide.util.Lookup getLookup() { 147 return dataObject; 148 } 149 150 161 public abstract int getLineNumber(); 162 163 167 public abstract void show(int kind, int column); 168 169 174 public void show(int kind) { 175 show(kind, 0); 176 } 177 178 182 @Deprecated 183 public abstract void setBreakpoint(boolean b); 184 185 189 @Deprecated 190 public abstract boolean isBreakpoint(); 191 192 195 @Deprecated 196 public abstract void markError(); 197 198 201 @Deprecated 202 public abstract void unmarkError(); 203 204 207 @Deprecated 208 public abstract void markCurrentLine(); 209 210 213 @Deprecated 214 public abstract void unmarkCurrentLine(); 215 216 233 @Deprecated 234 public boolean canBeMarkedCurrent(int action, Line previousLine) { 235 return true; 236 } 237 238 245 public Line.Part createPart(int column, int length) { 246 return nullPart; 247 } 248 249 public String getText() { 250 return null; 251 } 252 253 258 public static abstract class Part extends Annotatable { 259 260 public static final String PROP_LINE = "line"; 262 263 public static final String PROP_COLUMN = "column"; 265 266 public static final String PROP_LENGTH = "length"; 268 271 public abstract int getColumn(); 272 273 278 public abstract int getLength(); 279 280 283 public abstract Line getLine(); 284 } 285 286 287 static final private class NullPart extends Part { 288 NullPart() { 289 } 290 291 public int getColumn() { 292 return 0; 293 } 294 295 public int getLength() { 296 return 0; 297 } 298 299 public Line getLine() { 300 return null; 301 } 302 303 public String getText() { 304 return null; 305 } 306 } 307 308 320 public static abstract class Set extends Object { 321 322 private Date date; 323 324 331 private Map <Line,Reference <Line>> whm; 332 333 334 public Set() { 335 date = new Date (); 336 } 337 338 344 public abstract List <? extends Line> getLines(); 345 346 349 public final Date getDate() { 350 return date; 351 } 352 353 361 public abstract Line getOriginal(int line) throws IndexOutOfBoundsException ; 362 363 370 public abstract Line getCurrent(int line) throws IndexOutOfBoundsException ; 371 372 378 public int getOriginalLineNumber(Line line) { 379 return computeOriginal(this, line); 380 } 381 382 384 Map <Line,Reference <Line>> findWeakHashMap() { 385 synchronized (date) { 386 if (whm != null) { 387 return whm; 388 } 389 390 whm = new WeakHashMap <Line,Reference <Line>>(); 391 392 return whm; 393 } 394 } 395 396 401 final Line registerLine(Line line) { 402 if (line == null) { 404 throw new NullPointerException (); 405 } 406 407 Map <Line,Reference <Line>> lines = findWeakHashMap(); 408 409 synchronized (lines) { 410 Reference <Line> r = lines.get(line); 411 Line in = r != null ? r.get() : null; 412 413 if (in == null) { 414 if (line instanceof DocumentLine) { 415 ((DocumentLine) line).init(); 416 } 417 418 lines.put(line, new WeakReference <Line>(line)); 419 in = line; 420 } 421 422 return in; 423 } 424 } 425 426 430 final Line findLine(Line line) { 431 Map <Line,Reference <Line>> lines = findWeakHashMap(); 432 433 synchronized (lines) { 434 Reference <Line> r = lines.get(line); 435 Line in = r != null ? r.get() : null; 436 437 return in; 438 } 439 } 440 441 453 static int computeOriginal(Line.Set set, Line line) { 454 int n = line.getLineNumber(); 455 Line current = null; 456 457 try { 458 current = set.getOriginal(n); 459 460 if (line.equals(current)) { 461 return n; 462 } 463 } catch (IndexOutOfBoundsException ex) { 464 } 467 468 if (current == null) { 469 return binarySearch(set, n, 0, findMaxLine(set)); 470 } 471 472 if (n < current.getLineNumber()) { 473 return binarySearch(set, n, 0, current.getLineNumber()); 474 } else { 475 return binarySearch(set, n, current.getLineNumber(), findMaxLine(set)); 476 } 477 } 478 479 481 private static int binarySearch(Line.Set set, int number, int from, int to) { 482 while (from < to) { 483 int middle = (from + to) / 2; 484 485 Line l = set.getOriginal(middle); 486 int ln = l.getLineNumber(); 487 488 if (ln == number) { 489 return middle; 490 } 491 492 if (ln < number) { 493 from = middle + 1; 495 } else { 496 to = middle - 1; 498 } 499 } 500 501 return from; 502 } 503 504 private static int findMaxLine(Line.Set set) { 505 int from = 0; 506 int to = 32000; 507 508 for (;;) { 510 try { 511 set.getOriginal(to); 512 513 from = to; 516 to *= 2; 517 } catch (IndexOutOfBoundsException ex) { 518 break; 519 } 520 } 521 522 while (from < to) { 523 int middle = (from + to + 1) / 2; 524 525 try { 526 set.getOriginal(middle); 527 528 from = middle; 530 } catch (IndexOutOfBoundsException ex) { 531 to = middle - 1; 533 } 534 } 535 536 return from; 537 } 538 } 539 } 541 | Popular Tags |