001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.gsfret.editor.semantic;
042:
043: import java.io.IOException;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashSet;
047: import java.util.Map;
048: import java.util.Set;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import javax.swing.text.Document;
052: import org.netbeans.modules.gsf.api.OffsetRange;
053: import org.netbeans.modules.gsf.api.ColoringAttributes;
054: import org.netbeans.modules.gsf.api.SemanticAnalyzer;
055: import org.netbeans.napi.gsfret.source.CompilationInfo;
056: import org.netbeans.modules.editor.highlights.spi.Highlight;
057: import org.netbeans.modules.editor.highlights.spi.Highlighter;
058: import org.netbeans.modules.gsf.Language;
059: import org.netbeans.modules.gsf.LanguageRegistry;
060: import org.openide.ErrorManager;
061: import org.openide.cookies.EditorCookie;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.loaders.DataObject;
065:
066: /**
067: * This file is originally from Retouche, the Java Support
068: * infrastructure in NetBeans. I have modified the file as little
069: * as possible to make merging Retouche fixes back as simple as
070: * possible.
071: *
072: *
073: * @author Jan Lahoda
074: */
075: public class SemanticHighlighter extends
076: ScanningCancellableTask<CompilationInfo> {
077:
078: private FileObject file;
079:
080: /** Creates a new instance of SemanticHighlighter */
081: SemanticHighlighter(FileObject file) {
082: this .file = file;
083: }
084:
085: public Document getDocument() {
086: try {
087: DataObject d = DataObject.find(file);
088: EditorCookie ec = (EditorCookie) d
089: .getCookie(EditorCookie.class);
090:
091: if (ec == null)
092: return null;
093:
094: return ec.getDocument();
095: } catch (IOException e) {
096: Logger.global.log(Level.INFO,
097: "SemanticHighlighter: Cannot find DataObject for file: "
098: + FileUtil.getFileDisplayName(file), e);
099: return null;
100: }
101: }
102:
103: public @Override
104: void run(CompilationInfo info) {
105: resume();
106:
107: Document doc = getDocument();
108:
109: if (doc == null) {
110: Logger.global.log(Level.INFO,
111: "SemanticHighlighter: Cannot get document!");
112: return;
113: }
114:
115: Set<Highlight> highlights = process(info, doc);
116:
117: if (isCancelled())
118: return;
119:
120: Highlighter.getDefault().setHighlights(file, "semantic",
121: highlights);
122: OccurrencesMarkProvider.get(doc).setSematic(highlights);
123: }
124:
125: Set<Highlight> process(CompilationInfo info, final Document doc) {
126: if (isCancelled())
127: return Collections.emptySet();
128:
129: long start = System.currentTimeMillis();
130: Set<Highlight> result = new HashSet();
131:
132: Set<String> mimeTypes = info.getEmbeddedMimeTypes();
133: LanguageRegistry registry = LanguageRegistry.getInstance();
134:
135: for (String mimeType : mimeTypes) {
136: Language language = registry
137: .getLanguageByMimeType(mimeType);
138: if (language == null) {
139: continue;
140: }
141: SemanticAnalyzer task = language.getSemanticAnalyzer();
142: if (task != null) {
143: // Allow language plugins to do their own analysis too
144: try {
145: task.run(info);
146: } catch (Exception ex) {
147: ErrorManager.getDefault().notify(ex);
148: }
149:
150: if (isCancelled()) {
151: task.cancel();
152: return Collections.emptySet();
153: }
154:
155: Map<OffsetRange, ColoringAttributes> highlights = task
156: .getHighlights();
157: if (highlights != null) {
158:
159: for (OffsetRange range : highlights.keySet()) {
160: if (isCancelled())
161: return result;
162:
163: ColoringAttributes colors = highlights
164: .get(range);
165: Collection<ColoringAttributes> c = Collections
166: .singletonList(colors);
167: Highlight h = Utilities.createHighlight(
168: language, doc, range.getStart(), range
169: .getEnd(), c, null);
170:
171: if (h != null) {
172: result.add(h);
173: }
174: }
175: }
176: }
177: }
178:
179: //TimesCollector.getDefault().reportTime(((DataObject) doc.getProperty(Document.StreamDescriptionProperty)).getPrimaryFile(), "semantic", "Semantic", (System.currentTimeMillis() - start));
180:
181: return result;
182: }
183: }
|