1 19 20 24 package org.netbeans.modules.tasklist.usertasks.treetable; 25 26 import java.util.logging.Level ; 27 import javax.swing.JTree ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.event.TreeExpansionEvent ; 30 import javax.swing.event.TreeExpansionListener ; 31 import javax.swing.event.TreeModelEvent ; 32 import javax.swing.event.TreeModelListener ; 33 import javax.swing.table.AbstractTableModel ; 34 import javax.swing.tree.TreePath ; 35 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 36 37 49 public class TreeTableModelAdapter extends AbstractTableModel { 50 51 private static final long serialVersionUID = 1; 52 53 JTree tree; 54 TreeTableModel treeTableModel; 55 private TreeExpansionListener tel; 56 private TreeModelListener tml; 57 58 public TreeTableModelAdapter(TreeTableModel treeTableModel, JTree tree) { 59 this.tree = tree; 60 this.treeTableModel = treeTableModel; 61 62 tel = new TreeExpansionListener () { 63 public void treeExpanded(TreeExpansionEvent event) { 66 fireTableDataChanged(); 67 76 } 77 public void treeCollapsed(TreeExpansionEvent event) { 78 JTree tree = (JTree ) event.getSource(); 79 int row = tree.getRowForPath(event.getPath()); 80 int n = tree.getModel().getChildCount( 81 event.getPath().getLastPathComponent()); 82 if (n != 0) 83 fireTableRowsDeleted(row + 1, row + n); 84 } 85 }; 86 tree.addTreeExpansionListener(tel); 87 88 89 tml = new TreeModelListener () { 90 public void treeNodesChanged(TreeModelEvent e) { 91 TreePath tp = e.getTreePath(); 92 if (TreeTableModelAdapter.this.tree.isExpanded(tp)) { 93 int firstRow = Integer.MAX_VALUE; 94 int lastRow = Integer.MIN_VALUE; 95 int[] childIndices = e.getChildIndices(); 96 Object last = tp.getLastPathComponent(); 97 for (int i = 0; i < childIndices.length; i++) { 98 TreePath childPath = tp.pathByAddingChild( 99 TreeTableModelAdapter.this.treeTableModel.getChild( 100 last, childIndices[i])); 101 int row = TreeTableModelAdapter.this.tree.getRowForPath( 102 childPath); 103 if (row < firstRow) 104 firstRow = row; 105 if (row > lastRow) 106 lastRow = row; 107 } 108 delayedRowsUpdated(firstRow, lastRow); 109 } 110 } 111 112 public void treeNodesInserted(TreeModelEvent e) { 113 delayedFireTableDataChanged(); 114 } 115 116 public void treeNodesRemoved(TreeModelEvent e) { 117 delayedFireTableDataChanged(); 118 } 119 120 public void treeStructureChanged(TreeModelEvent e) { 121 delayedFireTableDataChanged(); 122 } 123 }; 124 125 treeTableModel.addTreeModelListener(tml); 130 } 131 132 136 public void unregister() { 137 tree.removeTreeExpansionListener(tel); 138 treeTableModel.removeTreeModelListener(tml); 139 } 140 141 143 public int getColumnCount() { 144 return treeTableModel.getColumnCount(); 145 } 146 147 public String getColumnName(int column) { 148 return treeTableModel.getColumnName(column); 149 } 150 151 public Class getColumnClass(int column) { 152 return treeTableModel.getColumnClass(column); 153 } 154 155 public int getRowCount() { 156 int r = tree.getRowCount(); 157 return r; 158 } 159 160 protected Object nodeForRow(int row) { 161 TreePath treePath = tree.getPathForRow(row); 162 if (treePath == null) 163 return null; 164 else 165 return treePath.getLastPathComponent(); 166 } 167 168 public Object getValueAt(int row, int column) { 169 Object node = nodeForRow(row); 170 if (node == null) 171 return null; 172 return treeTableModel.getValueAt(node, column); 173 } 174 175 public boolean isCellEditable(int row, int column) { 176 return treeTableModel.isCellEditable(nodeForRow(row), column); 177 } 178 179 public void setValueAt(Object value, int row, int column) { 180 treeTableModel.setValueAt(value, nodeForRow(row), column); 181 } 182 183 187 protected void delayedFireTableDataChanged() { 188 190 fireTableDataChanged(); 191 193 } 194 195 199 protected void delayedFireTableRowsInserted(final int first, 200 final int last) { 201 SwingUtilities.invokeLater(new Runnable () { 202 public void run() { 203 fireTableRowsInserted(first, last); 204 } 205 }); 206 } 207 208 212 protected void delayedRowsUpdated(final int firstRow, final int lastRow) { 213 215 fireTableRowsUpdated(firstRow, lastRow); 216 218 } 219 } 220 221 | Popular Tags |