View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.util.viewer.model;
5   
6   
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.Enumeration;
10  import java.util.List;
11  
12  import javax.swing.tree.TreeNode;
13  
14  import net.sourceforge.pmd.lang.ast.Node;
15  
16  
17  /**
18   * provides the adapter for the tree model
19   *
20   * @author Boris Gruschko ( boris at gruschko.org )
21   * @version $Id$
22   */
23  
24  public class SimpleNodeTreeNodeAdapter implements TreeNode {
25  	
26      private Node node;
27      private List<TreeNode> children;
28      private SimpleNodeTreeNodeAdapter parent;
29  
30      /**
31       * constructs the node
32       *
33       * @param node underlying AST's node
34       */
35      public SimpleNodeTreeNodeAdapter(SimpleNodeTreeNodeAdapter parent, Node node) {
36          this.parent = parent;
37          this.node = node;
38      }
39  
40      /**
41       * retrieves the underlying node
42       *
43       * @return AST node
44       */
45      public Node getSimpleNode() {
46          return node;
47      }
48  
49  
50      /**
51       * @see javax.swing.tree.TreeNode#getChildAt(int)
52       */
53      public TreeNode getChildAt(int childIndex) {
54          checkChildren();
55          return children.get(childIndex);
56      }
57  
58  
59      /**
60       * @see javax.swing.tree.TreeNode#getChildCount()
61       */
62      public int getChildCount() {
63          checkChildren();
64          return children.size();
65      }
66  
67  
68      /**
69       * @see javax.swing.tree.TreeNode#getParent()
70       */
71      public TreeNode getParent() {
72          return parent;
73      }
74  
75      /**
76       * @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
77       */
78      public int getIndex(TreeNode node) {
79          checkChildren();
80          return children.indexOf(node);
81      }
82  
83  
84      /**
85       * @see javax.swing.tree.TreeNode#getAllowsChildren()
86       */
87      public boolean getAllowsChildren() {
88          return true;
89      }
90  
91  
92      /**
93       * @see javax.swing.tree.TreeNode#isLeaf()
94       */
95  
96      public boolean isLeaf() {
97          checkChildren();
98          return children.isEmpty();
99      }
100 
101 
102     /**
103      * @see javax.swing.tree.TreeNode#children()
104      */
105 
106     public Enumeration<TreeNode> children() {
107         return Collections.enumeration(children);
108     }
109 
110 
111     /**
112      * checks the children and creates them if neccessary
113      */
114     private void checkChildren() {
115         if (children == null) {
116             children = new ArrayList<TreeNode>(node.jjtGetNumChildren());
117             for (int i = 0; i < node.jjtGetNumChildren(); i++) {
118                 children.add(new SimpleNodeTreeNodeAdapter(this, node.jjtGetChild(i)));
119             }
120         }
121     }
122 
123     /**
124      * @see java.lang.Object#toString()
125      */
126     public String toString() {
127         return node.toString();
128     }
129 }
130