TreePath (@NotNull JTree tree, @NotNull TreeVisitor visitor) { TreePath parent = null; int count = tree.getRowCount(); for (int row = 0; row < count; row++) { TreePath path = getVisiblePathWithValidation(tree, row, count); if (parent == null || !parent.isDescendant(path)) { switch (visitor.visit(path)) { case INTERRUPT -> { return path; // path is found } case CONTINUE -> parent = null; case SKIP_CHILDREN -> parent = path; case SKIP_SIBLINGS -> { parent = path.getParentPath(); if (parent == null) return null; } } } } return null; }