KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > gui > SpoonTreeBuilder


1 package spoon.support.gui;
2
3 import java.util.Stack JavaDoc;
4
5 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
6
7 import spoon.reflect.declaration.CtElement;
8 import spoon.reflect.declaration.CtNamedElement;
9 import spoon.reflect.reference.CtReference;
10 import spoon.reflect.visitor.CtScanner;
11
12 public class SpoonTreeBuilder extends CtScanner {
13     Stack JavaDoc<DefaultMutableTreeNode JavaDoc> nodes;
14
15     DefaultMutableTreeNode JavaDoc root;
16
17     public SpoonTreeBuilder() {
18         super();
19         root = new DefaultMutableTreeNode JavaDoc("Spoon Tree Root");
20         nodes = new Stack JavaDoc<DefaultMutableTreeNode JavaDoc>();
21         nodes.push(root);
22     }
23
24     private void createNode(Object JavaDoc o) {
25         DefaultMutableTreeNode JavaDoc node = new DefaultMutableTreeNode JavaDoc(o) {
26             private static final long serialVersionUID = 1L;
27
28             @Override JavaDoc
29             public String JavaDoc toString() {
30                 if (getUserObject() instanceof CtNamedElement) {
31                     return getUserObject().getClass().getSimpleName()
32                             + " - "
33                             + ((CtNamedElement) getUserObject())
34                                     .getSimpleName();
35                 }
36                 return getUserObject().getClass().getSimpleName() + " - "
37                         + getUserObject().toString();
38             }
39         };
40         nodes.peek().add(node);
41         nodes.push(node);
42     }
43
44     @Override JavaDoc
45     public void enter(CtElement element) {
46         createNode(element);
47         super.enter(element);
48     }
49
50     @Override JavaDoc
51     public void enterReference(CtReference e) {
52         createNode(e);
53         super.enterReference(e);
54     }
55
56     @Override JavaDoc
57     public void exitReference(CtReference e) {
58         nodes.pop();
59         super.exitReference(e);
60     }
61
62     @Override JavaDoc
63     public void exit(CtElement element) {
64         nodes.pop();
65         super.exit(element);
66     }
67
68     public DefaultMutableTreeNode JavaDoc getRoot() {
69         return root;
70     }
71
72 }
73
Popular Tags