KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > 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 package org.openide.explorer.view;
20
21 import org.openide.nodes.*;
22
23 import javax.swing.JTree JavaDoc;
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.event.*;
26 import javax.swing.tree.TreePath JavaDoc;
27
28
29 /* NodeTableModel synchronizing tree and table model. Used by TreeTable.
30  *
31  * @author Jan Rojcek
32  */

33 class TreeTableModelAdapter extends NodeTableModel {
34     private JTree JavaDoc tree;
35     private NodeTableModel nodeTableModel;
36
37     public TreeTableModelAdapter(JTree JavaDoc t, NodeTableModel ntm) {
38         this.tree = t;
39         this.nodeTableModel = ntm;
40
41         Listener listener = new Listener();
42         tree.addTreeExpansionListener(listener);
43         tree.getModel().addTreeModelListener(listener);
44         nodeTableModel.addTableModelListener(listener);
45     }
46
47     // NodeTableModel methods
48
public void setNodes(Node[] nodes) {
49         nodeTableModel.setNodes(nodes);
50     }
51
52     public void setProperties(Node.Property[] props) {
53         nodeTableModel.setProperties(props);
54     }
55
56     protected Node.Property getPropertyFor(Node node, Node.Property prop) {
57         return nodeTableModel.getPropertyFor(node, prop);
58     }
59
60     Node nodeForRow(int row) {
61         return Visualizer.findNode(tree.getPathForRow(row).getLastPathComponent());
62     }
63
64     Node.Property propertyForColumn(int column) {
65         return nodeTableModel.propertyForColumn(column - 1);
66     }
67
68     // Wrappers, implementing TableModel interface.
69
public int getColumnCount() {
70         return nodeTableModel.getColumnCount() + 1;
71     }
72
73     public String JavaDoc getColumnName(int column) {
74         return (column == 0) ? Visualizer.findNode(tree.getModel().getRoot()).getDisplayName()
75                              : nodeTableModel.getColumnName(column - 1);
76     }
77
78     public Class JavaDoc getColumnClass(int column) {
79         return (column == 0) ? TreeTableModelAdapter.class : nodeTableModel.getColumnClass(column - 1);
80     }
81
82     public int getRowCount() {
83         return tree.getRowCount();
84     }
85
86     public Object JavaDoc getValueAt(int row, int column) {
87         return (column == 0) ? tree.getPathForRow(row).getLastPathComponent()
88                              : nodeTableModel.getPropertyFor(nodeForRow(row), propertyForColumn(column));
89     }
90
91     public boolean isCellEditable(int row, int column) {
92         if (column == 0) {
93             return true;
94         }
95
96         Object JavaDoc o = getValueAt(row, column);
97
98         if (o == null) {
99             return false;
100         }
101
102         if (o instanceof Node.Property) {
103             return ((Node.Property) o).canWrite();
104         }
105
106         return false;
107     }
108
109     public void setValueAt(Object JavaDoc value, int row, int column) {
110     }
111
112     /* Listener for synchronizing tree and table model.
113      */

114     class Listener implements TreeExpansionListener, TreeModelListener, TableModelListener, Runnable JavaDoc {
115         // selection paths stored for restore after update
116
TreePath JavaDoc[] tps = null;
117
118         ///////// TreeExpansionListener
119
public void treeExpanded(TreeExpansionEvent event) {
120             updateNodes();
121         }
122
123         public void treeCollapsed(TreeExpansionEvent event) {
124             updateNodes();
125         }
126
127         ///////////// TreeModelListener
128
// Install a TreeModelListener that can update the table when
129
// tree changes. We use delayedUpdateNodes as we can
130
// not be guaranteed the tree will have finished processing
131
// the event before us.
132
public void treeNodesChanged(TreeModelEvent e) {
133             delayedUpdateNodes(e);
134         }
135
136         public void treeNodesInserted(TreeModelEvent e) {
137             delayedUpdateNodes(e);
138         }
139
140         public void treeNodesRemoved(TreeModelEvent e) {
141             delayedUpdateNodes(e);
142         }
143
144         public void treeStructureChanged(TreeModelEvent e) {
145             // bugfix #23757, store selection paths
146
tps = tree.getSelectionPaths();
147
148             // bugfix #30355, don't restore selection when the tree root changed
149
// (see javadoc TreeModelListener.treeStructureChanged)
150
if ((e.getPath().length == 1) && !e.getTreePath().equals(e.getPath()[0])) {
151                 tps = null;
152             }
153
154             delayedUpdateNodes(e);
155         }
156
157         ///////// TableModelListener
158
public void tableChanged(TableModelEvent e) {
159             int c = e.getColumn();
160             int column = (c == TableModelEvent.ALL_COLUMNS) ? TableModelEvent.ALL_COLUMNS : (c + 1);
161             fireTableChanged(
162                 new TableModelEvent(TreeTableModelAdapter.this, e.getFirstRow(), e.getLastRow(), column, e.getType())
163             );
164         }
165
166         /**
167          * Invokes fireTableDataChanged after all the pending events have been processed.
168          */

169         protected void delayedUpdateNodes(TreeModelEvent e) {
170             // Something like this can be used for updating tree column name ?!
171
//if (tree.getModel().getRoot().equals(e.getTreePath().getLastPathComponent())) {
172
// fireTableStructureChanged();
173
//}
174
SwingUtilities.invokeLater(this);
175         }
176
177         public void run() {
178             updateNodes();
179         }
180
181         private void updateNodes() {
182             Node[] nodes = new Node[tree.getRowCount()];
183
184             for (int i = 0; i < tree.getRowCount(); i++) {
185                 nodes[i] = Visualizer.findNode(tree.getPathForRow(i).getLastPathComponent());
186             }
187
188             setNodes(nodes);
189
190             // retore selection paths
191
if (tps != null) {
192                 tree.setSelectionPaths(tps);
193                 tps = null;
194             }
195         }
196     }
197 }
198
Popular Tags