1 package org.catacomb.druid.swing;
2
3 import org.catacomb.interlish.structure.Tree;
4 import org.catacomb.interlish.structure.TreeNode;
5 import org.catacomb.report.E;
6
7 import java.util.ArrayList;
8
9 import javax.swing.event.TreeModelEvent;
10 import javax.swing.event.TreeModelListener;
11 import javax.swing.tree.TreeModel;
12 import javax.swing.tree.TreePath;
13
14
15
16 public class DTreeModel implements TreeModel {
17
18 Tree tree;
19
20
21 ArrayList<TreeModelListener> treeModelListeners;
22
23
24 public DTreeModel(Tree t) {
25 tree = t;
26 treeModelListeners = new ArrayList<TreeModelListener>();
27 }
28
29
30
31
32 public Object getRoot() {
33 return tree.getRoot();
34 }
35
36 public Tree getTree() {
37 return tree;
38 }
39
40 public Object getChild(Object parent, int index) {
41 return ((TreeNode)parent).getChild(index);
42 }
43
44
45 public int getChildCount(Object parent) {
46 return ((TreeNode)parent).getChildCount();
47 }
48
49 public int getIndexOfChild(Object parent, Object child) {
50 return ((TreeNode)parent).getIndexOfChild(child);
51 }
52
53
54 public boolean isLeaf(Object node) {
55
56 return (node instanceof TreeNode) && ((TreeNode)node).isLeaf();
57 }
58
59
60
61
62 public void valueForPathChanged(TreePath path, Object newValue) {
63 System.out.println("val for path changed " + path);
64 }
65
66
67
68
69 protected void fireTreeStructureChanged(Object oldRoot) {
70
71 Object[] args = {oldRoot};
72 TreeModelEvent e = new TreeModelEvent(this, args);
73
74 for (TreeModelListener tml : treeModelListeners) {
75 tml.treeStructureChanged(e);
76 }
77 }
78
79
80 public void addTreeModelListener(TreeModelListener l) {
81 treeModelListeners.add(l);
82 }
83
84 public void removeTreeModelListener(TreeModelListener l) {
85 treeModelListeners.remove(l);
86 }
87
88
89
90
91 public void treeModified() {
92 E.info("firing a tree modified change....");
93 fireTreeStructureChanged(tree.getRoot());
94 }
95
96
97
98 }