Java JTree Expand expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList expansionStateStrings)

Here you can find the source of expandByExpansionStateStrings(final JTree tree, final TreePath parent, ArrayList expansionStateStrings)

Description

expand By Expansion State Strings

License

Apache License

Declaration

private static void expandByExpansionStateStrings(final JTree tree, final TreePath parent,
            ArrayList<String> expansionStateStrings) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.Enumeration;

import javax.swing.JTree;

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

public class Main {
    public static void expandByExpansionStateStrings(JTree tree, ArrayList<String> expansionStateStrings) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        tree.cancelEditing();//from w w  w .j  av a  2  s.  c  o  m
        expandByExpansionStateStrings(tree, new TreePath(root), expansionStateStrings);
    }

    private static void expandByExpansionStateStrings(final JTree tree, final TreePath parent,
            ArrayList<String> expansionStateStrings) {
        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);
                expandByExpansionStateStrings(tree, path, expansionStateStrings);
            }
        }

        if (isTreePathContainedInExpansionState(parent, expansionStateStrings)) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }

    private static boolean isTreePathContainedInExpansionState(TreePath treePath,
            ArrayList<String> expansionStateStrings) {
        if (treePath != null && expansionStateStrings != null) {
            return expansionStateStrings.contains(treePath.toString());
        }
        return false;
    }
}

Related

  1. expandAllNodes(JTree tree)
  2. expandAllNodes(JTree tree)
  3. expandAllNodes(JTree tree, int startingIndex, int rowCount)
  4. expandAllNodes(JTree tree, int startingIndex, int rowCount)
  5. expandAllNodes(JTree tree, TreeNode node)
  6. expandFirstNode(JTree tree)
  7. expandFirstNodeAtLevel(int level, JTree tree)
  8. expandFully(JTree tree, TreePath path)
  9. expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row, int depth)