View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  /***
21   * Original copyright notice below. Class was slightly adopted for XNap.
22   */
23  /*
24   * @(#)AbstractTreeTableModel.java	1.2 98/10/27
25   *
26   * Copyright 1997, 1998 by Sun Microsystems, Inc.,
27   * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
28   * All rights reserved.
29   *
30   * This software is the confidential and proprietary information
31   * of Sun Microsystems, Inc. ("Confidential Information").  You
32   * shall not disclose such Confidential Information and shall use
33   * it only in accordance with the terms of the license agreement
34   * you entered into with Sun.
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      // Default implmentations for methods in the TreeModel interface. 
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      // This is not called in the JTree's default mode: use a naive implementation. 
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         // Guaranteed to return a non-null array
109         Object[] listeners = listenerList.getListenerList();
110         TreeModelEvent e = null;
111         // Process the listeners last to first, notifying
112         // those that are interested in this event
113         for (int i = listeners.length-2; i>=0; i-=2) {
114             if (listeners[i]==TreeModelListener.class) {
115                 // Lazily create the event:
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         // Guaranteed to return a non-null array
135         Object[] listeners = listenerList.getListenerList();
136         TreeModelEvent e = null;
137         // Process the listeners last to first, notifying
138         // those that are interested in this event
139         for (int i = listeners.length-2; i>=0; i-=2) {
140             if (listeners[i]==TreeModelListener.class) {
141                 // Lazily create the event:
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         // Guaranteed to return a non-null array
161         Object[] listeners = listenerList.getListenerList();
162         TreeModelEvent e = null;
163         // Process the listeners last to first, notifying
164         // those that are interested in this event
165         for (int i = listeners.length-2; i>=0; i-=2) {
166             if (listeners[i]==TreeModelListener.class) {
167                 // Lazily create the event:
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         // Guaranteed to return a non-null array
187         Object[] listeners = listenerList.getListenerList();
188         TreeModelEvent e = null;
189         // Process the listeners last to first, notifying
190         // those that are interested in this event
191         for (int i = listeners.length-2; i>=0; i-=2) {
192             if (listeners[i]==TreeModelListener.class) {
193                 // Lazily create the event:
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     // Default impelmentations for methods in the TreeTableModel interface. 
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     // Left to be implemented in the subclass:
221 
222     /* 
223      *   public Object getChild(Object parent, int index)
224      *   public int getChildCount(Object parent) 
225      *   public int getColumnCount() 
226      *   public String getColumnName(Object node, int column)  
227      *   public Object getValueAt(Object node, int column) 
228      */
229 }