KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > features > AnnotationManager


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.modules.languages.features;
21
22 import org.netbeans.api.languages.ASTEvaluator;
23 import org.netbeans.api.languages.ASTItem;
24 import org.netbeans.api.languages.LanguagesManager;
25 import org.netbeans.api.languages.ParseException;
26 import org.netbeans.api.languages.ASTPath;
27 import org.netbeans.api.languages.ParserManager;
28 import org.netbeans.api.languages.ParserManager.State;
29 import org.netbeans.api.languages.ASTToken;
30 import org.netbeans.api.languages.SyntaxContext;
31 import org.netbeans.api.languages.SyntaxContext;
32 import org.netbeans.api.languages.ASTNode;
33 import org.netbeans.api.languages.ParseException;
34 import org.netbeans.api.languages.ASTToken;
35 import org.netbeans.modules.editor.NbEditorDocument;
36 import org.netbeans.modules.languages.Feature;
37 import org.netbeans.modules.languages.Language;
38 import org.netbeans.modules.languages.LanguagesManagerImpl;
39 import org.openide.text.Annotation;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import javax.swing.SwingUtilities JavaDoc;
45 import javax.swing.text.BadLocationException JavaDoc;
46 import javax.swing.text.Document JavaDoc;
47
48
49 /**
50  *
51  * @author Jan Jancura
52  */

53 public class AnnotationManager extends ASTEvaluator {
54     
55     private NbEditorDocument doc;
56     private ParserManager parser;
57     private List JavaDoc<ASTItem> items;
58     private List JavaDoc<Feature> marks;
59     private List JavaDoc<LanguagesAnnotation> annotations = new ArrayList JavaDoc<LanguagesAnnotation> ();
60
61     
62     /** Creates a new instance of AnnotationManager */
63     public AnnotationManager (Document doc) {
64         //doc.addDocumentListener (this);
65
this.doc = (NbEditorDocument) doc;
66         parser = ParserManager.get ((NbEditorDocument) doc);
67         parser.addASTEvaluator (this);
68     }
69
70     public void beforeEvaluation (State state, ASTNode root) {
71         items = new ArrayList JavaDoc<ASTItem> ();
72         marks = new ArrayList JavaDoc<Feature> ();
73     }
74
75     public void afterEvaluation (State state, ASTNode root) {
76         refresh (items, marks);
77     }
78
79     public void evaluate (State state, ASTPath path) {
80         try {
81             ASTItem item = path.getLeaf ();
82             Language language = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).
83                 getLanguage (item.getMimeType ());
84             Feature mark = language.getFeature (Language.MARK, path);
85             if (mark != null) {
86                 if (mark.getBoolean ("condition", SyntaxContext.create (doc, path), true)) {
87                     items.add (item);
88                     marks.add (mark);
89                 }
90             }
91         } catch (ParseException ex) {
92         }
93     }
94     
95     private void refresh (final List JavaDoc<ASTItem> items, final List JavaDoc<Feature> marks) {
96         SwingUtilities.invokeLater (new Runnable JavaDoc () {
97             public void run () {
98                 try {
99                     Iterator JavaDoc<LanguagesAnnotation> it = annotations.iterator ();
100                     while (it.hasNext ())
101                         doc.removeAnnotation (it.next ());
102                     annotations = new ArrayList JavaDoc<LanguagesAnnotation> ();
103                     Iterator JavaDoc<ASTItem> it2 = items.iterator ();
104                     Iterator JavaDoc<Feature> it3 = marks.iterator ();
105                     while (it2.hasNext ()) {
106                         ASTItem item = it2.next ();
107                         Feature mark = it3.next ();
108                         LanguagesAnnotation la = new LanguagesAnnotation (
109                             (String JavaDoc) mark.getValue ("type"),
110                             (String JavaDoc) mark.getValue ("message")
111                         );
112                         doc.addAnnotation (
113                             doc.createPosition (item.getOffset ()),
114                             item.getLength (),
115                             la
116                         );
117                         annotations.add (la);
118                     }
119                 } catch (BadLocationException JavaDoc ex) {
120                     //ErrorManager.getDefault ().notify (ex);
121
System.out.println ("AnnotationManager " + ex);
122                 }
123             }
124         });
125     }
126
127     
128     // innerclasses ............................................................
129

130     private static class LanguagesAnnotation extends Annotation {
131
132         private String JavaDoc type;
133         private String JavaDoc description;
134
135         /** Creates a new instance of ToolsAnotation */
136         private LanguagesAnnotation (
137             String JavaDoc type,
138             String JavaDoc description
139         ) {
140             this.type = type;
141             this.description = description;
142         }
143
144         /** Returns name of the file which describes the annotation type.
145          * The file must be defined in module installation layer in the
146          * directory "Editors/AnnotationTypes"
147          * @return name of the anotation type
148          */

149         public String JavaDoc getAnnotationType () {
150             return type;
151         }
152
153         /** Returns the tooltip text for this annotation.
154          * @return tooltip for this annotation
155          */

156         public String JavaDoc getShortDescription () {
157             return description;
158         }
159     }
160 }
161
162
Popular Tags