1 7 8 package org.jdesktop.swing.treetable; 9 10 import java.util.EventObject ; 11 12 import java.awt.Component ; 13 import java.awt.Rectangle ; 14 import java.awt.event.MouseEvent ; 15 import javax.swing.DefaultCellEditor ; 16 import javax.swing.Icon ; 17 import javax.swing.JTable ; 18 import javax.swing.JTextField ; 19 import javax.swing.JTree ; 20 import javax.swing.tree.DefaultTreeCellRenderer ; 21 import javax.swing.tree.TreeCellRenderer ; 22 23 import org.jdesktop.swing.JXTreeTable; 24 25 64 public class TreeTableCellEditor extends DefaultCellEditor { 65 public TreeTableCellEditor(JXTreeTable treeTable, JTree tree) { 66 super(new TreeTableTextField()); 67 if (treeTable == null) { 68 throw new IllegalArgumentException ("null treeTable"); 69 } 70 if (tree == null) { 71 throw new IllegalArgumentException ("null tree"); 72 } 73 74 this.treeTable = treeTable; this.tree = tree; } 77 78 86 public Component getTableCellEditorComponent(JTable table, Object value, 87 boolean isSelected, int row, 88 int column) { 89 Component component = super.getTableCellEditorComponent(table, value, 90 isSelected, row, column); 91 92 Rectangle bounds = tree.getRowBounds(row); 94 int offset = bounds.x; 95 TreeCellRenderer tcr = tree.getCellRenderer(); 96 if (tcr instanceof DefaultTreeCellRenderer ) { 97 Object node = tree.getPathForRow(row).getLastPathComponent(); 98 Icon icon; 99 if (tree.getModel().isLeaf(node)) 100 icon = ((DefaultTreeCellRenderer ) tcr).getLeafIcon(); 101 else if (tree.isExpanded(row)) 102 icon = ((DefaultTreeCellRenderer ) tcr).getOpenIcon(); 103 else 104 icon = ((DefaultTreeCellRenderer ) tcr).getClosedIcon(); 105 106 if (icon != null) { 107 offset += ((DefaultTreeCellRenderer ) tcr).getIconTextGap() + 108 icon.getIconWidth(); 109 } 110 } 111 ((TreeTableTextField) getComponent()).offset = offset; 112 return component; 114 } 115 116 120 public boolean isCellEditable(EventObject e) { 121 if (e == null) { 122 return true; 123 } 124 else if (e instanceof MouseEvent ) { 125 return (((MouseEvent ) e).getClickCount() >= clickCountToStart); 128 } 129 130 return false; 132 } 133 134 139 static class TreeTableTextField extends JTextField { 140 int offset; 142 public void reshape(int x, int y, int width, int height) { 143 int newOffset = offset - getInsets().left; 148 super.reshape(x + newOffset, y, width - newOffset, height); 149 } 150 } 151 152 private final JXTreeTable treeTable; private final JTree tree; } | Popular Tags |