1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.*; 22 23 import javax.swing.JTree ; 24 import javax.swing.SwingUtilities ; 25 import javax.swing.event.*; 26 import javax.swing.tree.TreePath ; 27 28 29 33 class TreeTableModelAdapter extends NodeTableModel { 34 private JTree tree; 35 private NodeTableModel nodeTableModel; 36 37 public TreeTableModelAdapter(JTree t, NodeTableModel ntm) { 38 this.tree = t; 39 this.nodeTableModel = ntm; 40 41 Listener listener = new Listener(); 42 tree.addTreeExpansionListener(listener); 43 tree.getModel().addTreeModelListener(listener); 44 nodeTableModel.addTableModelListener(listener); 45 } 46 47 public void setNodes(Node[] nodes) { 49 nodeTableModel.setNodes(nodes); 50 } 51 52 public void setProperties(Node.Property[] props) { 53 nodeTableModel.setProperties(props); 54 } 55 56 protected Node.Property getPropertyFor(Node node, Node.Property prop) { 57 return nodeTableModel.getPropertyFor(node, prop); 58 } 59 60 Node nodeForRow(int row) { 61 return Visualizer.findNode(tree.getPathForRow(row).getLastPathComponent()); 62 } 63 64 Node.Property propertyForColumn(int column) { 65 return nodeTableModel.propertyForColumn(column - 1); 66 } 67 68 public int getColumnCount() { 70 return nodeTableModel.getColumnCount() + 1; 71 } 72 73 public String getColumnName(int column) { 74 return (column == 0) ? Visualizer.findNode(tree.getModel().getRoot()).getDisplayName() 75 : nodeTableModel.getColumnName(column - 1); 76 } 77 78 public Class getColumnClass(int column) { 79 return (column == 0) ? TreeTableModelAdapter.class : nodeTableModel.getColumnClass(column - 1); 80 } 81 82 public int getRowCount() { 83 return tree.getRowCount(); 84 } 85 86 public Object getValueAt(int row, int column) { 87 return (column == 0) ? tree.getPathForRow(row).getLastPathComponent() 88 : nodeTableModel.getPropertyFor(nodeForRow(row), propertyForColumn(column)); 89 } 90 91 public boolean isCellEditable(int row, int column) { 92 if (column == 0) { 93 return true; 94 } 95 96 Object o = getValueAt(row, column); 97 98 if (o == null) { 99 return false; 100 } 101 102 if (o instanceof Node.Property) { 103 return ((Node.Property) o).canWrite(); 104 } 105 106 return false; 107 } 108 109 public void setValueAt(Object value, int row, int column) { 110 } 111 112 114 class Listener implements TreeExpansionListener, TreeModelListener, TableModelListener, Runnable { 115 TreePath [] tps = null; 117 118 public void treeExpanded(TreeExpansionEvent event) { 120 updateNodes(); 121 } 122 123 public void treeCollapsed(TreeExpansionEvent event) { 124 updateNodes(); 125 } 126 127 public void treeNodesChanged(TreeModelEvent e) { 133 delayedUpdateNodes(e); 134 } 135 136 public void treeNodesInserted(TreeModelEvent e) { 137 delayedUpdateNodes(e); 138 } 139 140 public void treeNodesRemoved(TreeModelEvent e) { 141 delayedUpdateNodes(e); 142 } 143 144 public void treeStructureChanged(TreeModelEvent e) { 145 tps = tree.getSelectionPaths(); 147 148 if ((e.getPath().length == 1) && !e.getTreePath().equals(e.getPath()[0])) { 151 tps = null; 152 } 153 154 delayedUpdateNodes(e); 155 } 156 157 public void tableChanged(TableModelEvent e) { 159 int c = e.getColumn(); 160 int column = (c == TableModelEvent.ALL_COLUMNS) ? TableModelEvent.ALL_COLUMNS : (c + 1); 161 fireTableChanged( 162 new TableModelEvent(TreeTableModelAdapter.this, e.getFirstRow(), e.getLastRow(), column, e.getType()) 163 ); 164 } 165 166 169 protected void delayedUpdateNodes(TreeModelEvent e) { 170 SwingUtilities.invokeLater(this); 175 } 176 177 public void run() { 178 updateNodes(); 179 } 180 181 private void updateNodes() { 182 Node[] nodes = new Node[tree.getRowCount()]; 183 184 for (int i = 0; i < tree.getRowCount(); i++) { 185 nodes[i] = Visualizer.findNode(tree.getPathForRow(i).getLastPathComponent()); 186 } 187 188 setNodes(nodes); 189 190 if (tps != null) { 192 tree.setSelectionPaths(tps); 193 tps = null; 194 } 195 } 196 } 197 } 198 | Popular Tags |