Java JTree Expand expandOrCollapseAllRows(final JTree tree, final boolean expand)

Here you can find the source of expandOrCollapseAllRows(final JTree tree, final boolean expand)

Description

Expand or collapse every node in the given tree.

License

LGPL

Parameter

Parameter Description
tree The tree to expand or collapse.
expand If <code>true</code>, then the given <code>tree</code> will be expanded.

Exception

Parameter Description
IllegalArgumentExceptionIf <code>tree == null</code>.

Declaration

public static final void expandOrCollapseAllRows(final JTree tree, final boolean expand)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/**//from w w w  . j  a va 2 s.c o m
 * Copyright (C) 2013, University of Manchester and University of Southampton
 *
 * Licensed under the GNU Lesser General Public License v2.1
 * See the "LICENSE" file that is distributed with the source code for license terms. 
 */

import java.util.Enumeration;

import javax.swing.JTree;

import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class Main {
    /**
     * Expand or collapse every node in the given <code>tree</code>.
     * 
     * @param tree  The tree to expand or collapse.
     * @param expand  If <code>true</code>, then the given <code>tree</code> will be expanded.
     * @throws IllegalArgumentException  If <code>tree == null</code>.
     */
    public static final void expandOrCollapseAllRows(final JTree tree, final boolean expand)
            throws IllegalArgumentException {
        if (tree == null) {
            throw new IllegalArgumentException(new NullPointerException("tree"));
        }

        expandOrCollapseAllRows(tree, new TreePath(tree.getModel().getRoot()), expand);
    }

    /**
     * Expand or collapse nodes in the given <code>tree</code> that match the given <code>path</code>.
     * 
     * @param tree  The tree to expand or collapse.
     * @param path  The tree path.
     * @param expand  If <code>true</code>, then the given <code>tree</code> will be expanded. 
     * @throws IllegalArgumentException  If <code>tree == null || path == null</code>.
     */
    private static final void expandOrCollapseAllRows(final JTree tree, final TreePath path, final boolean expand)
            throws IllegalArgumentException {
        if (tree == null) {
            throw new IllegalArgumentException(new NullPointerException("tree"));
        } else if (path == null) {
            throw new IllegalArgumentException(new NullPointerException("path"));
        }

        // Get the last component of the given 'path' (should be a tree node.)
        final TreeNode node = (TreeNode) path.getLastPathComponent();

        // Call this method for each child node. 
        if (node.getChildCount() > 0) {
            @SuppressWarnings("unchecked")
            final Enumeration<TreeNode> e = node.children();

            while (e.hasMoreElements()) {
                expandOrCollapseAllRows(tree, path.pathByAddingChild(e.nextElement()), expand);
            }
        }

        // Expand or collapse the given 'tree'.
        if (expand) {
            tree.expandPath(path);
        } else {
            tree.collapsePath(path);
        }
    }
}

Related

  1. expandFirstNodeAtLevel(int level, JTree tree)
  2. expandFully(JTree tree, TreePath path)
  3. expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)
  4. expandLevels(JTree tree, int levels, boolean expand)
  5. expandNodesAtLevel(int level, JTree tree, TreePath parent)
  6. expandOrCollapseJTree(JTree tree, boolean expand)
  7. expandPath(JTree tree, TreePath tp)
  8. expandPathOnEdt(final JTree tree, final TreePath path)
  9. expandPaths(JTree tree, Collection paths)