KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > editor > semantic > MarkOccurencesHighlighter


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.retouche.editor.semantic;
20
21 import java.awt.Color JavaDoc;
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.event.CaretEvent JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
34 import org.netbeans.api.gsf.OffsetRange;
35 import org.netbeans.api.gsf.Parser;
36 import org.netbeans.api.gsf.CancellableTask;
37 import org.netbeans.api.gsf.ColoringAttributes;
38 import org.netbeans.api.gsf.OccurrencesFinder;
39 import org.netbeans.api.retouche.source.CompilationInfo;
40 import org.netbeans.api.retouche.source.CompilationUnitTree;
41 //import org.netbeans.api.timers.TimesCollector;
42
import org.netbeans.modules.editor.highlights.spi.Highlight;
43 import org.netbeans.modules.editor.highlights.spi.Highlighter;
44 import org.openide.ErrorManager;
45 import org.openide.cookies.EditorCookie;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.loaders.DataObject;
49
50 /**
51  * This file is originally from Retouche, the Java Support
52  * infrastructure in NetBeans. I have modified the file as little
53  * as possible to make merging Retouche fixes back as simple as
54  * possible.
55  *
56  *
57  * @author Jan Lahoda
58  */

59 public class MarkOccurencesHighlighter implements CancellableTask<CompilationInfo> {
60     
61     private FileObject file;
62     
63     /** Creates a new instance of SemanticHighlighter */
64     MarkOccurencesHighlighter(FileObject file) {
65         this.file = file;
66     }
67     
68     public static final Color JavaDoc ES_COLOR = new Color JavaDoc( 175, 172, 102 ); // new Color(244, 164, 113);
69

70     public Document JavaDoc getDocument() {
71         try {
72             DataObject d = DataObject.find(file);
73             EditorCookie ec = (EditorCookie) d.getCookie(EditorCookie.class);
74             
75             if (ec == null)
76                 return null;
77             
78             return ec.getDocument();
79         } catch (IOException JavaDoc e) {
80             Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot find DataObject for file: " + FileUtil.getFileDisplayName(file), e);
81             return null;
82         }
83     }
84     
85     public void run(CompilationInfo info) {
86         resume();
87         
88         Document JavaDoc doc = getDocument();
89         
90         if (doc == null) {
91             Logger.global.log(Level.INFO, "MarkOccurencesHighlighter: Cannot get document!");
92             return ;
93         }
94         
95         long start = System.currentTimeMillis();
96         
97         int caretPosition = MarkOccurrencesHighlighterFactory.getLastPosition(file);
98         
99         if (isCancelled())
100             return;
101         
102         Set JavaDoc<Highlight> highlights = processImpl(info, doc, caretPosition);
103         
104         if (isCancelled())
105             return;
106         
107         //TimesCollector.getDefault().reportTime(((DataObject) doc.getProperty(Document.StreamDescriptionProperty)).getPrimaryFile(), "occurrences", "Occurrences", (System.currentTimeMillis() - start));
108

109         Highlighter.getDefault().setHighlights(file, "occurrences", highlights);
110         OccurrencesMarkProvider.get(doc).setOccurrences(highlights);
111     }
112     
113 // private boolean isIn(CompilationUnitTree cu, SourcePositions sp, Tree tree, int position) {
114
// return sp.getStartPosition(cu, tree) <= position && position <= sp.getEndPosition(cu, tree);
115
// }
116
//
117
private boolean isIn(int caretPosition, int[] span) {
118         return span[0] <= caretPosition && caretPosition <= span[1];
119     }
120     
121     Set JavaDoc<Highlight> processImpl(CompilationInfo info, Document JavaDoc doc, int caretPosition) {
122         Set JavaDoc<Highlight> localUsages = null;
123
124         // Allow language plugins to do their own analysis too
125
Parser parser = info.getParser();
126         if (parser != null) {
127             OccurrencesFinder task = parser.getMarkOccurrencesTask(caretPosition);
128             if (task != null) {
129                 try {
130                     task.run(info);
131                 } catch (Exception JavaDoc ex) {
132                     ErrorManager.getDefault().notify(ex);
133                 }
134                 
135                 if (isCancelled()) {
136                     task.cancel();
137                 }
138                 
139                 
140                 Map JavaDoc<OffsetRange,ColoringAttributes> highlights = task.getOccurrences();
141                 if (highlights != null) {
142                     for (OffsetRange range : highlights.keySet()) {
143                         if (isCancelled())
144                             return Collections.EMPTY_SET;
145
146                         ColoringAttributes colors = highlights.get(range);
147                         //Collection<ColoringAttributes> c = EnumSet.copyOf(set);
148
Collection JavaDoc<ColoringAttributes> c = Collections.singletonList(colors);
149                         Highlight h = Utilities.createHighlight(doc, range.getStart(), range.getEnd(), c, null);
150
151                         if (h != null) {
152                             if (localUsages == null) {
153                                 localUsages = new HashSet JavaDoc<Highlight>();
154                             }
155                             localUsages.add(h);
156                         }
157                     }
158                 }
159             }
160         }
161         
162         // CompilationUnitTree cu = info.getCompilationUnit();
163
// Tree lastTree = null;
164
// TreePath tp = info.getTreeUtilities().pathFor(caretPosition);
165
// while(tp != null) {
166
// if (isCancelled())
167
// return Collections.emptySet();
168
//
169
// Tree tree = tp.getLeaf();
170
// //detect caret inside the return type or throws clause:
171
// if (tree instanceof MethodTree && (lastTree instanceof IdentifierTree
172
// || lastTree instanceof PrimitiveTypeTree
173
// || lastTree instanceof MemberSelectTree)) {
174
// //hopefully found something, check:
175
// MethodTree decl = (MethodTree) tree;
176
// Tree type = decl.getReturnType();
177
//
178
// if (isIn(cu, info.getTrees().getSourcePositions(), type, caretPosition)) {
179
// MethodExitDetector med = new MethodExitDetector();
180
//
181
// setExitDetector(med);
182
//
183
// try {
184
// return med.process(info, doc, decl, null);
185
// } finally {
186
// setExitDetector(null);
187
// }
188
// }
189
//
190
// for (Tree exc : decl.getThrows()) {
191
// if (isIn(cu, info.getTrees().getSourcePositions(), exc, caretPosition)) {
192
// MethodExitDetector med = new MethodExitDetector();
193
//
194
// setExitDetector(med);
195
//
196
// try {
197
// return med.process(info, doc, decl, Collections.singletonList(exc));
198
// } finally {
199
// setExitDetector(null);
200
// }
201
// }
202
// }
203
// }
204
//
205
// if (localUsages != null)
206
// return localUsages;
207
//
208
// //variable declaration:
209
// Element el = info.getTrees().getElement(tp);
210
// if ( el != null
211
// && (!(tree instanceof ClassTree) || isIn(caretPosition, Utilities.findIdentifierSpan(tp, cu, info.getTrees().getSourcePositions(), doc)))
212
// && !Utilities.isKeyword(tree)
213
// && (!(tree instanceof MethodTree) || lastTree == null)) {
214
// FindLocalUsagesQuery fluq = new FindLocalUsagesQuery();
215
//
216
// setLocalUsages(fluq);
217
//
218
// try {
219
// localUsages = fluq.findUsages(el, info, doc);
220
// } finally {
221
// setLocalUsages(null);
222
// }
223
// }
224
// lastTree = tree;
225
// tp = tp.getParentPath();
226
// }
227
//
228
if (localUsages != null)
229             return localUsages;
230         
231         return Collections.emptySet();
232     }
233     
234     private boolean canceled;
235     private MethodExitDetector exitDetector;
236 // private FindLocalUsagesQuery localUsages;
237
//
238
// private final synchronized void setExitDetector(MethodExitDetector detector) {
239
// this.exitDetector = detector;
240
// }
241

242     public final synchronized void cancel() {
243         canceled = true;
244         
245         if (exitDetector != null) {
246 System.err.println("need to cancel exit detector!");
247 // exitDetector.cancel();
248
}
249 // if (localUsages != null) {
250
// localUsages.cancel();
251
// }
252
}
253     
254     protected final synchronized boolean isCancelled() {
255         return canceled;
256     }
257     
258     protected final synchronized void resume() {
259         canceled = false;
260     }
261     
262 }
263
Popular Tags