View Javadoc

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