1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /***
21 * Original copyright notice below. Class was slightly adopted for XNap.
22 */
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.xnap.gui.table;
37
38 import javax.swing.event.EventListenerList;
39 import javax.swing.event.TreeModelEvent;
40 import javax.swing.event.TreeModelListener;
41 import javax.swing.tree.TreePath;
42
43 /***
44 * @version 1.2 10/27/98 An abstract implementation of the TreeTableModel
45 * interface, handling the list of listeners.
46 * @author Philip Milne
47 */
48 public abstract class AbstractTreeTableModel implements TreeTableModel {
49 protected Object root;
50 protected EventListenerList listenerList = new EventListenerList();
51
52 public AbstractTreeTableModel(Object root) {
53 this.root = root;
54 }
55
56 public AbstractTreeTableModel() {
57 this.root = this;
58 }
59
60
61
62
63
64 public Object getRoot() {
65 return root;
66 }
67
68 public boolean isLeaf(Object node) {
69 return getChildCount(node) == 0;
70 }
71
72 public void valueForPathChanged(TreePath path, Object newValue) {}
73
74
75 public int getIndexOfChild(Object parent, Object child) {
76 for (int i = 0; i < getChildCount(parent); i++) {
77 if (getChild(parent, i).equals(child)) {
78 return i;
79 }
80 }
81 return -1;
82 }
83
84 /***
85 * Adds a listener to the model.
86 */
87 public void addTreeModelListener(TreeModelListener l) {
88 listenerList.add(TreeModelListener.class, l);
89 }
90
91 /***
92 * Removes a listener from the model.
93 */
94 public void removeTreeModelListener(TreeModelListener l) {
95 listenerList.remove(TreeModelListener.class, l);
96 }
97
98 /***
99 * Notify all listeners that have registered interest for
100 * notification on this event type. The event instance
101 * is lazily created using the parameters passed into
102 * the fire method.
103 * @see EventListenerList
104 */
105 protected void fireTreeNodesChanged(Object source, Object[] path,
106 int[] childIndices,
107 Object[] children) {
108
109 Object[] listeners = listenerList.getListenerList();
110 TreeModelEvent e = null;
111
112
113 for (int i = listeners.length-2; i>=0; i-=2) {
114 if (listeners[i]==TreeModelListener.class) {
115
116 if (e == null)
117 e = new TreeModelEvent(source, path,
118 childIndices, children);
119 ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
120 }
121 }
122 }
123
124 /***
125 * Notify all listeners that have registered interest for
126 * notification on this event type. The event instance
127 * is lazily created using the parameters passed into
128 * the fire method.
129 * @see EventListenerList
130 */
131 protected void fireTreeNodesInserted(Object source, Object[] path,
132 int[] childIndices,
133 Object[] children) {
134
135 Object[] listeners = listenerList.getListenerList();
136 TreeModelEvent e = null;
137
138
139 for (int i = listeners.length-2; i>=0; i-=2) {
140 if (listeners[i]==TreeModelListener.class) {
141
142 if (e == null)
143 e = new TreeModelEvent(source, path,
144 childIndices, children);
145 ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
146 }
147 }
148 }
149
150 /***
151 * Notify all listeners that have registered interest for
152 * notification on this event type. The event instance
153 * is lazily created using the parameters passed into
154 * the fire method.
155 * @see EventListenerList
156 */
157 protected void fireTreeNodesRemoved(Object source, Object[] path,
158 int[] childIndices,
159 Object[] children) {
160
161 Object[] listeners = listenerList.getListenerList();
162 TreeModelEvent e = null;
163
164
165 for (int i = listeners.length-2; i>=0; i-=2) {
166 if (listeners[i]==TreeModelListener.class) {
167
168 if (e == null)
169 e = new TreeModelEvent(source, path,
170 childIndices, children);
171 ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
172 }
173 }
174 }
175
176 /***
177 * Notify all listeners that have registered interest for
178 * notification on this event type. The event instance
179 * is lazily created using the parameters passed into
180 * the fire method.
181 * @see EventListenerList
182 */
183 protected void fireTreeStructureChanged(Object source, Object[] path,
184 int[] childIndices,
185 Object[] children) {
186
187 Object[] listeners = listenerList.getListenerList();
188 TreeModelEvent e = null;
189
190
191 for (int i = listeners.length-2; i>=0; i-=2) {
192 if (listeners[i]==TreeModelListener.class) {
193
194 if (e == null)
195 e = new TreeModelEvent(source, path,
196 childIndices, children);
197 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
198 }
199 }
200 }
201
202
203
204
205
206 public Class getColumnClass(int column) { return Object.class; }
207
208 /***
209 * By default, make the column with the Tree in it the only editable one.
210 * Making this column editable causes the JTable to forward mouse
211 * and keyboard events in the Tree column to the underlying JTree.
212 */
213 public boolean isCellEditable(Object node, int column) {
214 return getColumnClass(column) == TreeTableModel.class;
215 }
216
217 public void setValueAt(Object aValue, Object node, int column) {}
218
219
220
221
222
223
224
225
226
227
228
229 }