1 package wjhk.jupload2.gui.filepanel.treeview;
2
3 import javax.swing.JTree;
4 import javax.swing.event.TreeExpansionEvent;
5 import javax.swing.event.TreeExpansionListener;
6 import javax.swing.event.TreeModelEvent;
7 import javax.swing.event.TreeModelListener;
8 import javax.swing.table.AbstractTableModel;
9 import javax.swing.tree.TreePath;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class MyTreeTableModelAdapter<T extends MyTreeNode> extends AbstractTableModel {
31
32 private static final long serialVersionUID = 1L;
33
34 JTree tree;
35
36 MyTreeTableModel<T> treeTableModel;
37
38 public MyTreeTableModelAdapter(MyTreeTableModel<T> treeTableModel, JTree tree) {
39 this.tree = tree;
40 this.treeTableModel = treeTableModel;
41
42
43 tree.addTreeExpansionListener(new TreeExpansionListener() {
44 public void treeExpanded(TreeExpansionEvent event) {
45 fireTableDataChanged();
46 }
47
48 public void treeCollapsed(TreeExpansionEvent event) {
49 fireTableDataChanged();
50 }
51 });
52
53 treeTableModel.addTreeModelListener(new TreeModelListener() {
54
55 public void treeNodesChanged(TreeModelEvent e) {
56 fireTableDataChanged();
57 }
58
59 public void treeNodesInserted(TreeModelEvent e) {
60 fireTableDataChanged();
61 }
62
63 public void treeNodesRemoved(TreeModelEvent e) {
64 fireTableDataChanged();
65 }
66
67 public void treeStructureChanged(TreeModelEvent e) {
68 fireTableDataChanged();
69 }
70
71 });
72 }
73
74 public int getColumnCount() {
75 return treeTableModel.getColumnCount();
76 }
77
78 public String getColumnName(int column) {
79 return treeTableModel.getColumnName(column);
80 }
81
82 public Class<?> getColumnClass(int column) {
83 return treeTableModel.getColumnClass(column);
84 }
85
86 public int getRowCount() {
87 return tree.getRowCount();
88 }
89
90 @SuppressWarnings("unchecked")
91 protected T nodeForRow(int row) {
92 TreePath treePath = tree.getPathForRow(row);
93 return (treePath == null) ? null : (T) treePath.getLastPathComponent();
94 }
95
96 public Object getValueAt(int row, int column) {
97 return treeTableModel.getValueAt(nodeForRow(row), column);
98 }
99
100 public boolean isCellEditable(int row, int column) {
101 return treeTableModel.isCellEditable(nodeForRow(row), column);
102 }
103
104 public void setValueAt(Object value, int row, int column) {
105 treeTableModel.setValueAt(value, nodeForRow(row), column);
106 }
107 }