View Javadoc

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