1 package spoon.support.gui; 2 3 import java.util.Stack ; 4 5 import javax.swing.tree.DefaultMutableTreeNode ; 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 <DefaultMutableTreeNode > nodes; 14 15 DefaultMutableTreeNode root; 16 17 public SpoonTreeBuilder() { 18 super(); 19 root = new DefaultMutableTreeNode ("Spoon Tree Root"); 20 nodes = new Stack <DefaultMutableTreeNode >(); 21 nodes.push(root); 22 } 23 24 private void createNode(Object o) { 25 DefaultMutableTreeNode node = new DefaultMutableTreeNode (o) { 26 private static final long serialVersionUID = 1L; 27 28 @Override 29 public String 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 45 public void enter(CtElement element) { 46 createNode(element); 47 super.enter(element); 48 } 49 50 @Override 51 public void enterReference(CtReference e) { 52 createNode(e); 53 super.enterReference(e); 54 } 55 56 @Override 57 public void exitReference(CtReference e) { 58 nodes.pop(); 59 super.exitReference(e); 60 } 61 62 @Override 63 public void exit(CtElement element) { 64 nodes.pop(); 65 super.exit(element); 66 } 67 68 public DefaultMutableTreeNode getRoot() { 69 return root; 70 } 71 72 } 73 | Popular Tags |