01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.support.gui;
19:
20: import java.util.Stack;
21:
22: import javax.swing.tree.DefaultMutableTreeNode;
23:
24: import spoon.reflect.declaration.CtElement;
25: import spoon.reflect.declaration.CtNamedElement;
26: import spoon.reflect.reference.CtReference;
27: import spoon.reflect.visitor.CtScanner;
28:
29: public class SpoonTreeBuilder extends CtScanner {
30: Stack<DefaultMutableTreeNode> nodes;
31:
32: DefaultMutableTreeNode root;
33:
34: public SpoonTreeBuilder() {
35: super ();
36: root = new DefaultMutableTreeNode("Spoon Tree Root");
37: nodes = new Stack<DefaultMutableTreeNode>();
38: nodes.push(root);
39: }
40:
41: private void createNode(Object o) {
42: DefaultMutableTreeNode node = new DefaultMutableTreeNode(o) {
43: private static final long serialVersionUID = 1L;
44:
45: @Override
46: public String toString() {
47: if (getUserObject() instanceof CtNamedElement) {
48: return getUserObject().getClass().getSimpleName()
49: + " - "
50: + ((CtNamedElement) getUserObject())
51: .getSimpleName();
52: }
53: return getUserObject().getClass().getSimpleName()
54: + " - " + getUserObject().toString();
55: }
56: };
57: nodes.peek().add(node);
58: nodes.push(node);
59: }
60:
61: @Override
62: public void enter(CtElement element) {
63: createNode(element);
64: super .enter(element);
65: }
66:
67: @Override
68: public void enterReference(CtReference e) {
69: createNode(e);
70: super .enterReference(e);
71: }
72:
73: @Override
74: public void exitReference(CtReference e) {
75: nodes.pop();
76: super .exitReference(e);
77: }
78:
79: @Override
80: public void exit(CtElement element) {
81: nodes.pop();
82: super .exit(element);
83: }
84:
85: public DefaultMutableTreeNode getRoot() {
86: return root;
87: }
88:
89: }
|