Java JTree Node getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root)

Here you can find the source of getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root)

Description

Get a flat list of all the treeNodes at or below a certain level, where the userObject is of a certain class, starting from a particular root, or the topmost treeNode if root is null.

License

LGPL

Parameter

Parameter Description
model tree's model
startLevel method will not look at anything above this level
clazz if not null, only get these types of objects
root if not null, where to start recursing from

Return

flattened list of treeNodes

Declaration

public static List getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root) 

Method Source Code

//package com.java2s;
/*/*  ww w.ja v a2  s  . com*/
Strandz LGPL - an API that matches the user to the data.
Copyright (C) 2007 Chris Murphy
    
Strandz LGPL is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
    
The authors can be contacted via www.strandz.org
*/

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.TreeModel;

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static int treeNodeLevel;

    /**
     * Get a flat list of all the treeNodes where the
     * userObject is of a certain class
     *
     * @param model
     * @param clazz
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model, Class clazz) {
        return getAllTreeNodes(model, 1, clazz, null);
    }

    /**
     * Get a flat list of all the treeNodes where the
     * userObject is of a certain class, starting from the passed in treeNode
     *
     * @param model tree's model
     * @param clazz if not null, only get these types of objects
     * @param root  if not null, where to start recursing from
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model, Class clazz, DefaultMutableTreeNode root) {
        return getAllTreeNodes(model, 1, clazz, root);
    }

    /**
     * Get a flat list of all the treeNodes
     *
     * @param model tree's model
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model) {
        return getAllTreeNodes(model, 1, null, null);
    }

    /**
     * Get a flat list of all the treeNodes at or below a certain level, starting
     * from the topmost treeNode.
     *
     * @param model      tree's model
     * @param startLevel method will not look at anything above this level
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model, int startLevel) {
        return getAllTreeNodes(model, startLevel, null, null);
    }

    /**
     * Get a flat list of all the treeNodes at or below a certain level, where the
     * userObject is of a certain class, starting from the topmost treeNode.
     *
     * @param model      tree's model
     * @param startLevel method will not look at anything above this level
     * @param clazz      if not null, only get these types of objects
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model, int startLevel, Class clazz) {
        return getAllTreeNodes(model, startLevel, clazz, null);
    }

    /**
     * Get a flat list of all the treeNodes at or below a certain level, where the
     * userObject is of a certain class, starting from a particular root, or the
     * topmost treeNode if root is null.
     *
     * @param model      tree's model
     * @param startLevel method will not look at anything above this level
     * @param clazz      if not null, only get these types of objects
     * @param root       if not null, where to start recursing from
     * @return flattened list of treeNodes
     */
    public static List getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root) {
        List nodesResult = new ArrayList();
        // DefaultTreeModel model = (DefaultTreeModel)tree.getModel();
        // Err.pr( "^^ model is type: " + model.getClass().getName());
        if (root == null) {
            root = (DefaultMutableTreeNode) model.getRoot();
        }
        treeNodeLevel = 0;
        return getAllTreeNodes(model, root, nodesResult, startLevel, clazz);
    }

    private static List getAllTreeNodes(TreeModel model, DefaultMutableTreeNode parent, List nodesResult,
            int startLevel, Class clazz) {
        treeNodeLevel++;
        if (parent != null) {
            for (int i = 0; i < model.getChildCount(parent); i++) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getChild(parent, i);
                nodesResult = getAllTreeNodes(model, node, nodesResult, startLevel, clazz);
                if (treeNodeLevel >= startLevel) {
                    if (clazz == null || node.getUserObject().getClass() == clazz) {
                        // Err.pr( "At treeNodeLevel: " + treeNodeLevel + " will add " + node);
                        nodesResult.add(node);
                    }
                }
            }
        }
        treeNodeLevel--;
        return nodesResult;
    }
}

Related

  1. export(File file, DefaultMutableTreeNode node)
  2. findFirstTreeNodeMatchUserObject(DefaultMutableTreeNode treeNode, Object userObject)
  3. findTreeNodesWithLowestLevel(List multipleTreeNodes)
  4. findTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode, String nodeXmlPath)
  5. getAllChildCount(TreeNode node)
  6. getAllUserObject(TreeNode node, Set userObjectSet)
  7. getLevel(TreeNode treeNode)
  8. getMutableTreeNodes(TreeNode[] path)
  9. getNodeAt(final JTree tree, final int x, final int y)