KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > hints > infrastructure > HintsTask


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.hints.infrastructure;
20
21 import com.sun.source.tree.CompilationUnitTree;
22 import com.sun.source.tree.Tree;
23 import com.sun.source.tree.Tree.Kind;
24 import com.sun.source.util.TreePath;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.netbeans.api.java.source.CompilationInfo;
29 import org.netbeans.api.java.source.support.CancellableTreePathScanner;
30 import org.netbeans.modules.java.editor.semantic.ScanningCancellableTask;
31 import org.netbeans.modules.java.hints.spi.TreeRule;
32 import org.netbeans.spi.editor.hints.ErrorDescription;
33 import org.netbeans.spi.editor.hints.HintsController;
34
35 /**
36  *
37  * @author Jan Lahoda
38  */

39 public class HintsTask extends ScanningCancellableTask<CompilationInfo> {
40     
41     public HintsTask() {
42     }
43     
44     public void run(CompilationInfo info) throws Exception JavaDoc {
45         Map JavaDoc<Kind, List JavaDoc<TreeRule>> hints = RulesManager.getInstance().getHints();
46         
47         if (hints.isEmpty()) {
48             return ;
49         }
50         
51         List JavaDoc<ErrorDescription> result = new ArrayList JavaDoc<ErrorDescription>();
52         
53         scan(new ScannerImpl(info, hints), info.getCompilationUnit(), result);
54         
55         if (isCancelled())
56             return ;
57         
58         HintsController.setErrors(info.getFileObject(), HintsTask.class.getName(), result);
59     }
60     
61     private static final class ScannerImpl extends CancellableTreePathScanner<Void JavaDoc, List JavaDoc<ErrorDescription>> {
62         
63         private CompilationInfo info;
64         private Map JavaDoc<Kind, List JavaDoc<TreeRule>> hints;
65         
66         public ScannerImpl(CompilationInfo info, Map JavaDoc<Kind, List JavaDoc<TreeRule>> hints) {
67             this.info = info;
68             this.hints = hints;
69         }
70         
71         private void runAndAdd(TreePath path, List JavaDoc<TreeRule> rules, List JavaDoc<ErrorDescription> d) {
72             if (rules != null) {
73                 for (TreeRule tr : rules) {
74                     if (isCanceled()) {
75                         return ;
76                     }
77                     List JavaDoc<ErrorDescription> errors = tr.run(info, path);
78                     
79                     if (errors != null) {
80                         d.addAll(errors);
81                     }
82                 }
83             }
84         }
85         
86         @Override JavaDoc
87         public Void JavaDoc scan(Tree tree, List JavaDoc<ErrorDescription> p) {
88             if (tree == null)
89                 return null;
90             
91             TreePath tp = new TreePath(getCurrentPath(), tree);
92             Kind k = tree.getKind();
93             
94             runAndAdd(tp, hints.get(k), p);
95             
96             if (isCanceled()) {
97                 return null;
98             }
99             
100             return super.scan(tree, p);
101         }
102         
103         @Override JavaDoc
104         public Void JavaDoc scan(TreePath path, List JavaDoc<ErrorDescription> p) {
105             Kind k = path.getLeaf().getKind();
106             runAndAdd(path, hints.get(k), p);
107             
108             if (isCanceled()) {
109                 return null;
110             }
111             
112             return super.scan(path, p);
113         }
114         
115     }
116
117 }
118
Popular Tags