KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > util > viewer > model > ASTModel


1 package net.sourceforge.pmd.util.viewer.model;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.swing.event.TreeModelEvent JavaDoc;
8 import javax.swing.event.TreeModelListener JavaDoc;
9 import javax.swing.tree.TreeModel JavaDoc;
10 import javax.swing.tree.TreePath JavaDoc;
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,v 1.11 2006/10/20 02:40:14 hooperbloob Exp $
20  */

21
22 public class ASTModel implements TreeModel JavaDoc {
23     
24     private SimpleNode root;
25     private List JavaDoc listeners = new ArrayList JavaDoc(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 JavaDoc getChild(Object JavaDoc 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 JavaDoc 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 JavaDoc parent, Object JavaDoc 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 JavaDoc node) {
66         return ((SimpleNode) node).jjtGetNumChildren() == 0;
67     }
68
69     /**
70      * @see javax.swing.tree.TreeModel#getRoot()
71      */

72     public Object JavaDoc 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 JavaDoc path, Object JavaDoc newValue) {
80         throw new UnsupportedOperationException JavaDoc();
81     }
82
83     /**
84      * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
85      */

86     public void addTreeModelListener(TreeModelListener JavaDoc 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 JavaDoc l) {
95         listeners.remove(l);
96     }
97
98
99     protected void fireTreeModelEvent(TreeModelEvent JavaDoc e) {
100         for (int i = 0; i < listeners.size(); i++) {
101             ((TreeModelListener JavaDoc) listeners.get(i)).treeNodesChanged(e);
102         }
103     }
104
105 }
106
107
Popular Tags