KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > AstNodeAdapter


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

19 package org.netbeans.modules.ruby;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.swing.tree.TreeNode JavaDoc;
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 /** For debugging only (used by the AST Viewer */
42 @SuppressWarnings JavaDoc("unchecked")
43 class AstNodeAdapter implements ParserResult.AstTreeNode {
44     private static final boolean HIDE_NEWLINE_NODES = false;
45     private Node JavaDoc node;
46     private AstNodeAdapter parent;
47     private AstNodeAdapter[] children;
48
49     AstNodeAdapter(AstNodeAdapter parent, Node JavaDoc 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 JavaDoc<AstNodeAdapter> childList = new ArrayList JavaDoc<AstNodeAdapter>();
61             addChildren(childList, node);
62             children = childList.toArray(new AstNodeAdapter[childList.size()]);
63         } else {
64             List JavaDoc<Node JavaDoc> subnodes = (List JavaDoc<Node JavaDoc>)node.childNodes();
65             children = new AstNodeAdapter[subnodes.size()];
66
67             int index = 0;
68
69             for (Node JavaDoc child : subnodes) {
70                 children[index++] = new AstNodeAdapter(this, child);
71             }
72         }
73     }
74
75     private void addChildren(List JavaDoc<AstNodeAdapter> children, Node JavaDoc node) {
76         List JavaDoc<Node JavaDoc> subnodes = (List JavaDoc<Node JavaDoc>)node.childNodes();
77
78         for (Node JavaDoc 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 JavaDoc 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 JavaDoc getParent() {
100         ensureChildrenInitialized();
101
102         return parent;
103     }
104
105     public int getIndex(TreeNode JavaDoc 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 JavaDoc children() {
130         ensureChildrenInitialized();
131
132         return Enumerations.array(children);
133     }
134
135     public String JavaDoc toString() {
136         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getAstNode() {
206         return node;
207     }
208 }
209
Popular Tags