KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mc4j > console > swing > treetable > AbstractTreeTableModel


1 package org.mc4j.console.swing.treetable;
2
3 /*
4  * @(#)AbstractTreeTableModel.java 1.2 98/10/27
5  *
6  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
7  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
8  * All rights reserved.
9  *
10  * This software is the confidential and proprietary information
11  * of Sun Microsystems, Inc. ("Confidential Information"). You
12  * shall not disclose such Confidential Information and shall use
13  * it only in accordance with the terms of the license agreement
14  * you entered into with Sun.
15  */

16
17 import javax.swing.event.EventListenerList JavaDoc;
18 import javax.swing.event.TreeModelEvent JavaDoc;
19 import javax.swing.event.TreeModelListener JavaDoc;
20 import javax.swing.tree.TreePath JavaDoc;
21  
22 /**
23  * @version 1.2 10/27/98
24  * An abstract implementation of the TreeTableModel interface, handling the list
25  * of listeners.
26  * @author Philip Milne
27  */

28
29 public abstract class AbstractTreeTableModel implements TreeTableModel {
30     protected Object JavaDoc root;
31     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
32   
33     public AbstractTreeTableModel(Object JavaDoc root) {
34         this.root = root;
35     }
36
37     //
38
// Default implmentations for methods in the TreeModel interface.
39
//
40

41     public Object JavaDoc getRoot() {
42         return root;
43     }
44
45     public boolean isLeaf(Object JavaDoc node) {
46         return getChildCount(node) == 0;
47     }
48
49     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {}
50
51     // This is not called in the JTree's default mode: use a naive implementation.
52
public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
53         for (int i = 0; i < getChildCount(parent); i++) {
54         if (getChild(parent, i).equals(child)) {
55             return i;
56         }
57         }
58     return -1;
59     }
60
61     public void addTreeModelListener(TreeModelListener JavaDoc l) {
62         listenerList.add(TreeModelListener JavaDoc.class, l);
63     }
64
65     public void removeTreeModelListener(TreeModelListener JavaDoc l) {
66         listenerList.remove(TreeModelListener JavaDoc.class, l);
67     }
68
69     /*
70      * Notify all listeners that have registered interest for
71      * notification on this event type. The event instance
72      * is lazily created using the parameters passed into
73      * the fire method.
74      * @see EventListenerList
75      */

76     protected void fireTreeNodesChanged(Object JavaDoc source, Object JavaDoc[] path,
77                                         int[] childIndices,
78                                         Object JavaDoc[] children) {
79         // Guaranteed to return a non-null array
80
Object JavaDoc[] listeners = listenerList.getListenerList();
81         TreeModelEvent JavaDoc e = null;
82         // Process the listeners last to first, notifying
83
// those that are interested in this event
84
for (int i = listeners.length-2; i>=0; i-=2) {
85             if (listeners[i]==TreeModelListener JavaDoc.class) {
86                 // Lazily create the event:
87
if (e == null)
88                     e = new TreeModelEvent JavaDoc(source, path,
89                                            childIndices, children);
90                 ((TreeModelListener JavaDoc)listeners[i+1]).treeNodesChanged(e);
91             }
92         }
93     }
94
95     /*
96      * Notify all listeners that have registered interest for
97      * notification on this event type. The event instance
98      * is lazily created using the parameters passed into
99      * the fire method.
100      * @see EventListenerList
101      */

102     protected void fireTreeNodesInserted(Object JavaDoc source, Object JavaDoc[] path,
103                                         int[] childIndices,
104                                         Object JavaDoc[] children) {
105         // Guaranteed to return a non-null array
106
Object JavaDoc[] listeners = listenerList.getListenerList();
107         TreeModelEvent JavaDoc e = null;
108         // Process the listeners last to first, notifying
109
// those that are interested in this event
110
for (int i = listeners.length-2; i>=0; i-=2) {
111             if (listeners[i]==TreeModelListener JavaDoc.class) {
112                 // Lazily create the event:
113
if (e == null)
114                     e = new TreeModelEvent JavaDoc(source, path,
115                                            childIndices, children);
116                 ((TreeModelListener JavaDoc)listeners[i+1]).treeNodesInserted(e);
117             }
118         }
119     }
120
121     /*
122      * Notify all listeners that have registered interest for
123      * notification on this event type. The event instance
124      * is lazily created using the parameters passed into
125      * the fire method.
126      * @see EventListenerList
127      */

128     protected void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path,
129                                         int[] childIndices,
130                                         Object JavaDoc[] children) {
131         // Guaranteed to return a non-null array
132
Object JavaDoc[] listeners = listenerList.getListenerList();
133         TreeModelEvent JavaDoc e = null;
134         // Process the listeners last to first, notifying
135
// those that are interested in this event
136
for (int i = listeners.length-2; i>=0; i-=2) {
137             if (listeners[i]==TreeModelListener JavaDoc.class) {
138                 // Lazily create the event:
139
if (e == null)
140                     e = new TreeModelEvent JavaDoc(source, path,
141                                            childIndices, children);
142                 ((TreeModelListener JavaDoc)listeners[i+1]).treeNodesRemoved(e);
143             }
144         }
145     }
146
147     /*
148      * Notify all listeners that have registered interest for
149      * notification on this event type. The event instance
150      * is lazily created using the parameters passed into
151      * the fire method.
152      * @see EventListenerList
153      */

154     protected void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path,
155                                         int[] childIndices,
156                                         Object JavaDoc[] children) {
157         // Guaranteed to return a non-null array
158
Object JavaDoc[] listeners = listenerList.getListenerList();
159         TreeModelEvent JavaDoc e = null;
160         // Process the listeners last to first, notifying
161
// those that are interested in this event
162
for (int i = listeners.length-2; i>=0; i-=2) {
163             if (listeners[i]==TreeModelListener JavaDoc.class) {
164                 // Lazily create the event:
165
if (e == null)
166                     e = new TreeModelEvent JavaDoc(source, path,
167                                            childIndices, children);
168                 ((TreeModelListener JavaDoc)listeners[i+1]).treeStructureChanged(e);
169             }
170         }
171     }
172
173     //
174
// Default impelmentations for methods in the TreeTableModel interface.
175
//
176

177     public Class JavaDoc getColumnClass(int column) { return Object JavaDoc.class; }
178
179    /** By default, make the column with the Tree in it the only editable one.
180     * Making this column editable causes the JTable to forward mouse
181     * and keyboard events in the Tree column to the underlying JTree.
182     */

183     public boolean isCellEditable(Object JavaDoc node, int column) {
184          return getColumnClass(column) == TreeTableModel.class;
185     }
186
187     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column) {}
188
189
190     // Left to be implemented in the subclass:
191

192     /*
193      * public Object getChild(Object parent, int index)
194      * public int getChildCount(Object parent)
195      * public int getColumnCount()
196      * public String getColumnName(Object node, int column)
197      * public Object getValueAt(Object node, int column)
198      */

199 }
200
Popular Tags