0
|
1 /*
|
|
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 *
|
|
5 * This code is free software; you can redistribute it and/or modify it
|
|
6 * under the terms of the GNU General Public License version 2 only, as
|
|
7 * published by the Free Software Foundation.
|
|
8 *
|
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 * version 2 for more details (a copy is included in the LICENSE file that
|
|
13 * accompanied this code).
|
|
14 *
|
|
15 * You should have received a copy of the GNU General Public License version
|
|
16 * 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 *
|
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 package sun.jvm.hotspot.ui.treetable;
|
|
26
|
|
27 import javax.swing.JTree;
|
|
28 import javax.swing.SwingUtilities;
|
|
29 import javax.swing.table.AbstractTableModel;
|
|
30 import javax.swing.tree.TreePath;
|
|
31 import javax.swing.event.TreeExpansionEvent;
|
|
32 import javax.swing.event.TreeExpansionListener;
|
|
33 import javax.swing.event.TreeModelEvent;
|
|
34 import javax.swing.event.TreeModelListener;
|
|
35
|
|
36 /**
|
|
37 * This is a wrapper class takes a TreeTableModel and implements
|
|
38 * the table model interface. The implementation is trivial, with
|
|
39 * all of the event dispatching support provided by the superclass:
|
|
40 * the AbstractTableModel.
|
|
41 *
|
|
42 *
|
|
43 * @author Philip Milne
|
|
44 * @author Scott Violet
|
|
45 */
|
|
46 public class TreeTableModelAdapter extends AbstractTableModel
|
|
47 {
|
|
48 JTree tree;
|
|
49 TreeTableModel treeTableModel;
|
|
50
|
|
51 public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
|
|
52 this.tree = tree;
|
|
53 this.treeTableModel = treeTableModel;
|
|
54
|
|
55 tree.addTreeExpansionListener(new TreeExpansionListener() {
|
|
56 // Don't use fireTableRowsInserted() here; the selection model
|
|
57 // would get updated twice.
|
|
58 public void treeExpanded(TreeExpansionEvent event) {
|
|
59 fireTableDataChanged();
|
|
60 }
|
|
61 public void treeCollapsed(TreeExpansionEvent event) {
|
|
62 fireTableDataChanged();
|
|
63 }
|
|
64 });
|
|
65
|
|
66 // Install a TreeModelListener that can update the table when
|
|
67 // tree changes. We use delayedFireTableDataChanged as we can
|
|
68 // not be guaranteed the tree will have finished processing
|
|
69 // the event before us.
|
|
70 treeTableModel.addTreeModelListener(new TreeModelListener() {
|
|
71 public void treeNodesChanged(TreeModelEvent e) {
|
|
72 delayedFireTableDataChanged();
|
|
73 }
|
|
74
|
|
75 public void treeNodesInserted(TreeModelEvent e) {
|
|
76 delayedFireTableDataChanged();
|
|
77 }
|
|
78
|
|
79 public void treeNodesRemoved(TreeModelEvent e) {
|
|
80 delayedFireTableDataChanged();
|
|
81 }
|
|
82
|
|
83 public void treeStructureChanged(TreeModelEvent e) {
|
|
84 delayedFireTableDataChanged();
|
|
85 }
|
|
86 });
|
|
87 }
|
|
88
|
|
89 // Wrappers, implementing TableModel interface.
|
|
90
|
|
91 public int getColumnCount() {
|
|
92 return treeTableModel.getColumnCount();
|
|
93 }
|
|
94
|
|
95 public String getColumnName(int column) {
|
|
96 return treeTableModel.getColumnName(column);
|
|
97 }
|
|
98
|
|
99 public Class getColumnClass(int column) {
|
|
100 return treeTableModel.getColumnClass(column);
|
|
101 }
|
|
102
|
|
103 public int getRowCount() {
|
|
104 return tree.getRowCount();
|
|
105 }
|
|
106
|
|
107 protected Object nodeForRow(int row) {
|
|
108 TreePath treePath = tree.getPathForRow(row);
|
|
109 return treePath.getLastPathComponent();
|
|
110 }
|
|
111
|
|
112 public Object getValueAt(int row, int column) {
|
|
113 return treeTableModel.getValueAt(nodeForRow(row), column);
|
|
114 }
|
|
115
|
|
116 public boolean isCellEditable(int row, int column) {
|
|
117 return treeTableModel.isCellEditable(nodeForRow(row), column);
|
|
118 }
|
|
119
|
|
120 public void setValueAt(Object value, int row, int column) {
|
|
121 treeTableModel.setValueAt(value, nodeForRow(row), column);
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Invokes fireTableDataChanged after all the pending events have been
|
|
126 * processed. SwingUtilities.invokeLater is used to handle this.
|
|
127 */
|
|
128 protected void delayedFireTableDataChanged() {
|
|
129 SwingUtilities.invokeLater(new Runnable() {
|
|
130 public void run() {
|
|
131 fireTableDataChanged();
|
|
132 }
|
|
133 });
|
|
134 }
|
|
135 }
|