KickJava   Java API By Example, From Geeks To Geeks.

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


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.Tree.Kind;
22 import com.sun.source.util.TreePath;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.netbeans.api.java.source.CompilationInfo;
27 import org.netbeans.api.java.source.support.CaretAwareJavaSourceTaskFactory;
28 import org.netbeans.modules.java.editor.semantic.ScanningCancellableTask;
29 import org.netbeans.modules.java.hints.spi.TreeRule;
30 import org.netbeans.spi.editor.hints.ErrorDescription;
31 import org.netbeans.spi.editor.hints.HintsController;
32
33 /**
34  *
35  * @author Jan Lahoda
36  */

37 public class SuggestionsTask extends ScanningCancellableTask<CompilationInfo> {
38     
39     public SuggestionsTask() {
40     }
41     
42     public void run(CompilationInfo info) throws Exception JavaDoc {
43         Map JavaDoc<Kind, List JavaDoc<TreeRule>> suggestions = RulesManager.getInstance().getSuggestions();
44         
45         if (suggestions.isEmpty()) {
46             return ;
47         }
48         
49         int position = CaretAwareJavaSourceTaskFactory.getLastPosition(info.getFileObject());
50         List JavaDoc<ErrorDescription> result = new ArrayList JavaDoc<ErrorDescription>();
51         
52         if (position != (-1)) {
53             TreePath tp = info.getTreeUtilities().pathFor(position);
54             
55             while (tp != null) {
56                 if (isCancelled())
57                     return ;
58                 
59                 Kind k = tp.getLeaf().getKind();
60                 List JavaDoc<TreeRule> rules = suggestions.get(k);
61                 
62                 if (rules != null) {
63                     for (TreeRule rule : rules) {
64                         if (isCancelled())
65                             return ;
66                         
67                         List JavaDoc<ErrorDescription> errors = rule.run(info, tp);
68                         
69                         if (errors != null) {
70                             result.addAll(errors);
71                         }
72                     }
73                 }
74                 
75                 tp = tp.getParentPath();
76             }
77         }
78         
79         if (isCancelled())
80             return ;
81         
82         HintsController.setErrors(info.getFileObject(), SuggestionsTask.class.getName(), result);
83     }
84     
85 }
86
Popular Tags