1 19 package org.netbeans.modules.ruby; 20 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.List ; 24 25 import javax.swing.tree.TreeNode ; 26 27 import org.jruby.ast.ClassNode; 28 import org.jruby.ast.Colon2Node; 29 import org.jruby.ast.ConstDeclNode; 30 import org.jruby.ast.DefnNode; 31 import org.jruby.ast.DefsNode; 32 import org.jruby.ast.GlobalVarNode; 33 import org.jruby.ast.ModuleNode; 34 import org.jruby.ast.NewlineNode; 35 import org.jruby.ast.Node; 36 import org.jruby.ast.types.INameNode; 37 import org.netbeans.api.gsf.ParserResult; 38 import org.openide.util.Enumerations; 39 40 41 42 @SuppressWarnings ("unchecked") 43 class AstNodeAdapter implements ParserResult.AstTreeNode { 44 private static final boolean HIDE_NEWLINE_NODES = false; 45 private Node node; 46 private AstNodeAdapter parent; 47 private AstNodeAdapter[] children; 48 49 AstNodeAdapter(AstNodeAdapter parent, Node node) { 50 this.parent = parent; 51 this.node = node; 52 } 53 54 private void ensureChildrenInitialized() { 55 if (children != null) { 56 return; 57 } 58 59 if (HIDE_NEWLINE_NODES) { 60 List <AstNodeAdapter> childList = new ArrayList <AstNodeAdapter>(); 61 addChildren(childList, node); 62 children = childList.toArray(new AstNodeAdapter[childList.size()]); 63 } else { 64 List <Node > subnodes = (List <Node >)node.childNodes(); 65 children = new AstNodeAdapter[subnodes.size()]; 66 67 int index = 0; 68 69 for (Node child : subnodes) { 70 children[index++] = new AstNodeAdapter(this, child); 71 } 72 } 73 } 74 75 private void addChildren(List <AstNodeAdapter> children, Node node) { 76 List <Node > subnodes = (List <Node >)node.childNodes(); 77 78 for (Node child : subnodes) { 79 if (child instanceof NewlineNode) { 80 addChildren(children, child); 81 } else { 82 children.add(new AstNodeAdapter(this, child)); 83 } 84 } 85 } 86 87 public TreeNode getChildAt(int i) { 88 ensureChildrenInitialized(); 89 90 return children[i]; 91 } 92 93 public int getChildCount() { 94 ensureChildrenInitialized(); 95 96 return children.length; 97 } 98 99 public TreeNode getParent() { 100 ensureChildrenInitialized(); 101 102 return parent; 103 } 104 105 public int getIndex(TreeNode treeNode) { 106 ensureChildrenInitialized(); 107 108 for (int i = 0; i < children.length; i++) { 109 if (children[i] == treeNode) { 110 return i; 111 } 112 } 113 114 return -1; 115 } 116 117 public boolean getAllowsChildren() { 118 ensureChildrenInitialized(); 119 120 return children.length > 0; 121 } 122 123 public boolean isLeaf() { 124 ensureChildrenInitialized(); 125 126 return children.length == 0; 127 } 128 129 public Enumeration children() { 130 ensureChildrenInitialized(); 131 132 return Enumerations.array(children); 133 } 134 135 public String toString() { 136 StringBuilder sb = new StringBuilder (); 137 138 sb.append("<html>"); 139 sb.append(node.toString()); 140 sb.append("<i>"); 141 sb.append(" ("); 142 sb.append(getStartOffset()); 143 sb.append("-"); 144 sb.append(getEndOffset()); 145 sb.append(") "); 146 sb.append("</i>"); 147 String name = null; 148 149 if (node instanceof INameNode) { 150 name = ((INameNode) node).getName(); 151 } else if (node instanceof DefnNode) { 152 name = ((DefnNode) node).getName(); 153 } else if (node instanceof DefsNode) { 154 name = ((DefsNode) node).getName(); 155 } else if (node instanceof ConstDeclNode) { 156 name = ((ConstDeclNode) node).getName(); 157 } else if (node instanceof GlobalVarNode) { 158 name = ((GlobalVarNode) node).getName(); 159 } else if (node instanceof ClassNode) { 160 Node n = ((ClassNode) node).getCPath(); 161 162 if (n instanceof Colon2Node) { 163 Colon2Node c2n = (Colon2Node) n; 164 165 name = c2n.getName(); 166 } else { 167 name = n.toString(); 168 } 169 } else if (node instanceof ModuleNode) { 170 Node n = ((ModuleNode) node).getCPath(); 171 172 if (n instanceof Colon2Node) { 173 Colon2Node c2n = (Colon2Node) n; 174 175 name = c2n.getName(); 176 } else { 177 name = n.toString(); 178 } 179 } 180 if (name != null) { 181 sb.append(" : <b>"); 182 sb.append(name); 183 sb.append("</b>"); 184 } 185 sb.append("</html>"); 186 return sb.toString(); 187 } 188 189 public int getStartOffset() { 190 if (node.getPosition() != null) { 191 return node.getPosition().getStartOffset(); 192 } else { 193 return -1; 194 } 195 } 196 197 public int getEndOffset() { 198 if (node.getPosition() != null) { 199 return node.getPosition().getEndOffset(); 200 } else { 201 return -1; 202 } 203 } 204 205 public Object getAstNode() { 206 return node; 207 } 208 } 209 | Popular Tags |