1 19 package org.openide.text; 20 21 import java.awt.Color ; 22 import java.awt.Component ; 23 24 import java.beans.*; 25 26 import javax.swing.JEditorPane ; 27 import javax.swing.JToolBar ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.text.*; 30 31 32 36 public final class NbDocument extends Object { 37 41 public static final Object GUARDED = new AttributeSet.CharacterAttribute() { 42 }; 43 44 46 private static final SimpleAttributeSet ATTR_ADD = new SimpleAttributeSet(); 47 48 50 private static final SimpleAttributeSet ATTR_REMOVE = new SimpleAttributeSet(); 51 52 static { 53 ATTR_ADD.addAttribute(GUARDED, Boolean.TRUE); 54 ATTR_REMOVE.addAttribute(GUARDED, Boolean.FALSE); 55 } 56 57 60 public static final String BREAKPOINT_STYLE_NAME = "NbBreakpointStyle"; 62 65 public static final String ERROR_STYLE_NAME = "NbErrorStyle"; 67 70 public static final String CURRENT_STYLE_NAME = "NbCurrentStyle"; 72 75 public static final String NORMAL_STYLE_NAME = "NbNormalStyle"; 77 78 @Deprecated 79 public static final Colors COLORS = new Colors(); 80 81 private NbDocument() { 82 } 83 84 93 public static Element findLineRootElement(StyledDocument doc) { 94 checkDocParameter(doc); 95 96 Element e = doc.getParagraphElement(0).getParentElement(); 97 98 if (e == null) { 99 e = doc.getDefaultRootElement(); 101 } 102 103 return e; 104 } 105 106 113 public static int findLineNumber(StyledDocument doc, int offset) { 114 118 return new DocumentRenderer(DocumentRenderer.FIND_LINE_NUMBER, doc, offset).renderToInt(); 119 } 120 121 128 public static int findLineColumn(StyledDocument doc, int offset) { 129 134 return new DocumentRenderer(DocumentRenderer.FIND_LINE_COLUMN, doc, offset).renderToInt(); 135 } 136 137 146 public static int findLineOffset(StyledDocument doc, int lineNumber) { 147 158 return new DocumentRenderer(DocumentRenderer.FIND_LINE_OFFSET, doc, lineNumber).renderToInt(); 159 } 160 161 177 public static Position createPosition(Document doc, int offset, Position.Bias bias) 178 throws BadLocationException { 179 checkDocParameter(doc); 180 181 if (doc instanceof PositionBiasable) { 182 return ((PositionBiasable) doc).createPosition(offset, bias); 183 } else { 184 if (bias == Position.Bias.Forward) { 185 return doc.createPosition(offset); 187 } else { 188 return BackwardPosition.create(doc, offset); 190 } 191 } 192 } 193 194 201 public static void markGuarded(StyledDocument doc, int offset, int len) { 202 checkDocParameter(doc); 203 doc.setCharacterAttributes(offset, len, ATTR_ADD, false); 204 } 205 206 213 public static void unmarkGuarded(StyledDocument doc, int offset, int len) { 214 checkDocParameter(doc); 215 doc.setCharacterAttributes(offset, len, ATTR_REMOVE, false); 216 } 217 218 225 public static void insertGuarded(StyledDocument doc, int offset, String txt) 226 throws BadLocationException { 227 checkDocParameter(doc); 228 doc.insertString(offset, txt, ATTR_ADD); 229 } 230 231 242 @Deprecated 243 public static void markBreakpoint(StyledDocument doc, int offset) { 244 checkDocParameter(doc); 245 246 Style bp = doc.getStyle(BREAKPOINT_STYLE_NAME); 247 248 if (bp == null) { 249 bp = doc.addStyle(BREAKPOINT_STYLE_NAME, null); 251 252 if (bp == null) { 253 return; 254 } 255 256 bp.addAttribute(StyleConstants.ColorConstants.Background, Color.red); 257 bp.addAttribute(StyleConstants.ColorConstants.Foreground, Color.white); 258 } 259 260 doc.setLogicalStyle(offset, bp); 261 } 262 263 274 @Deprecated 275 public static void markError(StyledDocument doc, int offset) { 276 checkDocParameter(doc); 277 278 Style bp = doc.getStyle(ERROR_STYLE_NAME); 279 280 if (bp == null) { 281 bp = doc.addStyle(ERROR_STYLE_NAME, null); 283 284 if (bp == null) { 285 return; 286 } 287 288 bp.addAttribute(StyleConstants.ColorConstants.Background, Color.green); 289 bp.addAttribute(StyleConstants.ColorConstants.Foreground, Color.black); 290 } 291 292 doc.setLogicalStyle(offset, bp); 293 } 294 295 306 @Deprecated 307 public static void markCurrent(StyledDocument doc, int offset) { 308 checkDocParameter(doc); 309 310 Style bp = doc.getStyle(CURRENT_STYLE_NAME); 311 312 if (bp == null) { 313 bp = doc.addStyle(CURRENT_STYLE_NAME, null); 315 316 if (bp == null) { 317 return; 318 } 319 320 bp.addAttribute(StyleConstants.ColorConstants.Background, Color.blue); 321 bp.addAttribute(StyleConstants.ColorConstants.Foreground, Color.white); 322 } 323 324 doc.setLogicalStyle(offset, bp); 325 } 326 327 338 @Deprecated 339 public static void markNormal(StyledDocument doc, int offset) { 340 checkDocParameter(doc); 341 342 Style st = doc.getStyle(NORMAL_STYLE_NAME); 343 344 if (st == null) { 345 st = doc.addStyle(NORMAL_STYLE_NAME, null); 346 } 347 348 if (st != null) { 349 doc.setLogicalStyle(offset, st); 350 } 351 } 352 353 361 public static void runAtomic(StyledDocument doc, Runnable run) { 362 checkDocParameter(doc); 363 364 if (doc instanceof WriteLockable) { 365 ((WriteLockable) doc).runAtomic(run); 367 } else { 368 synchronized (doc) { 370 run.run(); 371 } 372 } 373 } 374 375 386 public static void runAtomicAsUser(StyledDocument doc, Runnable run) 387 throws BadLocationException { 388 checkDocParameter(doc); 389 390 if (doc instanceof WriteLockable) { 391 ((WriteLockable) doc).runAtomicAsUser(run); 393 } else { 394 synchronized (doc) { 396 run.run(); 397 } 398 } 399 } 400 401 404 private static void checkDocParameter(Document doc) { 405 if (doc == null) { 406 throw new NullPointerException ("Invalid doc parameter. Document may not be null!"); } 408 } 409 410 419 public static Object findPageable(StyledDocument doc) { 420 if (doc instanceof java.awt.print.Pageable ) { 421 return doc; 422 } else if (doc instanceof java.awt.print.Printable ) { 423 return doc; 424 } else { 425 return new DefaultPrintable(doc); 426 } 427 } 428 429 439 public static void addAnnotation( 440 final StyledDocument doc, final Position startPos, final int length, final Annotation annotation 441 ) { 442 if (!(doc instanceof Annotatable)) { 443 return; 444 } 445 446 if (SwingUtilities.isEventDispatchThread()) { 447 ((Annotatable) doc).addAnnotation(startPos, length, annotation); 448 } else { 449 SwingUtilities.invokeLater( 450 new Runnable () { 451 public void run() { 452 ((Annotatable) doc).addAnnotation(startPos, length, annotation); 453 } 454 } 455 ); 456 } 457 } 458 459 464 public static void removeAnnotation(final StyledDocument doc, final Annotation annotation) { 465 if (!(doc instanceof Annotatable)) { 466 return; 467 } 468 469 if (SwingUtilities.isEventDispatchThread()) { 470 ((Annotatable) doc).removeAnnotation(annotation); 471 } else { 472 SwingUtilities.invokeLater( 473 new Runnable () { 474 public void run() { 475 ((Annotatable) doc).removeAnnotation(annotation); 476 } 477 } 478 ); 479 } 480 } 481 482 485 public interface WriteLockable extends Document { 486 505 public void runAtomic(Runnable r); 506 507 515 public void runAtomicAsUser(Runnable r) throws BadLocationException; 516 } 517 518 523 public interface Printable extends Document { 524 531 public java.text.AttributedCharacterIterator [] createPrintIterators(); 532 } 533 534 544 public interface PositionBiasable extends Document { 545 556 public Position createPosition(int offset, Position.Bias bias) 557 throws BadLocationException; 558 } 559 560 564 public interface CustomEditor extends Document { 565 576 public Component createEditor(JEditorPane j); 577 } 578 579 582 public interface CustomToolbar extends Document { 583 586 public JToolBar createToolbar(JEditorPane j); 587 } 588 589 594 public interface Annotatable extends Document { 595 602 public void addAnnotation(Position startPos, int length, Annotation annotation); 603 604 606 public void removeAnnotation(Annotation annotation); 607 } 608 609 610 @Deprecated 611 public static final class Colors extends org.openide.options.SystemOption { 612 public static final String PROP_BREAKPOINT = BREAKPOINT_STYLE_NAME; 613 public static final String PROP_ERROR = ERROR_STYLE_NAME; 614 public static final String PROP_CURRENT = CURRENT_STYLE_NAME; 615 static final long serialVersionUID = -9152250591365746193L; 616 617 public void setBreakpoint(Color c) { 618 } 619 620 public Color getBreakpoint() { 621 return new Color (127, 127, 255); 622 } 623 624 public void setError(Color c) { 625 } 626 627 public Color getError() { 628 return Color.red; 629 } 630 631 public void setCurrent(Color c) { 632 } 633 634 public Color getCurrent() { 635 return Color.magenta; 636 } 637 638 public String displayName() { 639 return "COLORS"; } 641 } 642 643 private static final class DocumentRenderer implements Runnable { 644 private static final int FIND_LINE_NUMBER = 0; 645 private static final int FIND_LINE_COLUMN = 1; 646 private static final int FIND_LINE_OFFSET = 2; 647 private StyledDocument doc; 648 private int opCode; 649 private int argInt; 650 private int retInt; 651 652 DocumentRenderer(int opCode, StyledDocument doc, int argInt) { 653 this.opCode = opCode; 654 this.doc = doc; 655 this.argInt = argInt; 656 } 657 658 int renderToInt() { 659 doc.render(this); 660 661 return retInt; 662 } 663 664 public void run() { 665 switch (opCode) { 666 case FIND_LINE_NUMBER: { 667 Element paragraphsParent = findLineRootElement(doc); 668 retInt = paragraphsParent.getElementIndex(argInt); 670 break; 671 } 672 673 case FIND_LINE_COLUMN: { 674 Element paragraphsParent = findLineRootElement(doc); 675 int indx = paragraphsParent.getElementIndex(argInt); retInt = argInt - paragraphsParent.getElement(indx).getStartOffset(); 677 678 break; 679 } 680 681 case FIND_LINE_OFFSET: { 682 Element paragraphsParent = findLineRootElement(doc); 683 Element line = paragraphsParent.getElement(argInt); 685 if (line == null) { 686 throw new IndexOutOfBoundsException ("Index=" + argInt + " is out of bounds."); } 688 689 retInt = line.getStartOffset(); 690 691 break; 692 } 693 694 default: 695 throw new IllegalStateException (); 696 } 697 } 698 } 699 } 700 | Popular Tags |