KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > treetable > AbstractTreeTableModel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * this file is derived from the "Creating TreeTable" article at
22  * http://java.sun.com/products/jfc/tsc/articles/treetable2/index.html
23  */

24 package org.netbeans.modules.tasklist.usertasks.treetable;
25
26 import javax.swing.event.EventListenerList JavaDoc;
27 import javax.swing.event.TreeModelEvent JavaDoc;
28 import javax.swing.event.TreeModelListener JavaDoc;
29 import javax.swing.tree.TreePath JavaDoc;
30
31 /**
32  * An abstract implementation of the TreeTableModel interface, handling the list
33  * of listeners.
34  *
35  * @author Philip Milne
36  */

37 public abstract class AbstractTreeTableModel implements TreeTableModel {
38     /** root node */
39     protected Object JavaDoc root;
40     
41     /** TreeModelListener[] */
42     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
43     
44     /**
45      * Creates a table model for the given root node.
46      *
47      * @param root root node
48      */

49     public AbstractTreeTableModel(Object JavaDoc root) {
50         this.root = root;
51     }
52     
53     /**
54      * Sets new root node
55      *
56      * @param root new root node
57      */

58     public void setRoot(Object JavaDoc root) {
59         if (this.root != root) {
60             this.root = root;
61             fireTreeStructureChanged(this, new Object JavaDoc[] {root}, null, null);
62         }
63     }
64     
65     //
66
// Default implmentations for methods in the TreeModel interface.
67
//
68

69     public Object JavaDoc getRoot() {
70         return root;
71     }
72     
73     public boolean isLeaf(Object JavaDoc node) {
74         return getChildCount(node) == 0;
75     }
76     
77     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {}
78     
79     // This is not called in the JTree's default mode: use a naive implementation.
80
public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
81         for (int i = 0; i < getChildCount(parent); i++) {
82             if (getChild(parent, i).equals(child)) {
83                 return i;
84             }
85         }
86         return -1;
87     }
88     
89     public void addTreeModelListener(TreeModelListener JavaDoc l) {
90         listenerList.add(TreeModelListener JavaDoc.class, l);
91     }
92     
93     public void removeTreeModelListener(TreeModelListener JavaDoc l) {
94         listenerList.remove(TreeModelListener JavaDoc.class, l);
95     }
96     
97     /*
98      * Notify all listeners that have registered interest for
99      * notification on this event type. The event instance
100      * is lazily created using the parameters passed into
101      * the fire method.
102      * @see EventListenerList
103      */

104     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path) {
105         // Guaranteed to return a non-null array
106
Object JavaDoc[] listeners = listenerList.getListenerList();
107         TreeModelEvent JavaDoc e = null;
108         TreePath JavaDoc tp = path == null ? null : new TreePath JavaDoc(path);
109         // Process the listeners last to first, notifying
110
// those that are interested in this event
111
for (int i = listeners.length-2; i>=0; i-=2) {
112             if (listeners[i]==TreeModelListener JavaDoc.class) {
113                 // Lazily create the event:
114
if (e == null)
115                     e = new TreeModelEvent JavaDoc(source, tp);
116                 ((TreeModelListener JavaDoc)listeners[i+1]).treeStructureChanged(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 fireTreeNodesChanged(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]).treeNodesChanged(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 fireTreeNodesInserted(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]).treeNodesInserted(e);
169             }
170         }
171     }
172     
173     /*
174      * Notify all listeners that have registered interest for
175      * notification on this event type. The event instance
176      * is lazily created using the parameters passed into
177      * the fire method.
178      * @see EventListenerList
179      */

180     protected void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path,
181     int[] childIndices,
182     Object JavaDoc[] children) {
183         // Guaranteed to return a non-null array
184
Object JavaDoc[] listeners = listenerList.getListenerList();
185         TreeModelEvent JavaDoc e = null;
186         // Process the listeners last to first, notifying
187
// those that are interested in this event
188
for (int i = listeners.length-2; i>=0; i-=2) {
189             if (listeners[i]==TreeModelListener JavaDoc.class) {
190                 // Lazily create the event:
191
if (e == null)
192                     e = new TreeModelEvent JavaDoc(source, path,
193                     childIndices, children);
194                 ((TreeModelListener JavaDoc)listeners[i+1]).treeNodesRemoved(e);
195             }
196         }
197     }
198     
199     /*
200      * Notify all listeners that have registered interest for
201      * notification on this event type. The event instance
202      * is lazily created using the parameters passed into
203      * the fire method.
204      * @see EventListenerList
205      */

206     public void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path,
207     int[] childIndices, Object JavaDoc[] children) {
208         // Guaranteed to return a non-null array
209
Object JavaDoc[] listeners = listenerList.getListenerList();
210         TreeModelEvent JavaDoc e = null;
211         // Process the listeners last to first, notifying
212
// those that are interested in this event
213
for (int i = listeners.length-2; i>=0; i-=2) {
214             if (listeners[i]==TreeModelListener JavaDoc.class) {
215                 // Lazily create the event:
216
if (e == null)
217                     e = new TreeModelEvent JavaDoc(source, path,
218                     childIndices, children);
219                 ((TreeModelListener JavaDoc)listeners[i+1]).treeStructureChanged(e);
220             }
221         }
222     }
223     
224     //
225
// Default implementations for methods in the TreeTableModel interface.
226
//
227

228     public Class JavaDoc getColumnClass(int column) { return Object JavaDoc.class; }
229     
230     /** By default, make the column with the Tree in it the only editable one.
231      * Making this column editable causes the JTable to forward mouse
232      * and keyboard events in the Tree column to the underlying JTree.
233      */

234     public boolean isCellEditable(Object JavaDoc node, int column) {
235         // CHANGEEDIT
236
// return getColumnClass(column) == TreeTableModel.class;
237
return false;
238     }
239     
240     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column) {}
241     
242     
243     // Left to be implemented in the subclass:
244

245     /*
246      * public Object getChild(Object parent, int index)
247      * public int getChildCount(Object parent)
248      * public int getColumnCount()
249      * public String getColumnName(Object node, int column)
250      * public Object getValueAt(Object node, int column)
251      */

252 }
253
Popular Tags