1 19 package org.netbeans.modules.java.debug; 20 21 import com.sun.source.tree.Tree; 22 import java.awt.Color ; 23 import java.util.ArrayList ; 24 import java.util.List ; 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 43 public class ElementNode extends AbstractNode implements Highlight { 44 45 private static final Coloring HIGHLIGHT = new Coloring(null, null, new Color (224, 224, 224)); 46 47 private Element element; 48 private CompilationInfo info; 49 50 54 public static Node getTree(CompilationInfo info, Element element) { 55 List <Node> result = new ArrayList (); 56 57 new FindChildrenElementVisitor(info).scan(element, result); 58 59 return result.get(0); 60 } 61 62 63 public ElementNode(CompilationInfo info, Element element, List <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()); setIconBaseWithExtension("org/netbeans/modules/java/debug/resources/element.png"); } 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 <Node> nodes) { 96 setKeys(nodes); 97 } 98 99 protected Node[] createNodes(Object key) { 100 return new Node[] {(Node) key}; 101 } 102 103 } 104 105 private static class FindChildrenElementVisitor extends ElementScanner6<Void , List <Node>> { 106 107 private CompilationInfo info; 108 109 public FindChildrenElementVisitor(CompilationInfo info) { 110 this.info = info; 111 } 112 113 public Void visitPackage(PackageElement e, List <Node> p) { 114 List <Node> below = new ArrayList <Node>(); 115 116 super.visitPackage(e, below); 117 118 p.add(new ElementNode(info, e, below)); 119 return null; 120 } 121 122 public Void visitType(TypeElement e, List <Node> p) { 123 List <Node> below = new ArrayList <Node>(); 124 125 super.visitType(e, below); 126 127 p.add(new ElementNode(info, e, below)); 128 return null; 129 } 130 131 public Void visitVariable(VariableElement e, List <Node> p) { 132 List <Node> below = new ArrayList <Node>(); 133 134 super.visitVariable(e, below); 135 136 p.add(new ElementNode(info, e, below)); 137 return null; 138 } 139 140 public Void visitExecutable(ExecutableElement e, List <Node> p) { 141 List <Node> below = new ArrayList <Node>(); 142 143 super.visitExecutable(e, below); 144 145 p.add(new ElementNode(info, e, below)); 146 return null; 147 } 148 149 public Void visitTypeParameter(TypeParameterElement e, List <Node> p) { 150 List <Node> below = new ArrayList <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 |