KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > debug > ElementNode


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.debug;
20
21 import com.sun.source.tree.Tree;
22 import java.awt.Color JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.lang.model.element.Element;
26 import javax.lang.model.element.ExecutableElement;
27 import javax.lang.model.element.PackageElement;
28 import javax.lang.model.element.TypeElement;
29 import javax.lang.model.element.TypeParameterElement;
30 import javax.lang.model.element.VariableElement;
31 import javax.lang.model.util.ElementScanner6;
32 import org.netbeans.api.java.source.CompilationInfo;
33 import org.netbeans.editor.Coloring;
34 import org.netbeans.modules.editor.highlights.spi.Highlight;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38
39 /**
40  *
41  * @author Jan Lahoda
42  */

43 public class ElementNode extends AbstractNode implements Highlight {
44     
45     private static final Coloring HIGHLIGHT = new Coloring(null, null, new Color JavaDoc(224, 224, 224));
46
47     private Element element;
48     private CompilationInfo info;
49     
50 // public static Node getTree(CompilationInfo info) {
51
// return getTree(info, info.getElement(info.getTree().getTypeDecls().get(0)));
52
// }
53

54     public static Node getTree(CompilationInfo info, Element element) {
55         List JavaDoc<Node> result = new ArrayList JavaDoc();
56         
57         new FindChildrenElementVisitor(info).scan(element, result);
58         
59         return result.get(0);
60     }
61
62     /** Creates a new instance of TreeNode */
63     public ElementNode(CompilationInfo info, Element element, List JavaDoc<Node> nodes) {
64         super(nodes.isEmpty() ? Children.LEAF: new NodeChilren(nodes));
65         this.element = element;
66         this.info = info;
67         setDisplayName(element.getKind().toString() + ":" + element.toString()); //NOI18N
68
setIconBaseWithExtension("org/netbeans/modules/java/debug/resources/element.png"); //NOI18N
69
}
70
71     public int getStart() {
72         Tree tree = info.getTrees().getTree(element);
73         
74         if (tree != null)
75             return (int)info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tree);
76         else
77             return -1;
78     }
79     
80     public int getEnd() {
81         Tree tree = info.getTrees().getTree(element);
82         
83         if (tree != null)
84             return (int)info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), tree);
85         else
86             return -1;
87     }
88
89     public Coloring getColoring() {
90         return HIGHLIGHT;
91     }
92     
93     private static final class NodeChilren extends Children.Keys {
94         
95         public NodeChilren(List JavaDoc<Node> nodes) {
96             setKeys(nodes);
97         }
98         
99         protected Node[] createNodes(Object JavaDoc key) {
100             return new Node[] {(Node) key};
101         }
102         
103     }
104     
105     private static class FindChildrenElementVisitor extends ElementScanner6<Void JavaDoc, List JavaDoc<Node>> {
106         
107         private CompilationInfo info;
108         
109         public FindChildrenElementVisitor(CompilationInfo info) {
110             this.info = info;
111         }
112         
113         public Void JavaDoc visitPackage(PackageElement e, List JavaDoc<Node> p) {
114             List JavaDoc<Node> below = new ArrayList JavaDoc<Node>();
115
116             super.visitPackage(e, below);
117
118             p.add(new ElementNode(info, e, below));
119             return null;
120         }
121         
122         public Void JavaDoc visitType(TypeElement e, List JavaDoc<Node> p) {
123             List JavaDoc<Node> below = new ArrayList JavaDoc<Node>();
124
125             super.visitType(e, below);
126
127             p.add(new ElementNode(info, e, below));
128             return null;
129         }
130         
131         public Void JavaDoc visitVariable(VariableElement e, List JavaDoc<Node> p) {
132             List JavaDoc<Node> below = new ArrayList JavaDoc<Node>();
133
134             super.visitVariable(e, below);
135
136             p.add(new ElementNode(info, e, below));
137             return null;
138         }
139         
140         public Void JavaDoc visitExecutable(ExecutableElement e, List JavaDoc<Node> p) {
141             List JavaDoc<Node> below = new ArrayList JavaDoc<Node>();
142
143             super.visitExecutable(e, below);
144
145             p.add(new ElementNode(info, e, below));
146             return null;
147         }
148         
149         public Void JavaDoc visitTypeParameter(TypeParameterElement e, List JavaDoc<Node> p) {
150             List JavaDoc<Node> below = new ArrayList JavaDoc<Node>();
151
152             super.visitTypeParameter(e, below);
153
154             p.add(new ElementNode(info, e, below));
155             return null;
156         }
157         
158     }
159 }
160
Popular Tags