1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.tree;
21
22 import javax.swing.event.EventListenerList;
23 import javax.swing.event.TreeModelEvent;
24 import javax.swing.event.TreeModelListener;
25 import javax.swing.tree.TreeModel;
26 import javax.swing.tree.TreePath;
27
28 import org.apache.log4j.Logger;
29
30 public abstract class AbstractTreeModel implements TreeModel
31 {
32
33
34
35 protected String root;
36 protected transient EventListenerList listenerList
37 = new EventListenerList();
38
39 private static Logger logger = Logger.getLogger(AbstractTreeModel.class);
40
41
42
43 public AbstractTreeModel(String root)
44 {
45 this.root = root;
46 }
47
48
49
50 public abstract boolean isLeaf(Object node);
51
52 public abstract int getChildCount(Object node);
53
54 public abstract Object getChild(Object parent, int index);
55
56 public abstract int getIndexOfChild(Object parent, Object child);
57
58 public Object getRoot()
59 {
60 return root;
61 }
62
63 public void reload()
64 {
65 Object[] path = new Object[] { getRoot() };
66 fireTreeStructureChanged(new TreeModelEvent(this, path));
67 }
68
69 public void valueForPathChanged(TreePath path, Object value)
70 {
71 logger.debug("AbstractTreeModel: valueForPath.. " + path + ","
72 + value);
73 }
74
75 public void addTreeModelListener(TreeModelListener l)
76 {
77 listenerList.add(TreeModelListener.class, l);
78 }
79
80 public void removeTreeModelListener(TreeModelListener l)
81 {
82 listenerList.remove(TreeModelListener.class, l);
83 }
84
85 protected void fireTreeNodesInserted(TreeModelEvent e) {
86
87 Object[] listeners = listenerList.getListenerList();
88
89
90 for (int i = listeners.length-2; i >= 0; i -= 2) {
91 if (listeners[i] == TreeModelListener.class)
92 ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
93 }
94 }
95
96 protected void fireTreeNodesRemoved(TreeModelEvent e) {
97
98 Object[] listeners = listenerList.getListenerList();
99
100
101 for (int i = listeners.length-2; i >= 0; i -= 2) {
102 if (listeners[i] == TreeModelListener.class)
103 ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
104
105 }
106 }
107
108 protected void fireTreeStructureChanged(TreeModelEvent e) {
109
110 Object[] listeners = listenerList.getListenerList();
111
112
113 for (int i = listeners.length-2; i >= 0; i -= 2) {
114 if (listeners[i] == TreeModelListener.class)
115 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
116
117 }
118 }
119 }