public static List<TreePath> collectExpandedPaths(final JTree tree, TreePath path) { final ArrayList<TreePath> result = new ArrayList<TreePath>(); if (!tree.isExpanded(path)) return result; final Object lastPathComponent = path.getLastPathComponent(); final TreeModel model = tree.getModel(); if (model.isLeaf(lastPathComponent)) { result.add(path); } else { boolean pathWasAdded = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { final TreePath childPath = path.pathByAddingChild(model.getChild(lastPathComponent, i)); if (model.isLeaf(lastPathComponent)) { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } else if (tree.isExpanded(childPath)) { result.addAll(collectExpandedPaths(tree, childPath)); } else { if (!pathWasAdded) { result.add(path); pathWasAdded = true; } } } } return result; }
public static ActionCallback selectFirstNode(final JTree tree) { final TreeModel model = tree.getModel(); final Object root = model.getRoot(); TreePath selectionPath = new TreePath(root); if (!tree.isRootVisible() && model.getChildCount(root) > 0) selectionPath = selectionPath.pathByAddingChild(model.getChild(root, 0)); return selectPath(tree, selectionPath); }
@Nullable public static TreeNode findNodeWithObject( final Object object, final TreeModel model, final Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { final DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) model.getChild(parent, i); if (childNode.getUserObject().equals(object)) return childNode; } return null; }
@Nullable private static TreePath findNodePath(MavenArchetype object, TreeModel model, Object parent) { for (int i = 0; i < model.getChildCount(parent); i++) { DefaultMutableTreeNode each = (DefaultMutableTreeNode) model.getChild(parent, i); if (each.getUserObject().equals(object)) return new TreePath(each.getPath()); TreePath result = findNodePath(object, model, each); if (result != null) return result; } return null; }
private static boolean collectExpandedPathsImpl( final JTree tree, final Collection<TreePath> paths, final TreePath path) { final TreeModel model = tree.getModel(); final Object lastPathComponent = path.getLastPathComponent(); if (model.isLeaf(lastPathComponent)) { return false; } boolean hasExpandedChildren = false; for (int i = model.getChildCount(lastPathComponent) - 1; i >= 0; i--) { hasExpandedChildren |= collectExpandedPathsImpl( tree, paths, path.pathByAddingChild(model.getChild(lastPathComponent, i))); } if (!hasExpandedChildren) { paths.add(path); return true; } else { return false; } }
private String getFilterStatus(TreeModel displayModel, TreeModel originalModel) { int count = displayModel.getChildCount(displayModel.getRoot()); String text = count + " out of " + originalModel.getChildCount(originalModel.getRoot()) + " files"; return text; }