View Javadoc

    1   package net.sourceforge.pmd.util.viewer.model;
    2
    3
    4   import java.util.ArrayList;
    5   import java.util.List;
    6
    7   import javax.swing.event.TreeModelEvent;
    8   import javax.swing.event.TreeModelListener;
    9   import javax.swing.tree.TreeModel;
    10  import javax.swing.tree.TreePath;
    11
    12  import net.sourceforge.pmd.lang.ast.Node;
    13
    14
    15  /**
    16   * Model for the AST Panel Tree component
    17   *
    18   * @author Boris Gruschko ( boris at gruschko.org )
    19   * @version $Id$
    20   */
    21
    22  public class ASTModel implements TreeModel {
    23
    24      private Node root;
    25      private List<TreeModelListener> listeners = new ArrayList<TreeModelListener>(1);
    26
    27      /**
    28       * creates the tree model
    29       *
    30       * @param root tree's root
    31       */
    32      public ASTModel(Node root) {
    33          this.root = root;
    34      }
    35
    36      /**
    37       * @see javax.swing.tree.TreeModel
    38       */
    39      public Object getChild(Object parent, int index) {
    40          return ((Node) parent).jjtGetChild(index);
    41      }
    42
    43      /**
    44       * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
    45       */
    46      public int getChildCount(Object parent) {
    47          return ((Node) parent).jjtGetNumChildren();
    48      }
    49
    50      /**
    51       * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object,java.lang.Object)
    52       */
    53      public int getIndexOfChild(Object parent, Object child) {
    54  	Node node = (Node) parent;
    55          for (int i = 0; i < node.jjtGetNumChildren(); i++) {
    56              if (node.jjtGetChild(i).equals(child)) {
    57                  return i;
    58              }
    59          }
    60          return -1;
    61      }
    62
    63      /**
    64       * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
    65       */
    66      public boolean isLeaf(Object node) {
    67          return ((Node) node).jjtGetNumChildren() == 0;
    68      }
    69
    70      /**
    71       * @see javax.swing.tree.TreeModel#getRoot()
    72       */
    73      public Object getRoot() {
    74          return root;
    75      }
    76
    77      /**
    78       * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath,java.lang.Object)
    79       */
    80      public void valueForPathChanged(TreePath path, Object newValue) {
    81          throw new UnsupportedOperationException();
    82      }
    83
    84      /**
    85       * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
    86       */
    87      public void addTreeModelListener(TreeModelListener l) {
    88          listeners.add(l);
    89      }
    90
    91
    92      /**
    93       * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
    94       */
    95      public void removeTreeModelListener(TreeModelListener l) {
    96          listeners.remove(l);
    97      }
    98
    99
    100     protected void fireTreeModelEvent(TreeModelEvent e) {
    101         for (TreeModelListener listener : listeners) {
    102             listener.treeNodesChanged(e);
    103         }
    104     }
    105
    106 }
    107