View Javadoc
1   package edu.jiangxin.apktoolbox.swing.treetable;
2   
3   import java.awt.Rectangle;
4   
5   import javax.swing.JTree;
6   import javax.swing.event.TreeExpansionEvent;
7   import javax.swing.event.TreeExpansionListener;
8   import javax.swing.table.AbstractTableModel;
9   import javax.swing.tree.TreePath;
10  
11  public class MyTreeTableModelAdapter extends AbstractTableModel {
12  
13      JTree tree;
14      MyAbstractTreeTableModel treeTableModel;
15  
16      public MyTreeTableModelAdapter(MyAbstractTreeTableModel treeTableModel, JTree tree) {
17          this.tree = tree;
18          this.treeTableModel = treeTableModel;
19  
20          tree.addTreeExpansionListener(new TreeExpansionListener() {
21              public void treeExpanded(TreeExpansionEvent event) {
22                  fireTableDataChanged();
23              }
24  
25              public void treeCollapsed(TreeExpansionEvent event) {
26                  fireTableDataChanged();
27              }
28          });
29      }
30  
31  
32  
33      public int getColumnCount() {
34          return treeTableModel.getColumnCount();
35      }
36  
37      public String getColumnName(int column) {
38          return treeTableModel.getColumnName(column);
39      }
40  
41      public Class<?> getColumnClass(int column) {
42          return treeTableModel.getColumnClass(column);
43      }
44  
45      public int getRowCount() {
46          return tree.getRowCount();
47      }
48  
49      protected Object nodeForRow(int row) {
50          TreePath treePath = tree.getPathForRow(row);
51          return treePath.getLastPathComponent();
52      }
53  
54      public Object getValueAt(int row, int column) {
55          return treeTableModel.getValueAt(nodeForRow(row), column);
56      }
57  
58      public boolean isCellEditable(int row, int column) {
59          return treeTableModel.isCellEditable(nodeForRow(row), column);
60      }
61  
62      public void setValueAt(Object value, int row, int column) {
63          treeTableModel.setValueAt(value, nodeForRow(row), column);
64      }
65  }