KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.logging.Level JavaDoc;
27 import javax.swing.JTree JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.TreeExpansionEvent JavaDoc;
30 import javax.swing.event.TreeExpansionListener JavaDoc;
31 import javax.swing.event.TreeModelEvent JavaDoc;
32 import javax.swing.event.TreeModelListener JavaDoc;
33 import javax.swing.table.AbstractTableModel JavaDoc;
34 import javax.swing.tree.TreePath JavaDoc;
35 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
36
37 /**
38  * This is a wrapper class takes a TreeTableModel and implements
39  * the table model interface. The implementation is trivial, with
40  * all of the event dispatching support provided by the superclass:
41  * the AbstractTableModel.
42  *
43  * @version 1.2 10/27/98
44  *
45  * @author Philip Milne
46  * @author Scott Violet
47  * @author tl
48  */

49 public class TreeTableModelAdapter extends AbstractTableModel JavaDoc {
50
51     private static final long serialVersionUID = 1;
52
53     JTree JavaDoc tree;
54     TreeTableModel treeTableModel;
55     private TreeExpansionListener JavaDoc tel;
56     private TreeModelListener JavaDoc tml;
57     
58     public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree JavaDoc tree) {
59         this.tree = tree;
60         this.treeTableModel = treeTableModel;
61         
62         tel = new TreeExpansionListener JavaDoc() {
63             // Don't use fireTableRowsInserted() here; the selection model
64
// would get updated twice.
65
public void treeExpanded(TreeExpansionEvent JavaDoc event) {
66                 fireTableDataChanged();
67                 /** this code seems better, but does not work
68                  * (e.g. for Expand All)
69                 JTree tree = (JTree) event.getSource();
70                 int row = tree.getRowForPath(event.getPath());
71                 int n = tree.getModel().getChildCount(
72                         event.getPath().getLastPathComponent());
73                 if (n != 0)
74                     fireTableRowsInserted(row + 1, row + n);
75                  */

76             }
77             public void treeCollapsed(TreeExpansionEvent JavaDoc event) {
78                 JTree JavaDoc tree = (JTree JavaDoc) event.getSource();
79                 int row = tree.getRowForPath(event.getPath());
80                 int n = tree.getModel().getChildCount(
81                         event.getPath().getLastPathComponent());
82                 if (n != 0)
83                     fireTableRowsDeleted(row + 1, row + n);
84             }
85         };
86         tree.addTreeExpansionListener(tel);
87         
88         
89         tml = new TreeModelListener JavaDoc() {
90             public void treeNodesChanged(TreeModelEvent JavaDoc e) {
91                 TreePath JavaDoc tp = e.getTreePath();
92                 if (TreeTableModelAdapter.this.tree.isExpanded(tp)) {
93                     int firstRow = Integer.MAX_VALUE;
94                     int lastRow = Integer.MIN_VALUE;
95                     int[] childIndices = e.getChildIndices();
96                     Object JavaDoc last = tp.getLastPathComponent();
97                     for (int i = 0; i < childIndices.length; i++) {
98                         TreePath JavaDoc childPath = tp.pathByAddingChild(
99                             TreeTableModelAdapter.this.treeTableModel.getChild(
100                             last, childIndices[i]));
101                         int row = TreeTableModelAdapter.this.tree.getRowForPath(
102                                 childPath);
103                         if (row < firstRow)
104                             firstRow = row;
105                         if (row > lastRow)
106                             lastRow = row;
107                     }
108                     delayedRowsUpdated(firstRow, lastRow);
109                 }
110             }
111             
112             public void treeNodesInserted(TreeModelEvent JavaDoc e) {
113                 delayedFireTableDataChanged();
114             }
115             
116             public void treeNodesRemoved(TreeModelEvent JavaDoc e) {
117                 delayedFireTableDataChanged();
118             }
119             
120             public void treeStructureChanged(TreeModelEvent JavaDoc e) {
121                 delayedFireTableDataChanged();
122             }
123         };
124         
125         // Install a TreeModelListener that can update the table when
126
// tree changes. We use delayedFireTableDataChanged as we can
127
// not be guaranteed the tree will have finished processing
128
// the event before us.
129
treeTableModel.addTreeModelListener(tml);
130     }
131     
132     /**
133      * This method must always be called if this object is removed from a
134      * TreeTable
135      */

136     public void unregister() {
137         tree.removeTreeExpansionListener(tel);
138         treeTableModel.removeTreeModelListener(tml);
139     }
140     
141     // Wrappers, implementing TableModel interface.
142

143     public int getColumnCount() {
144         return treeTableModel.getColumnCount();
145     }
146     
147     public String JavaDoc getColumnName(int column) {
148         return treeTableModel.getColumnName(column);
149     }
150     
151     public Class JavaDoc getColumnClass(int column) {
152         return treeTableModel.getColumnClass(column);
153     }
154     
155     public int getRowCount() {
156         int r = tree.getRowCount();
157         return r;
158     }
159     
160     protected Object JavaDoc nodeForRow(int row) {
161         TreePath JavaDoc treePath = tree.getPathForRow(row);
162         if (treePath == null)
163             return null;
164         else
165             return treePath.getLastPathComponent();
166     }
167     
168     public Object JavaDoc getValueAt(int row, int column) {
169         Object JavaDoc node = nodeForRow(row);
170         if (node == null)
171             return null;
172         return treeTableModel.getValueAt(node, column);
173     }
174     
175     public boolean isCellEditable(int row, int column) {
176         return treeTableModel.isCellEditable(nodeForRow(row), column);
177     }
178     
179     public void setValueAt(Object JavaDoc value, int row, int column) {
180         treeTableModel.setValueAt(value, nodeForRow(row), column);
181     }
182     
183     /**
184      * Invokes fireTableDataChanged after all the pending events have been
185      * processed. SwingUtilities.invokeLater is used to handle this.
186      */

187     protected void delayedFireTableDataChanged() {
188         /*SwingUtilities.invokeLater(new Runnable() {
189             public void run() {*/

190                 fireTableDataChanged();
191             /*}
192         });*/

193     }
194
195     /**
196      * Invokes fireTableRowsInserted after all the pending events have been
197      * processed. SwingUtilities.invokeLater is used to handle this.
198      */

199     protected void delayedFireTableRowsInserted(final int first,
200             final int last) {
201         SwingUtilities.invokeLater(new Runnable JavaDoc() {
202             public void run() {
203                 fireTableRowsInserted(first, last);
204             }
205         });
206     }
207     
208     /**
209      * Invokes fireTableRowsUpdated after all the pending events have been
210      * processed. SwingUtilities.invokeLater is used to handle this.
211      */

212     protected void delayedRowsUpdated(final int firstRow, final int lastRow) {
213         /*SwingUtilities.invokeLater(new Runnable() {
214             public void run() {*/

215                 fireTableRowsUpdated(firstRow, lastRow);
216             /*}
217         });*/

218     }
219 }
220
221
Popular Tags