View Javadoc
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   * This code is taken from the tutorial written by Jörn Hameister, <A
13   * HREF="http://www.hameister.org/JavaSwingTreeTable.html">available here</A>.<BR/>
14   * <BR/>
15   * From Jörn sample, the main modifications are:
16   * <UL>
17   * <LI>Add of an even Listener, to report data modifications to the table.</LI>
18   * <UL>
19   * <BR/>
20   * Since Java Swing GUI components also still needed a model that actually is not equal to the data model, is now a
21   * class MyTreeTableModelAdapter which of AbstractTableModel inherits created. This class will be later in the class
22   * MyTreeTable as a model for the JTable uses. If the TreeTable later for values ​​asks to be displayed, it must be
23   * determined whether the requested values ​​of the tree or directly from the data model
24   * MyAbstractTreeTableModel<MyHierarchicalNode> can be delivered. In addition, in the class, nor is
25   * TreeExpansionListener created and registered. This responds to clicks in the tree and makes dafüf that the tree is up
26   * and closed.
27   * 
28   * @author Jörn Hameister
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          // Registration of a new TreeExpansionListener, for the JTree
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 }