View Javadoc
1   package net.logAnalyzer.utils.gui.treetable;
2   /*
3    * %W% %E%
4    *
5    * Copyright 1997, 1998 Sun Microsystems, Inc. All Rights Reserved.
6    * 
7    * Redistribution and use in source and binary forms, with or
8    * without modification, are permitted provided that the following
9    * conditions are met:
10   * 
11   * - Redistributions of source code must retain the above copyright
12   *   notice, this list of conditions and the following disclaimer. 
13   *   
14   * - Redistribution in binary form must reproduce the above
15   *   copyright notice, this list of conditions and the following
16   *   disclaimer in the documentation and/or other materials
17   *   provided with the distribution. 
18   *   
19   * Neither the name of Sun Microsystems, Inc. or the names of
20   * contributors may be used to endorse or promote products derived
21   * from this software without specific prior written permission.  
22   * 
23   * This software is provided "AS IS," without a warranty of any
24   * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
25   * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
26   * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
27   * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
28   * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
29   * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
30   * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 
31   * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,   
32   * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER  
33   * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 
34   * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 
35   * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
36   * 
37   * You acknowledge that this software is not designed, licensed or
38   * intended for use in the design, construction, operation or
39   * maintenance of any nuclear facility.
40   */
41  
42  import javax.swing.tree.*;
43  import javax.swing.event.*;
44   
45  /***
46   * An abstract implementation of the TreeTableModel interface, handling 
47   * the list of listeners. 
48   *
49   * @version %I% %G%
50   *
51   * @author Philip Milne
52   */
53  
54  public abstract class AbstractTreeTableModel implements TreeTableModel {
55      protected Object root;     
56      protected EventListenerList listenerList = new EventListenerList();
57    
58      public AbstractTreeTableModel(Object root) {
59          this.root = root; 
60      }
61  
62      //
63      // Default implmentations for methods in the TreeModel interface. 
64      //
65  
66      public Object getRoot() {
67          return root;
68      }
69  
70      public boolean isLeaf(Object node) {
71          return getChildCount(node) == 0; 
72      }
73  
74      public void valueForPathChanged(TreePath path, Object newValue) {}
75  
76      // This is not called in the JTree's default mode: use a naive implementation. 
77      public int getIndexOfChild(Object parent, Object child) {
78          for (int i = 0; i < getChildCount(parent); i++) {
79  	    if (getChild(parent, i).equals(child)) { 
80  	        return i; 
81  	    }
82          }
83  	return -1; 
84      }
85  
86      public void addTreeModelListener(TreeModelListener l) {
87          listenerList.add(TreeModelListener.class, l);
88      }
89  
90      public void removeTreeModelListener(TreeModelListener l) {
91          listenerList.remove(TreeModelListener.class, l);
92      }
93  
94      /*
95       * Notify all listeners that have registered interest for
96       * notification on this event type.  The event instance 
97       * is lazily created using the parameters passed into 
98       * the fire method.
99       * @see EventListenerList
100      */
101     protected void fireTreeNodesChanged(Object source, Object[] path, 
102                                         int[] childIndices, 
103                                         Object[] children) {
104         // Guaranteed to return a non-null array
105         Object[] listeners = listenerList.getListenerList();
106         TreeModelEvent e = null;
107         // Process the listeners last to first, notifying
108         // those that are interested in this event
109         for (int i = listeners.length-2; i>=0; i-=2) {
110             if (listeners[i]==TreeModelListener.class) {
111                 // Lazily create the event:
112                 if (e == null)
113                     e = new TreeModelEvent(source, path, 
114                                            childIndices, children);
115                 ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
116             }          
117         }
118     }
119 
120     /*
121      * Notify all listeners that have registered interest for
122      * notification on this event type.  The event instance 
123      * is lazily created using the parameters passed into 
124      * the fire method.
125      * @see EventListenerList
126      */
127     protected void fireTreeNodesInserted(Object source, Object[] path, 
128                                         int[] childIndices, 
129                                         Object[] children) {
130         // Guaranteed to return a non-null array
131         Object[] listeners = listenerList.getListenerList();
132         TreeModelEvent e = null;
133         // Process the listeners last to first, notifying
134         // those that are interested in this event
135         for (int i = listeners.length-2; i>=0; i-=2) {
136             if (listeners[i]==TreeModelListener.class) {
137                 // Lazily create the event:
138                 if (e == null)
139                     e = new TreeModelEvent(source, path, 
140                                            childIndices, children);
141                 ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
142             }          
143         }
144     }
145 
146     /*
147      * Notify all listeners that have registered interest for
148      * notification on this event type.  The event instance 
149      * is lazily created using the parameters passed into 
150      * the fire method.
151      * @see EventListenerList
152      */
153     protected void fireTreeNodesRemoved(Object source, Object[] path, 
154                                         int[] childIndices, 
155                                         Object[] children) {
156         // Guaranteed to return a non-null array
157         Object[] listeners = listenerList.getListenerList();
158         TreeModelEvent e = null;
159         // Process the listeners last to first, notifying
160         // those that are interested in this event
161         for (int i = listeners.length-2; i>=0; i-=2) {
162             if (listeners[i]==TreeModelListener.class) {
163                 // Lazily create the event:
164                 if (e == null)
165                     e = new TreeModelEvent(source, path, 
166                                            childIndices, children);
167                 ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
168             }          
169         }
170     }
171 
172     /*
173      * Notify all listeners that have registered interest for
174      * notification on this event type.  The event instance 
175      * is lazily created using the parameters passed into 
176      * the fire method.
177      * @see EventListenerList
178      */
179     protected void fireTreeStructureChanged(Object source, Object[] path, 
180                                         int[] childIndices, 
181                                         Object[] children) {
182         // Guaranteed to return a non-null array
183         Object[] listeners = listenerList.getListenerList();
184         TreeModelEvent e = null;
185         // Process the listeners last to first, notifying
186         // those that are interested in this event
187         for (int i = listeners.length-2; i>=0; i-=2) {
188             if (listeners[i]==TreeModelListener.class) {
189                 // Lazily create the event:
190                 if (e == null)
191                     e = new TreeModelEvent(source, path, 
192                                            childIndices, children);
193                 ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
194             }          
195         }
196     }
197 
198     //
199     // Default impelmentations for methods in the TreeTableModel interface. 
200     //
201 
202     public Class getColumnClass(int column) { return Object.class; }
203 
204    /*** By default, make the column with the Tree in it the only editable one. 
205     *  Making this column editable causes the JTable to forward mouse 
206     *  and keyboard events in the Tree column to the underlying JTree. 
207     */ 
208     public boolean isCellEditable(Object node, int column) { 
209          return getColumnClass(column) == TreeTableModel.class; 
210     }
211 
212     public void setValueAt(Object aValue, Object node, int column) {}
213 
214 
215     // Left to be implemented in the subclass:
216 
217     /* 
218      *   public Object getChild(Object parent, int index)
219      *   public int getChildCount(Object parent) 
220      *   public int getColumnCount() 
221      *   public String getColumnName(Object node, int column)  
222      *   public Object getValueAt(Object node, int column) 
223      */
224 
225 }
226