View Javadoc

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