1 20 33 34 package com.puppycrawl.tools.checkstyle.gui; 35 36 import javax.swing.JTree ; 37 import javax.swing.SwingUtilities ; 38 import javax.swing.table.AbstractTableModel ; 39 import javax.swing.tree.TreePath ; 40 import javax.swing.event.TreeExpansionEvent ; 41 import javax.swing.event.TreeExpansionListener ; 42 import javax.swing.event.TreeModelEvent ; 43 import javax.swing.event.TreeModelListener ; 44 45 56 public class TreeTableModelAdapter extends AbstractTableModel 57 { 58 private JTree mTree; 59 private TreeTableModel mTreeTableModel; 60 61 public TreeTableModelAdapter(TreeTableModel aTreeTableModel, JTree aTree) 62 { 63 this.mTree = aTree; 64 this.mTreeTableModel = aTreeTableModel; 65 66 aTree.addTreeExpansionListener(new TreeExpansionListener () 67 { 68 public void treeExpanded(TreeExpansionEvent event) 71 { 72 fireTableDataChanged(); 73 } 74 75 public void treeCollapsed(TreeExpansionEvent event) 76 { 77 fireTableDataChanged(); 78 } 79 }); 80 81 aTreeTableModel.addTreeModelListener(new TreeModelListener () 86 { 87 public void treeNodesChanged(TreeModelEvent e) 88 { 89 delayedFireTableDataChanged(); 90 } 91 92 public void treeNodesInserted(TreeModelEvent e) 93 { 94 delayedFireTableDataChanged(); 95 } 96 97 public void treeNodesRemoved(TreeModelEvent e) 98 { 99 delayedFireTableDataChanged(); 100 } 101 102 public void treeStructureChanged(TreeModelEvent e) 103 { 104 delayedFireTableDataChanged(); 105 } 106 }); 107 } 108 109 111 public int getColumnCount() 112 { 113 return mTreeTableModel.getColumnCount(); 114 } 115 116 public String getColumnName(int column) 117 { 118 return mTreeTableModel.getColumnName(column); 119 } 120 121 public Class getColumnClass(int column) 122 { 123 return mTreeTableModel.getColumnClass(column); 124 } 125 126 public int getRowCount() 127 { 128 return mTree.getRowCount(); 129 } 130 131 protected Object nodeForRow(int row) 132 { 133 final TreePath treePath = mTree.getPathForRow(row); 134 return treePath.getLastPathComponent(); 135 } 136 137 public Object getValueAt(int row, int column) 138 { 139 return mTreeTableModel.getValueAt(nodeForRow(row), column); 140 } 141 142 public boolean isCellEditable(int row, int column) 143 { 144 return mTreeTableModel.isCellEditable(nodeForRow(row), column); 145 } 146 147 public void setValueAt(Object value, int row, int column) 148 { 149 mTreeTableModel.setValueAt(value, nodeForRow(row), column); 150 } 151 152 156 protected void delayedFireTableDataChanged() 157 { 158 SwingUtilities.invokeLater(new Runnable () 159 { 160 public void run() 161 { 162 fireTableDataChanged(); 163 } 164 }); 165 } 166 } 167 168 | Popular Tags |