KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > highlights > HighlightLayer


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 package org.netbeans.modules.editor.highlights;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.SortedSet JavaDoc;
28 import java.util.TreeSet JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import org.netbeans.editor.Coloring;
31 import org.netbeans.editor.DrawContext;
32 import org.netbeans.editor.DrawLayer;
33 import org.netbeans.editor.MarkFactory;
34 import org.netbeans.modules.editor.highlights.spi.*;
35 import org.openide.ErrorManager;
36
37 /**
38  *
39  * @author Jan Lahoda
40  */

41 final class HighlightLayer extends DrawLayer.AbstractLayer {
42     
43     /** Creates a new instance of HighlightLayer */
44     public HighlightLayer() {
45         super("highlight-layer"); // NOI18N
46
this.initialized = false;
47         type2Highlights = new HashMap JavaDoc/*<String, SortedSet<Highlight>>*/();
48         fakeHighlight = new FakeHighlight();
49     }
50     
51     private Map JavaDoc/*<String, SortedSet<Highlight>>*/ type2Highlights;
52     
53     private Highlight fakeHighlight;
54     private int fakePosition;
55     
56     public static final int VISIBILITY = 3000;
57     
58     private boolean initialized = false;
59     
60     private void checkDocument(Document JavaDoc doc) {
61     }
62     
63     public boolean extendsEOL() {
64         return true;
65     }
66     
67     public synchronized void init(final DrawContext ctx) {
68         if (!initialized) {
69             Document JavaDoc doc = ctx.getEditorUI().getDocument();
70             
71             initialized = true;
72             checkDocument(doc);
73         }
74         
75         if (isActive())
76             setNextActivityChangeOffset(0);
77     }
78     
79     private boolean isActive() {
80         return true;
81     }
82
83     public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) {
84         if (isActive()) {
85             int currentOffset = ctx.getFragmentOffset();
86             List JavaDoc/*<Highlight>*/ highlights = getHighlightsForOffset(currentOffset);
87             int nextActivity = Integer.MAX_VALUE;
88
89 // for (Highlight h : highlights) {
90
for (Iterator JavaDoc i = highlights.iterator(); i.hasNext(); ) {
91                 Highlight h = (Highlight) i.next();
92                 
93                 if (h.getStart() <= currentOffset) {
94                     if (nextActivity > h.getEnd()) {
95                         nextActivity = h.getEnd();
96                     }
97                 } else {
98                     if (nextActivity > h.getStart()) {
99                         nextActivity = h.getStart();
100                     }
101                 }
102             }
103
104             if (nextActivity == currentOffset) {
105                 nextActivity++;
106             }
107
108             setNextActivityChangeOffset(nextActivity);
109             return true;
110         }
111
112         return false;
113     }
114     
115     public void updateContext(DrawContext ctx) {
116         if (!isActive())
117             return ;
118         
119         int currentOffset = /*ctx.getTokenOffset();/*/ctx.getFragmentOffset();
120         List JavaDoc/*<Highlight>*/ highlights = getHighlightsForOffset(currentOffset);
121         int nextActivity = Integer.MAX_VALUE;
122         int starts = Integer.MAX_VALUE;
123         boolean applied = false;
124
125 // for (Highlight h : highlights) {
126
for (Iterator JavaDoc i = highlights.iterator(); i.hasNext(); ) {
127             Highlight h = (Highlight) i.next();
128             
129             if (h.getStart() <= currentOffset && currentOffset < h.getEnd()) {
130                 h.getColoring().apply(ctx);
131                 applied = true;
132             }
133
134             if (nextActivity > h.getEnd()) {
135                 nextActivity = h.getEnd();
136             }
137
138             if (starts > h.getStart()) {
139                 starts = h.getStart();
140             }
141         }
142
143         if (!applied && starts < nextActivity)
144             nextActivity = starts;
145
146         setNextActivityChangeOffset(nextActivity);
147     }
148
149     public synchronized void setHighlights(String JavaDoc type, Collection JavaDoc/*<Highlight>*/ highlights) {
150         SortedSet JavaDoc/*<Highlight>*/ target = (SortedSet JavaDoc) type2Highlights.get(type);
151         
152         if (target == null) {
153             type2Highlights.put(type, target = new TreeSet JavaDoc/*<Highlight>*/(new HighlightComparator()));
154         } else {
155             target.clear();
156         }
157         
158 // for (Highlight h : highlights) {
159
for (Iterator JavaDoc i = highlights.iterator(); i.hasNext(); ) {
160             Highlight h = (Highlight) i.next();
161             
162             if (h != null) {
163                 target.add(h);
164             } else {
165                 ErrorManager.getDefault().log(ErrorManager.WARNING, "Got a null highlight, type: " + type); // NOI18N
166
}
167         }
168     }
169     
170     private synchronized List JavaDoc/*<Highlight>*/ getHighlightsForOffset(int offset) {
171         List JavaDoc/*<Highlight>*/ highlights = new ArrayList JavaDoc/*<Highlight>*/();
172         
173         fakePosition = offset;
174         
175 // for (String type : type2Highlights.keySet()) {
176
for (Iterator JavaDoc i = type2Highlights.keySet().iterator(); i.hasNext(); ) {
177             String JavaDoc type = (String JavaDoc) i.next();
178             
179             SortedSet JavaDoc/*<Highlight>*/ tail = ((SortedSet JavaDoc) type2Highlights.get(type)).tailSet(fakeHighlight);
180             
181             if (!tail.isEmpty()) {
182                 Highlight h = (Highlight) tail.first();
183                 
184                 highlights.add(h);
185             }
186         }
187         
188         return highlights;
189     }
190     
191     private final class FakeHighlight implements Highlight {
192         
193         public int getStart() {
194             return -1;
195         }
196         
197         public int getEnd() {
198             return fakePosition;
199         }
200         
201         public Coloring getColoring() {
202             return null;
203         }
204         
205     }
206 }
207
Popular Tags