1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// NOTE: split back the renderer and the editor into two separate class as it provoked rendering issues when the editing occured.
/** Class that serves as the tree renderer (displays check box).
* @author Fabrice Bouyé (fabriceb@spc.int)
* @version 1.0
*/
private class CheckRenderer
extends DefaultTreeCellRenderer {
/** The delegated checkbox.
*/
private JCheckBox check = new JCheckBox();
/** Creates a new instance.
*/
public CheckRenderer() {
super();
check.setOpaque(false);
check.setHorizontalTextPosition(AbstractButton.LEADING);
check.setFocusPainted(true);
}
/** {@inheritDoc}
*/
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
//Component component = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
TreeCell cell = (TreeCell) node.getUserObject();
//check.setForeground(component.getForeground());
//check.setBackground(component.getBackground());
check.setSelected(cell.displayed);
check.setText(cell.screenname);
check.setEnabled(cell.enabled);
String tooltip = ExtendedConstants.HTML_LABEL_PREFIX + cell.description + ExtendedConstants.HTML_LABEL_SUFFIX;
check.setToolTipText(tooltip);
return check;
}
}
/** Class that serves as the tree editor (displays check box).
* @author Fabrice Bouyé (fabriceb@spc.int)
* @version 1.0
*/
private class CheckEditor
extends DefaultTreeCellEditor implements ActionListener {
/** The delegated checkbox.
*/
private JCheckBox check = new JCheckBox();
/** A hack that prevents the check box from beeing unselectd the first time the user clicks on it.
*/
private boolean firstClick = true;
/** The editor value.
*/
private Object value;
/** The previous value.
*/
private boolean oldDisplayed;
/** Creates a new instance.
* @param displayTree The parent tree.
*/
public CheckEditor(JTree displayTree) {
super(displayTree, (DefaultTreeCellRenderer) displayTree.getCellRenderer());
check.setOpaque(false);
check.setHorizontalTextPosition(AbstractButton.LEADING);
check.addActionListener(this);
check.setFocusPainted(true);
}
/** {@inheritDoc}
*/
public void cancelCellEditing() {
super.cancelCellEditing();
//DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
//TreeCell cell = (TreeCell) node.getUserObject();
//cell.displayed = oldDisplayed;
//System.err.println("cancelCellEditing");
firstClick = true;
}
/** {@inheritDoc}
*/
public Object getCellEditorValue() {
//System.err.println("getCellEditorValue");
return value;
}
/** {@inheritDoc}
*/
public boolean isCellEditable(EventObject anEvent) {
//System.err.println("isCellEditable");
boolean result = false;
if (anEvent instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) anEvent;
// How to detect the mouse click happened ontop of the check area, not on the check label?
//if (mouseEvent.getClickCount() >= 2) {
TreePath path = displayTree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
TreeCell cell = (TreeCell) node.getUserObject();
result = cell.enabled;
//}
}
return result;
}
/** {@inheritDoc}
*/
public boolean stopCellEditing() {
boolean result = super.stopCellEditing();
//System.err.println("stopCellEditing " + result);
firstClick = true;
return result;
}
/** {@inheritDoc}
*/
public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) {
//Component component = super.getTreeCellEditorComponent(tree, value, isSelected, expanded, leaf, row);
//System.err.println("getTreeCellEditorComponent");
this.value = value;
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
TreeCell cell = (TreeCell) node.getUserObject();
this.oldDisplayed = cell.displayed;
//check.setForeground(component.getForeground());
//check.setBackground(component.getBackground());
check.setSelected(cell.displayed);
check.setText(cell.screenname);
check.setEnabled(cell.enabled);
String tooltip = ExtendedConstants.HTML_LABEL_PREFIX + cell.description + ExtendedConstants.HTML_LABEL_SUFFIX;
check.setToolTipText(tooltip);
//firstClick = true;
return check;
}
/** {@inheritDoc}
*/
public void actionPerformed(ActionEvent event) {
//System.err.println("actionPerformed");
if (firstClick) {
check.setSelected(!check.isSelected());
firstClick = false;
return;
}
DefaultMutableTreeNode node = (DefaultMutableTreeNode) getCellEditorValue();
TreeCell treeCell = (TreeCell) node.getUserObject();
boolean value = check.isSelected();
treeCell.displayed = value;
enableChildren(node, value);
displayTree.repaint();
}
/** Enable/disable the children of the given node.
* @param parent The parent node.
* @param value If <CODE>true</CODE> the node will be disabled.
*/
private void enableChildren(DefaultMutableTreeNode parent, boolean value) {
int childNumber = parent.getChildCount();
for (int i = 0; i < childNumber; i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) parent.getChildAt(i);
TreeCell cell = (TreeCell) child.getUserObject();
cell.enabled = value;
enableChildren(child, value);
}
}
} |