View Javadoc

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