1 | package genius.core.jtreetable;
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * @(#)TreeTableModelAdapter.java 1.2 98/10/27
|
---|
5 | *
|
---|
6 | * Copyright 1997, 1998 by Sun Microsystems, Inc.,
|
---|
7 | * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
|
---|
8 | * All rights reserved.
|
---|
9 | *
|
---|
10 | * This software is the confidential and proprietary information
|
---|
11 | * of Sun Microsystems, Inc. ("Confidential Information"). You
|
---|
12 | * shall not disclose such Confidential Information and shall use
|
---|
13 | * it only in accordance with the terms of the license agreement
|
---|
14 | * you entered into with Sun.
|
---|
15 | */
|
---|
16 |
|
---|
17 | import javax.swing.JTree;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 | import javax.swing.table.AbstractTableModel;
|
---|
20 | import javax.swing.tree.TreePath;
|
---|
21 | import javax.swing.event.TreeExpansionEvent;
|
---|
22 | import javax.swing.event.TreeExpansionListener;
|
---|
23 | import javax.swing.event.TreeModelEvent;
|
---|
24 | import javax.swing.event.TreeModelListener;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This is a wrapper class takes a TreeTableModel and implements
|
---|
28 | * the table model interface. The implementation is trivial, with
|
---|
29 | * all of the event dispatching support provided by the superclass:
|
---|
30 | * the AbstractTableModel.
|
---|
31 | *
|
---|
32 | * @version 1.2 10/27/98
|
---|
33 | *
|
---|
34 | * @author Philip Milne
|
---|
35 | * @author Scott Violet
|
---|
36 | */
|
---|
37 | public class TreeTableModelAdapter extends AbstractTableModel
|
---|
38 | {
|
---|
39 | JTree tree;
|
---|
40 | TreeTableModel treeTableModel;
|
---|
41 |
|
---|
42 | public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) {
|
---|
43 | this.tree = tree;
|
---|
44 | this.treeTableModel = treeTableModel;
|
---|
45 |
|
---|
46 | tree.addTreeExpansionListener(new TreeExpansionListener() {
|
---|
47 | // Don't use fireTableRowsInserted() here; the selection model
|
---|
48 | // would get updated twice.
|
---|
49 | public void treeExpanded(TreeExpansionEvent event) {
|
---|
50 | fireTableDataChanged();
|
---|
51 | }
|
---|
52 | public void treeCollapsed(TreeExpansionEvent event) {
|
---|
53 | fireTableDataChanged();
|
---|
54 | }
|
---|
55 | });
|
---|
56 |
|
---|
57 | // Install a TreeModelListener that can update the table when
|
---|
58 | // tree changes. We use delayedFireTableDataChanged as we can
|
---|
59 | // not be guaranteed the tree will have finished processing
|
---|
60 | // the event before us.
|
---|
61 | treeTableModel.addTreeModelListener(new TreeModelListener() {
|
---|
62 | public void treeNodesChanged(TreeModelEvent e) {
|
---|
63 | delayedFireTableDataChanged();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void treeNodesInserted(TreeModelEvent e) {
|
---|
67 | delayedFireTableDataChanged();
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void treeNodesRemoved(TreeModelEvent e) {
|
---|
71 | delayedFireTableDataChanged();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void treeStructureChanged(TreeModelEvent e) {
|
---|
75 | delayedFireTableDataChanged();
|
---|
76 | }
|
---|
77 | });
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Wrappers, implementing TableModel interface.
|
---|
81 |
|
---|
82 | public int getColumnCount() {
|
---|
83 | return treeTableModel.getColumnCount();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public String getColumnName(int column) {
|
---|
87 | return treeTableModel.getColumnName(column);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public Class getColumnClass(int column) {
|
---|
91 | return treeTableModel.getColumnClass(column);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public int getRowCount() {
|
---|
95 | return tree.getRowCount();
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected Object nodeForRow(int row) {
|
---|
99 | TreePath treePath = tree.getPathForRow(row);
|
---|
100 | return treePath.getLastPathComponent();
|
---|
101 | }
|
---|
102 |
|
---|
103 | public Object getValueAt(int row, int column) {
|
---|
104 | return treeTableModel.getValueAt(nodeForRow(row), column);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public boolean isCellEditable(int row, int column) {
|
---|
108 | return treeTableModel.isCellEditable(nodeForRow(row), column);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void setValueAt(Object value, int row, int column) {
|
---|
112 | treeTableModel.setValueAt(value, nodeForRow(row), column);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Invokes fireTableDataChanged after all the pending events have been
|
---|
117 | * processed. SwingUtilities.invokeLater is used to handle this.
|
---|
118 | */
|
---|
119 | protected void delayedFireTableDataChanged() {
|
---|
120 | SwingUtilities.invokeLater(new Runnable() {
|
---|
121 | public void run() {
|
---|
122 | fireTableDataChanged();
|
---|
123 | }
|
---|
124 | });
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|