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 21 22 public class ASTModel implements TreeModel { 23 24 private SimpleNode root; 25 private List listeners = new ArrayList (1); 26 27 32 public ASTModel(SimpleNode root) { 33 this.root = root; 34 } 35 36 39 public Object getChild(Object parent, int index) { 40 return ((SimpleNode) parent).jjtGetChild(index); 41 } 42 43 46 public int getChildCount(Object parent) { 47 return ((SimpleNode) parent).jjtGetNumChildren(); 48 } 49 50 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 65 public boolean isLeaf(Object node) { 66 return ((SimpleNode) node).jjtGetNumChildren() == 0; 67 } 68 69 72 public Object getRoot() { 73 return root; 74 } 75 76 79 public void valueForPathChanged(TreePath path, Object newValue) { 80 throw new UnsupportedOperationException (); 81 } 82 83 86 public void addTreeModelListener(TreeModelListener l) { 87 listeners.add(l); 88 } 89 90 91 94 public void removeTreeModelListener(TreeModelListener l) { 95 listeners.remove(l); 96 } 97 98 99 protected void fireTreeModelEvent(TreeModelEvent e) { 100 for (int i = 0; i < listeners.size(); i++) { 101 ((TreeModelListener ) listeners.get(i)).treeNodesChanged(e); 102 } 103 } 104 105 } 106 107 | Popular Tags |