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