private TreePath find2(JTree tree, TreePath parent, Village pNode, int depth) { TreeNode node = (TreeNode) parent.getLastPathComponent(); DefaultMutableTreeNode o = (DefaultMutableTreeNode) node; // If equal, go down the branch if (o.getUserObject().equals(pNode)) { // If at end, return match return parent; } else { // Traverse children if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); TreePath result = find2(tree, path, pNode, depth + 1); // Found a match if (result != null) { return result; } } } } // No match at this branch return null; }
private TreePath find2(TreePath parent, Object[] nodes, int depth, boolean byName) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (depth > nodes.length - 1) { return parent; } if (node.getChildCount() >= 0) { for (Enumeration<TreeNode> e = node.children(); e.hasMoreElements(); ) { TreeNode n = e.nextElement(); TreePath path = parent.pathByAddingChild(n); boolean find; if (byName) { find = n.toString().toUpperCase().contains(nodes[depth].toString().toUpperCase()); } else { find = n.equals(nodes[depth]); } if (find) { TreePath result = find2(path, nodes, depth + 1, byName); if (result != null) { return result; } } else { TreePath result = find2(path, nodes, depth, byName); if (result != null) { return result; } } } } return null; }
private static void expand(JTree tree, TreePath path, int levels) { if (levels == 0) return; tree.expandPath(path); TreeNode node = (TreeNode) path.getLastPathComponent(); Enumeration children = node.children(); while (children.hasMoreElements()) { expand(tree, path.pathByAddingChild(children.nextElement()), levels - 1); } }
/** * Traverses the children, finds those affected by the given node, and notifies them that they are * modified. * * @param start the node to start from * @param node the given node */ private void traverseModified(TreeNode start, Object node) { Enumeration children = start.children(); while (children.hasMoreElements()) { TreeNode child = (TreeNode) children.nextElement(); traverseModified(child, node); } if (start instanceof ExplorerTreeNode) { ((ExplorerTreeNode) start).nodeModified(node); } }
private void expandTree(JTree tree, TreePath parent) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration<?> e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandTree(tree, path); } } tree.expandPath(parent); }
/** * * 完全展开或关闭一个树,用于递规执行 * * @param tree JTree * @param parent 父节点 * @param expand 为true则表示展开树,否则为关闭整棵树 */ private void expandAll(JTree tree, TreePath parent, boolean expand) { TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, path, expand); } } if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } }
/** * Returns a copy of the tree starting at the given node. * * @param tree The tree to copy (may be {@code null}). * @return A mutable copy of the given tree, or {@code null} if the tree was null. * @todo Use {@code getUserObject} when we can. * @since 2.5 */ public static MutableTreeNode copy(final TreeNode node) { if (node == null) { return null; } final DefaultMutableTreeNode target = new DefaultMutableTreeNode(node.toString(), node.getAllowsChildren()); final Enumeration children = node.children(); if (children != null) { while (children.hasMoreElements()) { final TreeNode child = (TreeNode) children.nextElement(); target.add(copy(child)); } } return target; }
/** * Traverses all the children of the last component of the given tree path and calls this method * recursively on them and also expands or collapse the tree path. * * @param tree The working JTree * @param expand true means expand the tree path, false collapse it. * @param parent */ private static void expandAll(JTree tree, boolean expand, TreePath parent) { // Traverse children TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration<?> e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, expand, path); } } // Expansion or collapse must be done bottom-up if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } }
public void getPaths(JTree tree, TreePath parent, boolean expanded, List list) { // Return if node is not expanded if (expanded && !tree.isVisible(parent)) { return; } // Add node to list list.add(parent); // Create paths for all children TreeNode node = (TreeNode) parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e = node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode) e.nextElement(); TreePath path = parent.pathByAddingChild(n); getPaths(tree, path, expanded, list); } } }