001: package net.sourceforge.pmd.util.viewer.model;
002:
003: import net.sourceforge.pmd.ast.SimpleNode;
004:
005: import javax.swing.tree.TreeNode;
006: import java.util.ArrayList;
007: import java.util.Collections;
008: import java.util.Enumeration;
009: import java.util.List;
010:
011: /**
012: * provides the adapter for the tree model
013: *
014: * @author Boris Gruschko ( boris at gruschko.org )
015: * @version $Id: SimpleNodeTreeNodeAdapter.java 4991 2007-01-28 00:59:32Z xlv $
016: */
017:
018: public class SimpleNodeTreeNodeAdapter implements TreeNode {
019:
020: private SimpleNode node;
021: private List<TreeNode> children;
022: private SimpleNodeTreeNodeAdapter parent;
023:
024: /**
025: * constructs the node
026: *
027: * @param node underlying AST's node
028: */
029: public SimpleNodeTreeNodeAdapter(SimpleNodeTreeNodeAdapter parent,
030: SimpleNode node) {
031: this .parent = parent;
032: this .node = node;
033: }
034:
035: /**
036: * retrieves the underlying node
037: *
038: * @return AST node
039: */
040: public SimpleNode getSimpleNode() {
041: return node;
042: }
043:
044: /**
045: * @see javax.swing.tree.TreeNode#getChildAt(int)
046: */
047: public TreeNode getChildAt(int childIndex) {
048: checkChildren();
049: return children.get(childIndex);
050: }
051:
052: /**
053: * @see javax.swing.tree.TreeNode#getChildCount()
054: */
055: public int getChildCount() {
056: checkChildren();
057: return children.size();
058: }
059:
060: /**
061: * @see javax.swing.tree.TreeNode#getParent()
062: */
063: public TreeNode getParent() {
064: return parent;
065: }
066:
067: /**
068: * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
069: */
070: public int getIndex(TreeNode node) {
071: checkChildren();
072: return children.indexOf(node);
073: }
074:
075: /**
076: * @see javax.swing.tree.TreeNode#getAllowsChildren()
077: */
078: public boolean getAllowsChildren() {
079: return true;
080: }
081:
082: /**
083: * @see javax.swing.tree.TreeNode#isLeaf()
084: */
085:
086: public boolean isLeaf() {
087: checkChildren();
088: return children.isEmpty();
089: }
090:
091: /**
092: * @see javax.swing.tree.TreeNode#children()
093: */
094:
095: public Enumeration<TreeNode> children() {
096: return Collections.enumeration(children);
097: }
098:
099: /**
100: * checks the children and creates them if neccessary
101: */
102: private void checkChildren() {
103: if (children == null) {
104: children = new ArrayList<TreeNode>(node.jjtGetNumChildren());
105: for (int i = 0; i < node.jjtGetNumChildren(); i++) {
106: children.add(new SimpleNodeTreeNodeAdapter(this ,
107: (SimpleNode) node.jjtGetChild(i)));
108: }
109: }
110: }
111:
112: /**
113: * @see java.lang.Object#toString()
114: */
115: public String toString() {
116: return node.toString();
117: }
118: }
|