KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > DrawLayerFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.awt.Color JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.swing.text.Style JavaDoc;
25 import javax.swing.text.StyleConstants JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.View JavaDoc;
29
30 /**
31  * Various draw layers are located here
32  *
33  * @author Miloslav Metelka
34  * @version 1.00
35  *
36  * @deprecated Please use Highlighting SPI instead, for details see
37  * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
38  */

39 public class DrawLayerFactory {
40
41     /** Syntax draw layer name */
42     public static final String JavaDoc SYNTAX_LAYER_NAME = "syntax-layer"; // NOI18N
43

44     /** Syntax draw layer visibility */
45     public static final int SYNTAX_LAYER_VISIBILITY = 1000;
46
47     /** Annotation draw layer name */
48     public static final String JavaDoc ANNOTATION_LAYER_NAME = "annotation-layer"; // NOI18N
49

50     /** Annotation draw layer visibility */
51     public static final int ANNOTATION_LAYER_VISIBILITY = 2100;
52     
53     /** Highlight search layer name */
54     public static final String JavaDoc HIGHLIGHT_SEARCH_LAYER_NAME = "highlight-search-layer"; // NOI18N
55

56     /** Highlight search layer visibility */
57     public static final int HIGHLIGHT_SEARCH_LAYER_VISIBILITY = 9000;
58
59     /** Incremental search layer name */
60     public static final String JavaDoc INC_SEARCH_LAYER_NAME = "inc-search-layer"; // NOI18N
61

62     /** Incremental search layer visibility */
63     public static final int INC_SEARCH_LAYER_VISIBILITY = 9500;
64     
65     /** Search block layer name */
66     public static final String JavaDoc BLOCK_SEARCH_LAYER_NAME = "block-search-layer"; // NOI18N
67

68     /** Search block layer visibility */
69     public static final int BLOCK_SEARCH_LAYER_VISIBILITY = 8500;
70
71     /** Selection draw layer name */
72     public static final String JavaDoc CARET_LAYER_NAME = "caret-layer"; // NOI18N
73

74     /** Selection draw layer visibility */
75     public static final int CARET_LAYER_VISIBILITY = 10000;
76
77
78     /** Guarded layer name */
79     public static final String JavaDoc GUARDED_LAYER_NAME = "guarded-layer"; // NOI18N
80

81     /** Guarded layer visibility */
82     public static final int GUARDED_LAYER_VISIBILITY = 1400;
83
84
85     /** Layer that colors the text according to the tokens that were parsed.
86     * It's active all the time.
87     */

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             // Get the token type and docColorings
103
TokenID tokenID = ctx.getTokenID();
104             TokenContextPath tcp = ctx.getTokenContextPath();
105             if (tokenID != null && tcp != null) {
106                 // Get the coloring according the name of the token
107
String JavaDoc fullName = tcp.getFullTokenName(tokenID);
108                 Coloring c = ctx.getEditorUI().getColoring(fullName);
109                 if (c != null) {
110                     c.apply(ctx);
111
112                 } else { // Token coloring null, try category
113
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     /** This layer colors the line by a color specified in constructor
129     * It requires only activation mark since it deactivates automatically
130     * at the end of line.
131     */

132     public static abstract class ColorLineLayer extends DrawLayer.AbstractLayer {
133
134         /** Coloring to use for highlighting */
135         Coloring coloring;
136
137         public ColorLineLayer(String JavaDoc 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 JavaDoc 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) { // end of doc
169
nextRowStartPos = Integer.MAX_VALUE;
170                         }
171                         setNextActivityChangeOffset(nextRowStartPos);
172
173                     } catch (BadLocationException JavaDoc 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     /** Layer that covers selection services provided by caret.
198      * This layer assumes that both caretMark and selectionMark in
199      * BaseCaret are properly served so that their active flags
200      * are properly set.
201      *
202      * @deprecated Please use Highlighting SPI instead, for details see
203      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
204      */

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 JavaDoc 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     /** Highlight search layer highlights all occurences
248      * of the searched string in text.
249      *
250      * @deprecated Please use Highlighting SPI instead, for details see
251      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
252      */

253     public static class HighlightSearchLayer extends DrawLayer.AbstractLayer {
254
255         /** Pairs of start and end position of the found string */
256         int blocks[] = new int[] { -1, -1 };
257
258         /** Coloring to use for highlighting */
259         Coloring coloring;
260
261         /** Current index for painting */
262         int curInd;
263
264         /** Enabled flag */
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 JavaDoc e) {
286                     blocks = new int[] { -1, -1 };
287                 }
288                 coloring = null; // reset so it will be re-read
289
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]) { // just follows
306
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     /** Layer covering incremental search. There are just two positions
336      * begining and end of the searched string
337      *
338      * @deprecated Please use Highlighting SPI instead, for details see
339      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
340      */

341     public static class IncSearchLayer extends DrawLayer.AbstractLayer implements SettingsChangeListener{
342
343         /** Coloring to use for highlighting */
344         Coloring coloring;
345
346         Coloring invertedColoring;
347         
348         /** Position where the searched string begins */
349         int pos;
350
351         /** Length of area to highlight */
352         int len;
353
354         /** Whether this layer is enabled */
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         /** current INC_SEARCH_COLORING is used only in block search.
402          * if there is no search, use selection
403          * @param invert if true - selection coloring is used
404          */

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 JavaDoc 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     /**
440      * @deprecated Please use Highlighting SPI instead, for details see
441      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
442      */

443     public static class BlockSearchLayer extends DrawLayer.AbstractLayer implements SettingsChangeListener{
444
445         /** Coloring to use for highlighting */
446         Coloring coloring;
447
448         /** Position where the searched string begins */
449         int pos;
450
451         /** Length of area to highlight */
452         int len;
453
454         /** Whether this layer is enabled */
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) { // end of doc
502
nextRowStartPos = Integer.MAX_VALUE;
503                         }
504                         setNextActivityChangeOffset(Math.min(nextRowStartPos,(pos+len)));
505
506                     } catch (BadLocationException JavaDoc 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 JavaDoc 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 //
538
// XXX: Deprecated and not used anymore. Can be removed.
539
//
540
// /** Layer for guarded blocks
541
// *
542
// * @deprecated Please use Highlighting SPI instead, for details see
543
// * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
544
// */
545
// static class GuardedLayer extends ColorLineLayer {
546
//
547
// GuardedDocument doc;
548
//
549
// GuardedLayer() {
550
// super(GUARDED_LAYER_NAME);
551
// }
552
//
553
// public void init(DrawContext ctx) {
554
// super.init(ctx);
555
// doc = (GuardedDocument)ctx.getEditorUI().getDocument();
556
// }
557
//
558
// public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) {
559
// boolean active;
560
// if (mark != null) {
561
// active = mark.activateLayer;
562
// } else {
563
// active = doc.isPosGuarded(ctx.getFragmentOffset());
564
// }
565
//
566
// return active;
567
// }
568
//
569
// protected Coloring getColoring(DrawContext ctx) {
570
// return ctx.getEditorUI().getColoring(SettingsNames.GUARDED_COLORING);
571
// }
572
//
573
// }
574

575     
576     
577     /**
578      * Style layer getting color settings from particular style
579      *
580      * @deprecated Please use Highlighting SPI instead, for details see
581      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
582      */

583     public static class StyleLayer extends DrawLayer.AbstractLayer {
584
585         protected Style JavaDoc style;
586
587         protected MarkChain markChain;
588
589         protected Color JavaDoc backColor;
590
591         protected Color JavaDoc foreColor;
592
593         public StyleLayer(String JavaDoc layerName, BaseDocument doc, Style JavaDoc 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) { // end of doc
623
nextRowStartPos = Integer.MAX_VALUE;
624                         }
625
626                         setNextActivityChangeOffset(nextRowStartPos);
627
628                     } catch (BadLocationException JavaDoc 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 JavaDoc toString() {
648             return super.toString() + ((markChain != null) ? (", " + markChain) : ""); // NOI18N
649
}
650
651     }
652
653     /**
654      * Test layer for coloring the specific words
655      *
656      * @deprecated Please use Highlighting SPI instead, for details see
657      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
658      */

659     public static class WordColoringLayer extends DrawLayer.AbstractLayer {
660
661         protected StringMap stringMap = new StringMap();
662
663         public WordColoringLayer(String JavaDoc name) {
664             super(name);
665         }
666
667         public void put(String JavaDoc s, Coloring c) {
668             stringMap.put(s, c);
669         }
670
671         public void put(String JavaDoc[] 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 JavaDoc stringList, Coloring c) {
678             String JavaDoc strings[] = new String JavaDoc[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     // XXX: AnnotationLayer needs to be rewritten using the new Highlighting SPI.
701
/**
702      * Annotation layer for drawing of annotations. Each mark which is stored in markChain has
703      * corresponding Annotation. More than one Annotation can share one mark. In this case
704      * the only one annotation is active and this must be drawn.
705      *
706      * @deprecated Please use Highlighting SPI instead, for details see
707      * <a HREF="@org-netbeans-modules-editor-lib2@/overview-summary.html">Editor Library 2</a>.
708      */

709     public static class AnnotationLayer extends DrawLayer.AbstractLayer {
710
711         /** Current coloring */
712         private Coloring coloring;
713         
714         /** Chain of marks attached to this layer */
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         /** Get chain of marks attached to this draw layer
724          * @return mark chain */

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 JavaDoc 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             // Gets the active annotation attached to this mark. It is possible that
752
// no active annotation might exist for the mark, e.g. there can be
753
// mark at the beginning of the line for a whole line annotation
754
// and there can be mark in the middle of the line for a line-part annotation
755
AnnotationDesc anno = doc.getAnnotations().getActiveAnnotation(mark);
756             if (anno == null) {
757                 // if no active annotation was found for the given mark, check
758
// whether we are not already drawing some other annotation. If that's
759
// true we have to continue drawing it (means return true here)
760
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 JavaDoc e) {
775                 return false;
776             }
777             if (nextLineStart < 0) { // end of doc
778
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 // The following code ensures that if active annotation does not
793
// have highlight the color of next one will be used.
794
// It was decided that it will not be used
795
//
796
// if (coloring.getBackColor() == null) {
797
// AnnotationDesc[] annos = doc.getAnnotations().getPasiveAnnotations(anno.getLine());
798
// if (annos != null) {
799
// for (int i=0; i<annos.length; i++) {
800
// if (annos[i].getColoring().getBackColor() != null) {
801
// coloring = annos[i].getColoring();
802
// break;
803
// }
804
// }
805
// }
806
// }
807

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 JavaDoc view, String JavaDoc 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 { // found
836
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 JavaDoc e) {}
853         return null;
854     }
855 }
856
Popular Tags