1 19 20 package org.netbeans.editor; 21 22 import java.awt.Color ; 23 import java.util.List ; 24 import javax.swing.text.Style ; 25 import javax.swing.text.StyleConstants ; 26 import javax.swing.text.BadLocationException ; 27 import javax.swing.text.JTextComponent ; 28 import javax.swing.text.View ; 29 30 39 public class DrawLayerFactory { 40 41 42 public static final String SYNTAX_LAYER_NAME = "syntax-layer"; 44 45 public static final int SYNTAX_LAYER_VISIBILITY = 1000; 46 47 48 public static final String ANNOTATION_LAYER_NAME = "annotation-layer"; 50 51 public static final int ANNOTATION_LAYER_VISIBILITY = 2100; 52 53 54 public static final String HIGHLIGHT_SEARCH_LAYER_NAME = "highlight-search-layer"; 56 57 public static final int HIGHLIGHT_SEARCH_LAYER_VISIBILITY = 9000; 58 59 60 public static final String INC_SEARCH_LAYER_NAME = "inc-search-layer"; 62 63 public static final int INC_SEARCH_LAYER_VISIBILITY = 9500; 64 65 66 public static final String BLOCK_SEARCH_LAYER_NAME = "block-search-layer"; 68 69 public static final int BLOCK_SEARCH_LAYER_VISIBILITY = 8500; 70 71 72 public static final String CARET_LAYER_NAME = "caret-layer"; 74 75 public static final int CARET_LAYER_VISIBILITY = 10000; 76 77 78 79 public static final String GUARDED_LAYER_NAME = "guarded-layer"; 81 82 public static final int GUARDED_LAYER_VISIBILITY = 1400; 83 84 85 88 public static class SyntaxLayer extends DrawLayer.AbstractLayer { 89 90 public SyntaxLayer() { 91 super(SYNTAX_LAYER_NAME); 92 } 93 94 public void init(DrawContext ctx) { 95 } 96 97 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 98 return true; 99 } 100 101 public void updateContext(DrawContext ctx) { 102 TokenID tokenID = ctx.getTokenID(); 104 TokenContextPath tcp = ctx.getTokenContextPath(); 105 if (tokenID != null && tcp != null) { 106 String fullName = tcp.getFullTokenName(tokenID); 108 Coloring c = ctx.getEditorUI().getColoring(fullName); 109 if (c != null) { 110 c.apply(ctx); 111 112 } else { TokenCategory cat = tokenID.getCategory(); 114 if (cat != null) { 115 fullName = tcp.getFullTokenName(cat); 116 c = ctx.getEditorUI().getColoring(fullName); 117 if (c != null) { 118 c.apply(ctx); 119 } 120 } 121 } 122 } 123 } 124 125 } 126 127 128 132 public static abstract class ColorLineLayer extends DrawLayer.AbstractLayer { 133 134 135 Coloring coloring; 136 137 public ColorLineLayer(String name) { 138 super(name); 139 } 140 141 public boolean extendsEOL() { 142 return true; 143 } 144 145 public void init(DrawContext ctx) { 146 coloring = null; 147 } 148 149 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 150 boolean active; 151 if (mark == null) { 152 View view = ctx instanceof DrawEngine.DrawInfo ? ((DrawEngine.DrawInfo)ctx).view.getParent() : null; 153 if (view instanceof FoldMultiLineView) { 154 FoldMultiLineView fmlView = (FoldMultiLineView)view; 155 mark = getFoldedMark(fmlView, getName()); 156 if (mark == null) 157 mark = getFoldedMark(fmlView.getView(fmlView.getViewCount() -1), getName()); 158 } 159 } 160 if (mark != null) { 161 active = (ctx.getEditorUI().getComponent() != null) 162 && mark.activateLayer; 163 if (active) { 164 try { 165 BaseDocument doc = ctx.getEditorUI().getDocument(); 166 int nextRowStartPos = Utilities.getRowStart( 167 doc, ctx.getFragmentOffset(), 1); 168 if (nextRowStartPos < 0) { nextRowStartPos = Integer.MAX_VALUE; 170 } 171 setNextActivityChangeOffset(nextRowStartPos); 172 173 } catch (BadLocationException e) { 174 active = false; 175 } 176 } 177 } else { 178 active = false; 179 } 180 return active; 181 } 182 183 public void updateContext(DrawContext ctx) { 184 if (coloring == null) { 185 coloring = getColoring(ctx); 186 } 187 if (coloring != null) { 188 coloring.apply(ctx); 189 } 190 } 191 192 protected abstract Coloring getColoring(DrawContext ctx); 193 194 } 195 196 197 205 public static class CaretLayer extends DrawLayer.AbstractLayer { 206 207 Coloring coloring; 208 209 public CaretLayer() { 210 super(CARET_LAYER_NAME); 211 } 212 213 public boolean extendsEmptyLine() { 214 return true; 215 } 216 217 public void init(DrawContext ctx) { 218 coloring = null; 219 } 220 221 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 222 boolean active; 223 if (mark != null) { 224 active = mark.activateLayer; 225 } else { 226 JTextComponent c = ctx.getEditorUI().getComponent(); 227 active = (c != null) && c.getCaret().isSelectionVisible() 228 && ctx.getFragmentOffset() >= c.getSelectionStart() 229 && ctx.getFragmentOffset() < c.getSelectionEnd(); 230 } 231 232 return active; 233 } 234 235 public void updateContext(DrawContext ctx) { 236 if (coloring == null) { 237 coloring = ctx.getEditorUI().getColoring(SettingsNames.SELECTION_COLORING); 238 } 239 if (coloring != null) { 240 coloring.apply(ctx); 241 } 242 } 243 244 } 245 246 247 253 public static class HighlightSearchLayer extends DrawLayer.AbstractLayer { 254 255 256 int blocks[] = new int[] { -1, -1 }; 257 258 259 Coloring coloring; 260 261 262 int curInd; 263 264 265 boolean enabled; 266 267 public HighlightSearchLayer() { 268 super(HIGHLIGHT_SEARCH_LAYER_NAME); 269 } 270 271 public boolean isEnabled() { 272 return enabled; 273 } 274 275 public void setEnabled(boolean enabled) { 276 this.enabled = enabled; 277 } 278 279 public void init(DrawContext ctx) { 280 if (enabled) { 281 try { 282 BaseDocument doc = ctx.getEditorUI().getDocument(); 283 blocks = FindSupport.getFindSupport().getBlocks(blocks, 284 doc, ctx.getStartOffset(), ctx.getEndOffset()); 285 } catch (BadLocationException e) { 286 blocks = new int[] { -1, -1 }; 287 } 288 coloring = null; curInd = 0; 290 } 291 } 292 293 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 294 boolean active; 295 if (enabled) { 296 int pos = ctx.getFragmentOffset(); 297 if (pos == blocks[curInd]) { 298 active = true; 299 setNextActivityChangeOffset(blocks[curInd + 1]); 300 301 } else if (pos == blocks[curInd + 1]) { 302 active = false; 303 curInd += 2; 304 setNextActivityChangeOffset(blocks[curInd]); 305 if (pos == blocks[curInd]) { setNextActivityChangeOffset(blocks[curInd + 1]); 307 active = true; 308 } 309 310 } else { 311 setNextActivityChangeOffset(blocks[curInd]); 312 active = false; 313 } 314 } else { 315 active = false; 316 } 317 318 return active; 319 } 320 321 public void updateContext(DrawContext ctx) { 322 int pos = ctx.getFragmentOffset(); 323 if (pos >= blocks[curInd] && pos < blocks[curInd + 1]) { 324 if (coloring == null) { 325 coloring = ctx.getEditorUI().getColoring(SettingsNames.HIGHLIGHT_SEARCH_COLORING); 326 } 327 if (coloring != null) { 328 coloring.apply(ctx); 329 } 330 } 331 } 332 333 } 334 335 341 public static class IncSearchLayer extends DrawLayer.AbstractLayer implements SettingsChangeListener{ 342 343 344 Coloring coloring; 345 346 Coloring invertedColoring; 347 348 349 int pos; 350 351 352 int len; 353 354 355 boolean enabled; 356 357 boolean invert; 358 359 public IncSearchLayer() { 360 super(INC_SEARCH_LAYER_NAME); 361 Settings.addSettingsChangeListener(this); 362 } 363 364 public boolean isEnabled() { 365 return enabled; 366 } 367 368 public void setEnabled(boolean enabled) { 369 this.enabled = enabled; 370 } 371 372 void setArea(int pos, int len) { 373 this.pos = pos; 374 this.len = len; 375 } 376 377 public int getOffset() { 378 return pos; 379 } 380 381 public int getLength() { 382 return len; 383 } 384 385 public void init(DrawContext ctx) { 386 setNextActivityChangeOffset(enabled ? pos : Integer.MAX_VALUE); 387 } 388 389 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 390 boolean active = false; 391 if (enabled) { 392 if (ctx.getFragmentOffset() == pos) { 393 active = true; 394 setNextActivityChangeOffset(pos + len); 395 } 396 } 397 398 return active; 399 } 400 401 405 void setInversion(boolean invert){ 406 this.invert = invert; 407 } 408 409 public void updateContext(DrawContext ctx) { 410 if (!invert){ 411 if (coloring == null) { 412 coloring = ctx.getEditorUI().getColoring(SettingsNames.INC_SEARCH_COLORING); 413 } 414 if (coloring != null) { 415 coloring.apply(ctx); 416 } 417 }else{ 418 if (invertedColoring == null) { 419 invertedColoring = ctx.getEditorUI().getColoring(SettingsNames.SELECTION_COLORING); 420 } 421 if (invertedColoring != null) { 422 invertedColoring.apply(ctx); 423 } 424 } 425 } 426 427 public void settingsChange(org.netbeans.editor.SettingsChangeEvent evt) 428 { 429 if (evt != null){ 430 String incSearchColoring = SettingsNames.INC_SEARCH_COLORING+SettingsNames.COLORING_NAME_SUFFIX; 431 if (incSearchColoring.equals(evt.getSettingName())){ 432 coloring = null; 433 } 434 } 435 } 436 437 } 438 439 443 public static class BlockSearchLayer extends DrawLayer.AbstractLayer implements SettingsChangeListener{ 444 445 446 Coloring coloring; 447 448 449 int pos; 450 451 452 int len; 453 454 455 boolean enabled; 456 457 public BlockSearchLayer() { 458 super(BLOCK_SEARCH_LAYER_NAME); 459 Settings.addSettingsChangeListener(this); 460 } 461 462 public boolean extendsEmptyLine(){ 463 return true; 464 } 465 466 public boolean isEnabled() { 467 return enabled; 468 } 469 470 public void setEnabled(boolean enabled) { 471 this.enabled = enabled; 472 } 473 474 void setArea(int pos, int len) { 475 this.pos = pos; 476 this.len = len; 477 } 478 479 int getOffset() { 480 return pos; 481 } 482 483 int getLength() { 484 return len; 485 } 486 487 public void init(DrawContext ctx) { 488 setNextActivityChangeOffset(enabled ? pos : Integer.MAX_VALUE); 489 } 490 491 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 492 boolean active = false; 493 if (enabled) { 494 int fragOffset = ctx.getFragmentOffset(); 495 if (fragOffset >= pos && fragOffset<(pos+len)) { 496 active = true; 497 try { 498 BaseDocument doc = ctx.getEditorUI().getDocument(); 499 int nextRowStartPos = Utilities.getRowStart( 500 doc, fragOffset, 1); 501 if (nextRowStartPos < 0) { nextRowStartPos = Integer.MAX_VALUE; 503 } 504 setNextActivityChangeOffset(Math.min(nextRowStartPos,(pos+len))); 505 506 } catch (BadLocationException e) { 507 active = false; 508 } 509 510 } 511 } 512 513 return active; 514 } 515 516 public void updateContext(DrawContext ctx) { 517 if (coloring == null) { 518 coloring = ctx.getEditorUI().getColoring(SettingsNames.BLOCK_SEARCH_COLORING); 519 } 520 if (coloring != null) { 521 coloring.apply(ctx); 522 } 523 } 524 525 public void settingsChange(org.netbeans.editor.SettingsChangeEvent evt) 526 { 527 if (evt != null){ 528 String blockSearchColoring = SettingsNames.BLOCK_SEARCH_COLORING+SettingsNames.COLORING_NAME_SUFFIX; 529 if (blockSearchColoring.equals(evt.getSettingName())){ 530 coloring = null; 531 } 532 } 533 } 534 535 } 536 537 575 576 577 583 public static class StyleLayer extends DrawLayer.AbstractLayer { 584 585 protected Style style; 586 587 protected MarkChain markChain; 588 589 protected Color backColor; 590 591 protected Color foreColor; 592 593 public StyleLayer(String layerName, BaseDocument doc, Style style) { 594 super(layerName); 595 this.style = style; 596 markChain = new MarkChain(doc, layerName); 597 } 598 599 public boolean extendsEOL() { 600 return true; 601 } 602 603 public final MarkChain getMarkChain() { 604 return markChain; 605 } 606 607 public void init(DrawContext ctx) { 608 foreColor = StyleConstants.getForeground(style); 609 backColor = StyleConstants.getBackground(style); 610 } 611 612 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 613 boolean active = false; 614 if (mark != null) { 615 active = (ctx.getEditorUI().getComponent() != null) 616 && mark.activateLayer; 617 if (active) { 618 try { 619 BaseDocument doc = ctx.getEditorUI().getDocument(); 620 int nextRowStartPos = Utilities.getRowStart( 621 doc, ctx.getFragmentOffset(), 1); 622 if (nextRowStartPos < 0) { nextRowStartPos = Integer.MAX_VALUE; 624 } 625 626 setNextActivityChangeOffset(nextRowStartPos); 627 628 } catch (BadLocationException e) { 629 active = false; 630 } 631 } 632 633 } 634 635 return active; 636 } 637 638 public void updateContext(DrawContext ctx) { 639 if (foreColor != null) { 640 ctx.setForeColor(foreColor); 641 } 642 if (backColor != null) { 643 ctx.setBackColor(backColor); 644 } 645 } 646 647 public String toString() { 648 return super.toString() + ((markChain != null) ? (", " + markChain) : ""); } 650 651 } 652 653 659 public static class WordColoringLayer extends DrawLayer.AbstractLayer { 660 661 protected StringMap stringMap = new StringMap(); 662 663 public WordColoringLayer(String name) { 664 super(name); 665 } 666 667 public void put(String s, Coloring c) { 668 stringMap.put(s, c); 669 } 670 671 public void put(String [] strings, Coloring c) { 672 for (int i = 0; i < strings.length; i++) { 673 put(strings[i], c); 674 } 675 } 676 677 public void put(List stringList, Coloring c) { 678 String strings[] = new String [stringList.size()]; 679 stringList.toArray(strings); 680 put(strings, c); 681 } 682 683 public void init(DrawContext ctx) { 684 } 685 686 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 687 return true; 688 } 689 690 public void updateContext(DrawContext ctx) { 691 Coloring c = (Coloring)stringMap.get(ctx.getBuffer(), 692 ctx.getTokenOffset(), ctx.getTokenLength()); 693 if (c != null) { 694 c.apply(ctx); 695 } 696 } 697 698 } 699 700 709 public static class AnnotationLayer extends DrawLayer.AbstractLayer { 710 711 712 private Coloring coloring; 713 714 715 private MarkChain markChain; 716 717 public AnnotationLayer(BaseDocument doc) { 718 super(ANNOTATION_LAYER_NAME); 719 coloring = null; 720 markChain = new MarkChain(doc, ANNOTATION_LAYER_NAME); 721 } 722 723 725 public final MarkChain getMarkChain() { 726 return markChain; 727 } 728 729 public boolean extendsEOL() { 730 return true; 731 } 732 733 public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { 734 int nextActivityOffset; 735 coloring = null; 736 737 if (mark == null) { 738 View view = ctx instanceof DrawEngine.DrawInfo ? ((DrawEngine.DrawInfo)ctx).view : null; 739 if (view != null && view.getParent() instanceof FoldMultiLineView) 740 mark = getFoldedMark(view, getName()); 741 } 742 743 if (mark == null) 744 return false; 745 746 if (ctx.getEditorUI().getComponent() == null || !mark.activateLayer) 747 return false; 748 749 BaseDocument doc = ctx.getEditorUI().getDocument(); 750 751 AnnotationDesc anno = doc.getAnnotations().getActiveAnnotation(mark); 756 if (anno == null) { 757 AnnotationDesc activeAnno = doc.getAnnotations().getLineActiveAnnotation(mark); 761 if (activeAnno == null) 762 return false; 763 if (ctx.getFragmentOffset() >= activeAnno.getOffset()) 764 if (ctx.getFragmentOffset() < activeAnno.getOffset()+activeAnno.getLength() || activeAnno.isWholeLine()) { 765 coloring = activeAnno.getColoring(); 766 return true; 767 } 768 return false; 769 } 770 771 int nextLineStart = -1; 772 try { 773 nextLineStart = Utilities.getRowStart(doc, ctx.getFragmentOffset(), 1); 774 } catch (BadLocationException e) { 775 return false; 776 } 777 if (nextLineStart < 0) { nextLineStart = Integer.MAX_VALUE; 779 } 780 if (anno.isWholeLine()) { 781 nextActivityOffset = nextLineStart; 782 } 783 else { 784 nextActivityOffset = ctx.getFragmentOffset() + anno.getLength(); 785 if (nextActivityOffset > nextLineStart) 786 nextActivityOffset = nextLineStart; 787 } 788 789 setNextActivityChangeOffset(nextActivityOffset); 790 coloring = anno.getColoring(); 791 792 808 return true; 809 } 810 811 public void updateContext(DrawContext ctx) { 812 if (coloring != null) { 813 coloring.apply(ctx); 814 } 815 } 816 817 } 818 819 private static MarkFactory.DrawMark getFoldedMark(View view, String layerName) { 820 try { 821 BaseDocument doc = (BaseDocument)view.getDocument(); 822 int startPos = Utilities.getRowStart(doc, view.getStartOffset()); 823 int endPos = view.getStartOffset(); 824 synchronized (doc.marks) { 825 MarkVector docMarksStorage = doc.marksStorage; 826 int low = 0; 827 int high = docMarksStorage.getMarkCount() - 1; 828 while (low < high) { 829 int mid = (low + high) >> 1; 830 int cmp = docMarksStorage.getMarkOffsetInternal(mid) - endPos; 831 if (cmp < 0) 832 low = mid + 1; 833 else if (cmp > 0) 834 high = mid - 1; 835 else { while (++mid <= high && docMarksStorage.getMarkOffsetInternal(mid) == endPos); 837 low = high = mid - 1; 838 } 839 } 840 while (low >= 0) { 841 MultiMark mm = (MultiMark)docMarksStorage.getMark(low--); 842 if (mm.isValid() && mm.getOffset() <= endPos) { 843 if (mm.getOffset() < startPos) 844 break; 845 Mark mrk = (Mark)doc.marks.get(mm); 846 if (mrk instanceof MarkFactory.DrawMark && layerName.equals(((MarkFactory.DrawMark)mrk).layerName)) { 847 return (MarkFactory.DrawMark)mrk; 848 } 849 } 850 } 851 } 852 } catch (BadLocationException e) {} 853 return null; 854 } 855 } 856 | Popular Tags |