01: package logconfig;
02:
03: import java.util.*;
04:
05: import javax.swing.event.*;
06: import javax.swing.tree.*;
07:
08: import org.dom4j.Node;
09:
10: public class DomModel implements TreeModel {
11:
12: private static final String xpath = "(./* | ./@*)";
13:
14: private List<TreeModelListener> listeners;
15: private Node root;
16:
17: public DomModel(Node root) {
18: this .root = root;
19: listeners = new ArrayList<TreeModelListener>();
20: }
21:
22: public Object getRoot() {
23: return root;
24: }
25:
26: public Object getChild(Object parent, int index) {
27: return ((Node) parent).selectSingleNode(xpath + "["
28: + (index + 1) + "]");
29: }
30:
31: public int getChildCount(Object parent) {
32: return ((Node) parent).selectNodes(xpath).size();
33: }
34:
35: public int getIndexOfChild(Object parent, Object child) {
36: return ((Node) parent).selectNodes(xpath).indexOf((Node) child);
37: }
38:
39: public boolean isLeaf(Object node) {
40: if (getChildCount(node) > 0)
41: return false;
42: return true;
43: }
44:
45: public void valueForPathChanged(TreePath path, Object newValue) {
46: // do nothing!
47: }
48:
49: public void addTreeModelListener(TreeModelListener tml) {
50: if (tml != null && !listeners.contains(tml)) {
51: listeners.add(tml);
52: }
53: }
54:
55: public void removeTreeModelListener(TreeModelListener tml) {
56: if (tml != null) {
57: listeners.remove(tml);
58: }
59: }
60:
61: public void fireTreeNodesChanged(TreeModelEvent tme) {
62: Iterator i = listeners.iterator();
63: while (i.hasNext()) {
64: TreeModelListener tml = (TreeModelListener) i.next();
65: tml.treeNodesChanged(tme);
66: }
67: }
68:
69: public void fireTreeNodesInserted(TreeModelEvent tme) {
70: Iterator i = listeners.iterator();
71: while (i.hasNext()) {
72: TreeModelListener tml = (TreeModelListener) i.next();
73: tml.treeNodesInserted(tme);
74: }
75: }
76:
77: public void fireTreeNodesRemoved(TreeModelEvent tme) {
78: Iterator i = listeners.iterator();
79: while (i.hasNext()) {
80: TreeModelListener tml = (TreeModelListener) i.next();
81: tml.treeNodesRemoved(tme);
82: }
83: }
84:
85: public void fireTreeStructureChanged(TreeModelEvent tme) {
86: Iterator i = listeners.iterator();
87: while (i.hasNext()) {
88: TreeModelListener tml = (TreeModelListener) i.next();
89: tml.treeStructureChanged(tme);
90: }
91: }
92:
93: }
|