void (JTree tree) { TreePath anchor = getAnchor(tree); if (anchor == null) return; // not found if (!tree.isPathSelected(anchor)) return; // not selected TreePath lower = anchor; for (TreePath upper = anchor.getParentPath(); upper != null; lower = upper, upper = upper.getParentPath()) { if (testDescendants(tree, upper, tree::isPathSelected)) { if (tree.isPathSelected(upper)) continue; // search for upper bounds if all descendants are selected TreePath except = lower; // to be effective final if (acceptDescendants(tree, upper, path -> tree.isPathSelected(path) && !except.isDescendant(path), tree::removeSelectionPaths)) { setAnchor(tree, anchor); return; // interrupt if siblings were unselected } } if (lower != anchor) { tree.removeSelectionPath(lower); setAnchor(tree, anchor); return; // interrupt if an anchored child is unselected } if (acceptDescendants(tree, lower, tree::isPathSelected, tree::removeSelectionPaths)) { setAnchor(tree, anchor); // store an anchor only if selection is changed } return; } }