001: package net.sourceforge.pmd.util.viewer.model;
002:
003: import java.util.ArrayList;
004: import java.util.List;
005:
006: import javax.swing.event.TreeModelEvent;
007: import javax.swing.event.TreeModelListener;
008: import javax.swing.tree.TreeModel;
009: import javax.swing.tree.TreePath;
010:
011: import net.sourceforge.pmd.ast.SimpleNode;
012:
013: /**
014: * Model for the AST Panel Tree component
015: *
016: * @author Boris Gruschko ( boris at gruschko.org )
017: * @version $Id: ASTModel.java 4980 2007-01-27 18:11:14Z allancaplan $
018: */
019:
020: public class ASTModel implements TreeModel {
021:
022: private SimpleNode root;
023: private List<TreeModelListener> listeners = new ArrayList<TreeModelListener>(
024: 1);
025:
026: /**
027: * creates the tree model
028: *
029: * @param root tree's root
030: */
031: public ASTModel(SimpleNode root) {
032: this .root = root;
033: }
034:
035: /**
036: * @see javax.swing.tree.TreeModel
037: */
038: public Object getChild(Object parent, int index) {
039: return ((SimpleNode) parent).jjtGetChild(index);
040: }
041:
042: /**
043: * @see javax.swing.tree.TreeModel#getChildCount(java.lang.Object)
044: */
045: public int getChildCount(Object parent) {
046: return ((SimpleNode) parent).jjtGetNumChildren();
047: }
048:
049: /**
050: * @see javax.swing.tree.TreeModel#getIndexOfChild(java.lang.Object,java.lang.Object)
051: */
052: public int getIndexOfChild(Object parent, Object child) {
053: SimpleNode node = ((SimpleNode) parent);
054: for (int i = 0; i < node.jjtGetNumChildren(); i++)
055: if (node.jjtGetChild(i).equals(child)) {
056: return i;
057: }
058: return -1;
059: }
060:
061: /**
062: * @see javax.swing.tree.TreeModel#isLeaf(java.lang.Object)
063: */
064: public boolean isLeaf(Object node) {
065: return ((SimpleNode) node).jjtGetNumChildren() == 0;
066: }
067:
068: /**
069: * @see javax.swing.tree.TreeModel#getRoot()
070: */
071: public Object getRoot() {
072: return root;
073: }
074:
075: /**
076: * @see javax.swing.tree.TreeModel#valueForPathChanged(javax.swing.tree.TreePath,java.lang.Object)
077: */
078: public void valueForPathChanged(TreePath path, Object newValue) {
079: throw new UnsupportedOperationException();
080: }
081:
082: /**
083: * @see javax.swing.tree.TreeModel#addTreeModelListener(javax.swing.event.TreeModelListener)
084: */
085: public void addTreeModelListener(TreeModelListener l) {
086: listeners.add(l);
087: }
088:
089: /**
090: * @see javax.swing.tree.TreeModel#removeTreeModelListener(javax.swing.event.TreeModelListener)
091: */
092: public void removeTreeModelListener(TreeModelListener l) {
093: listeners.remove(l);
094: }
095:
096: protected void fireTreeModelEvent(TreeModelEvent e) {
097: for (TreeModelListener listener : listeners) {
098: listener.treeNodesChanged(e);
099: }
100: }
101:
102: }
|