1 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 ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import javax.swing.SwingUtilities ; 45 import javax.swing.text.BadLocationException ; 46 import javax.swing.text.Document ; 47 48 49 53 public class AnnotationManager extends ASTEvaluator { 54 55 private NbEditorDocument doc; 56 private ParserManager parser; 57 private List <ASTItem> items; 58 private List <Feature> marks; 59 private List <LanguagesAnnotation> annotations = new ArrayList <LanguagesAnnotation> (); 60 61 62 63 public AnnotationManager (Document doc) { 64 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 <ASTItem> (); 72 marks = new ArrayList <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 <ASTItem> items, final List <Feature> marks) { 96 SwingUtilities.invokeLater (new Runnable () { 97 public void run () { 98 try { 99 Iterator <LanguagesAnnotation> it = annotations.iterator (); 100 while (it.hasNext ()) 101 doc.removeAnnotation (it.next ()); 102 annotations = new ArrayList <LanguagesAnnotation> (); 103 Iterator <ASTItem> it2 = items.iterator (); 104 Iterator <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 ) mark.getValue ("type"), 110 (String ) 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 ex) { 120 System.out.println ("AnnotationManager " + ex); 122 } 123 } 124 }); 125 } 126 127 128 130 private static class LanguagesAnnotation extends Annotation { 131 132 private String type; 133 private String description; 134 135 136 private LanguagesAnnotation ( 137 String type, 138 String description 139 ) { 140 this.type = type; 141 this.description = description; 142 } 143 144 149 public String getAnnotationType () { 150 return type; 151 } 152 153 156 public String getShortDescription () { 157 return description; 158 } 159 } 160 } 161 162 | Popular Tags |