KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)TreeTableModelAdapter.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.JTree JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import javax.swing.event.TreeExpansionEvent JavaDoc;
37 import javax.swing.event.TreeExpansionListener JavaDoc;
38 import javax.swing.event.TreeModelEvent JavaDoc;
39 import javax.swing.event.TreeModelListener JavaDoc;
40 import javax.swing.table.AbstractTableModel JavaDoc;
41 import javax.swing.tree.TreePath JavaDoc;
42
43 /**
44  * This is a wrapper class takes a TreeTableModel and implements
45  * the table model interface. The implementation is trivial, with
46  * all of the event dispatching support provided by the superclass:
47  * the AbstractTableModel.
48  *
49  * @version 1.2 10/27/98
50  *
51  * @author Philip Milne
52  * @author Scott Violet
53  */

54 public class TreeTableModelAdapter extends AbstractTableModel JavaDoc
55 {
56     JTree JavaDoc tree;
57     TreeTableModel treeTableModel;
58
59     public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree JavaDoc tree) {
60         this.tree = tree;
61         this.treeTableModel = treeTableModel;
62
63     tree.addTreeExpansionListener(new TreeExpansionListener JavaDoc() {
64         // Don't use fireTableRowsInserted() here; the selection model
65
// would get updated twice.
66
public void treeExpanded(TreeExpansionEvent JavaDoc event) {
67           fireTableDataChanged();
68         }
69             public void treeCollapsed(TreeExpansionEvent JavaDoc event) {
70           fireTableDataChanged();
71         }
72     });
73
74     // Install a TreeModelListener that can update the table when
75
// tree changes. We use delayedFireTableDataChanged as we can
76
// not be guaranteed the tree will have finished processing
77
// the event before us.
78
treeTableModel.addTreeModelListener(new TreeModelListener JavaDoc() {
79         public void treeNodesChanged(TreeModelEvent JavaDoc e) {
80         delayedFireTableDataChanged();
81         }
82
83         public void treeNodesInserted(TreeModelEvent JavaDoc e) {
84         delayedFireTableDataChanged();
85         }
86
87         public void treeNodesRemoved(TreeModelEvent JavaDoc e) {
88         delayedFireTableDataChanged();
89         }
90
91         public void treeStructureChanged(TreeModelEvent JavaDoc e) {
92         delayedFireTableDataChanged();
93         }
94     });
95     }
96
97     // Wrappers, implementing TableModel interface.
98

99     public int getColumnCount() {
100     return treeTableModel.getColumnCount();
101     }
102
103     public String JavaDoc getColumnName(int column) {
104     return treeTableModel.getColumnName(column);
105     }
106
107     public Class JavaDoc getColumnClass(int column) {
108     return treeTableModel.getColumnClass(column);
109     }
110
111     public int getRowCount() {
112     return tree.getRowCount();
113     }
114
115     protected Object JavaDoc nodeForRow(int row) {
116     TreePath JavaDoc treePath = tree.getPathForRow(row);
117     return treePath.getLastPathComponent();
118     }
119
120     public Object JavaDoc getValueAt(int row, int column) {
121     return treeTableModel.getValueAt(nodeForRow(row), column);
122     }
123
124     public boolean isCellEditable(int row, int column) {
125          return treeTableModel.isCellEditable(nodeForRow(row), column);
126     }
127
128     public void setValueAt(Object JavaDoc value, int row, int column) {
129     treeTableModel.setValueAt(value, nodeForRow(row), column);
130     }
131
132     /**
133      * Invokes fireTableDataChanged after all the pending events have been
134      * processed. SwingUtilities.invokeLater is used to handle this.
135      */

136     protected void delayedFireTableDataChanged() {
137     SwingUtilities.invokeLater(new Runnable JavaDoc() {
138         public void run() {
139         fireTableDataChanged();
140         }
141     });
142     }
143 }
144
145
Popular Tags