KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > swing > treetable > AbstractTreeTableModel


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.swing.treetable;
19
20 /*
21  * @(#)AbstractTreeTableModel.java 1.2 98/10/27
22  *
23  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
24  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
25  * All rights reserved.
26  *
27  * This software is the confidential and proprietary information
28  * of Sun Microsystems, Inc. ("Confidential Information"). You
29  * shall not disclose such Confidential Information and shall use
30  * it only in accordance with the terms of the license agreement
31  * you entered into with Sun.
32  */

33
34 import javax.swing.event.EventListenerList JavaDoc;
35 import javax.swing.event.TreeModelEvent JavaDoc;
36 import javax.swing.event.TreeModelListener JavaDoc;
37 import javax.swing.tree.TreePath JavaDoc;
38
39 /**
40  * @version 1.2 10/27/98
41  * An abstract implementation of the TreeTableModel interface, handling the list
42  * of listeners.
43  * @author Philip Milne
44  */

45
46 public abstract class AbstractTreeTableModel implements TreeTableModel {
47     protected Object JavaDoc root;
48     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
49
50     public AbstractTreeTableModel(Object JavaDoc root) {
51         this.root = root;
52     }
53
54     //
55
// Default implmentations for methods in the TreeModel interface.
56
//
57

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

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

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

145     protected void fireTreeNodesRemoved(Object JavaDoc source, Object JavaDoc[] path,
146                                         int[] childIndices,
147                                         Object JavaDoc[] children) {
148         // Guaranteed to return a non-null array
149
Object JavaDoc[] listeners = listenerList.getListenerList();
150         TreeModelEvent JavaDoc e = null;
151         // Process the listeners last to first, notifying
152
// those that are interested in this event
153
for (int i = listeners.length-2; i>=0; i-=2) {
154             if (listeners[i]==TreeModelListener JavaDoc.class) {
155                 // Lazily create the event:
156
if (e == null)
157                     e = new TreeModelEvent JavaDoc(source, path,
158                                            childIndices, children);
159                 ((TreeModelListener JavaDoc)listeners[i+1]).treeNodesRemoved(e);
160             }
161         }
162     }
163
164     /*
165      * Notify all listeners that have registered interest for
166      * notification on this event type. The event instance
167      * is lazily created using the parameters passed into
168      * the fire method.
169      * @see EventListenerList
170      */

171     protected void fireTreeStructureChanged(Object JavaDoc source, Object JavaDoc[] path,
172                                         int[] childIndices,
173                                         Object JavaDoc[] children) {
174         // Guaranteed to return a non-null array
175
Object JavaDoc[] listeners = listenerList.getListenerList();
176         TreeModelEvent JavaDoc e = null;
177         // Process the listeners last to first, notifying
178
// those that are interested in this event
179
for (int i = listeners.length-2; i>=0; i-=2) {
180             if (listeners[i]==TreeModelListener JavaDoc.class) {
181                 // Lazily create the event:
182
if (e == null)
183                     e = new TreeModelEvent JavaDoc(source, path,
184                                            childIndices, children);
185                 ((TreeModelListener JavaDoc)listeners[i+1]).treeStructureChanged(e);
186             }
187         }
188     }
189
190     //
191
// Default impelmentations for methods in the TreeTableModel interface.
192
//
193

194     public Class JavaDoc getColumnClass(int column) { return Object JavaDoc.class; }
195
196    /** By default, make the column with the Tree in it the only editable one.
197     * Making this column editable causes the JTable to forward mouse
198     * and keyboard events in the Tree column to the underlying JTree.
199     */

200     public boolean isCellEditable(Object JavaDoc node, int column) {
201          return getColumnClass(column) == TreeTableModel.class;
202     }
203
204     public void setValueAt(Object JavaDoc aValue, Object JavaDoc node, int column) {}
205
206
207     // Left to be implemented in the subclass:
208

209     /*
210      * public Object getChild(Object parent, int index)
211      * public int getChildCount(Object parent)
212      * public int getColumnCount()
213      * public String getColumnName(Object node, int column)
214      * public Object getValueAt(Object node, int column)
215      */

216 }
217
Popular Tags