public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); if (!showRootNodeCheckBox && tree.getModel().getRoot() == value) return renderer; TreePath path = tree.getPathForRow(row); if (path != null) { if (checkBoxCustomer != null && !checkBoxCustomer.showCheckBox(path)) return renderer; if (selectionModel.isPathSelected(path, selectionModel.isDigged())) checkBox.getTristateModel().setState(TristateState.SELECTED); else checkBox .getTristateModel() .setState( selectionModel.isDigged() && selectionModel.isPartiallySelected(path) ? TristateState.INDETERMINATE : TristateState.DESELECTED); } removeAll(); add(checkBox, BorderLayout.WEST); add(renderer, BorderLayout.CENTER); return this; }
/** * Constructor. * * @param tree a JTree */ public CheckTreeManager(JTree tree) { this.tree = tree; selectionModel = new CheckTreeSelectionModel(tree.getModel()); tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel)); tree.addMouseListener(this); tree.addMouseMotionListener(this); selectionModel.addTreeSelectionListener(this); }
/** * Handles mouse click events. * * @param e the mouse event */ public void mouseClicked(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) return; if (e.getX() > tree.getPathBounds(path).x + hotspot) return; boolean selected = selectionModel.isAncestorSelected(path); try { ignoreEvents = true; if (selected) { selectionModel.removeSelectionPath(path); } else { selectionModel.addSelectionPath(path); } } finally { ignoreEvents = false; tree.treeDidChange(); } }