KickJava   Java API By Example, From Geeks To Geeks.

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


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

54 public class SemanticHighlighter extends ScanningCancellableTask<CompilationInfo> {
55     
56     private FileObject file;
57     
58     /** Creates a new instance of SemanticHighlighter */
59     SemanticHighlighter(FileObject file) {
60         this.file = file;
61     }
62
63     public Document JavaDoc getDocument() {
64         try {
65             DataObject d = DataObject.find(file);
66             EditorCookie ec = (EditorCookie) d.getCookie(EditorCookie.class);
67             
68             if (ec == null)
69                 return null;
70             
71             return ec.getDocument();
72         } catch (IOException JavaDoc e) {
73             Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot find DataObject for file: " + FileUtil.getFileDisplayName(file), e);
74             return null;
75         }
76     }
77     
78     public @Override JavaDoc void run(CompilationInfo info) {
79         resume();
80         
81         Document JavaDoc doc = getDocument();
82
83         if (doc == null) {
84             Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot get document!");
85             return;
86         }
87
88         Set JavaDoc<Highlight> highlights = process(info, doc);
89         
90         if (isCancelled())
91             return;
92         
93         Highlighter.getDefault().setHighlights(file, "semantic", highlights);
94         OccurrencesMarkProvider.get(doc).setSematic(highlights);
95     }
96     
97
98     Set JavaDoc<Highlight> process(CompilationInfo info, final Document JavaDoc doc) {
99         if (isCancelled())
100             return Collections.emptySet();
101         
102         long start = System.currentTimeMillis();
103         Set JavaDoc<Highlight> result = new HashSet JavaDoc();
104
105         // Allow language plugins to do their own analysis too
106
Parser parser = info.getParser();
107         if (parser != null) {
108             SemanticAnalyzer task = parser.getSemanticAnalysisTask();
109             if (task != null) {
110                 try {
111                     task.run(info);
112                 } catch (Exception JavaDoc ex) {
113                     ErrorManager.getDefault().notify(ex);
114                 }
115                 
116                 if (isCancelled()) {
117                     task.cancel();
118                     return Collections.emptySet();
119                 }
120                 
121                 Map JavaDoc<OffsetRange,ColoringAttributes> highlights = task.getHighlights();
122                 if (highlights != null) {
123
124                     for (OffsetRange range : highlights.keySet()) {
125                         if (isCancelled())
126                             return result;
127
128                         ColoringAttributes colors = highlights.get(range);
129                         Collection JavaDoc<ColoringAttributes> c = Collections.singletonList(colors);
130                         Highlight h = Utilities.createHighlight(doc, range.getStart(), range.getEnd(), c, null);
131
132                         if (h != null) {
133                             result.add(h);
134                         }
135                     }
136                 }
137             }
138         }
139         
140         //TimesCollector.getDefault().reportTime(((DataObject) doc.getProperty(Document.StreamDescriptionProperty)).getPrimaryFile(), "semantic", "Semantic", (System.currentTimeMillis() - start));
141

142         return result;
143     }
144 }
145
Popular Tags