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.ast.SimpleNode;
    13
    14
    15  /**
    16   * Model for the AST Panel Tree component
    17   *
    18   * @author Boris Gruschko ( boris at gruschko.org )
    19   * @version $Id: ASTModel.java 4980 2007-01-27 18:11:14Z allancaplan $
    20   */
    21
    22  public class ASTModel implements TreeModel {
    23
    24      private SimpleNode 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(SimpleNode 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 ((SimpleNode) 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 ((SimpleNode) 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          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,java.lang.Object)
    78       */
    79      public void valueForPathChanged(TreePath path, Object newValue) {
    80          throw new UnsupportedOperationException();
    81      }
    82
    83      /**
    84       * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
    85       */
    86      public void addTreeModelListener(TreeModelListener l) {
    87          listeners.add(l);
    88      }
    89
    90
    91      /**
    92       * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
    93       */
    94      public void removeTreeModelListener(TreeModelListener l) {
    95          listeners.remove(l);
    96      }
    97
    98
    99      protected void fireTreeModelEvent(TreeModelEvent e) {
    100         for (TreeModelListener listener : listeners) {
    101             listener.treeNodesChanged(e);
    102         }
    103     }
    104
    105 }
    106