Java JTree Model getAllTreeNodes(TreeModel model)

Here you can find the source of getAllTreeNodes(TreeModel model)

Description

Get a flat list of all the treeNodes

License

LGPL

Parameter

Parameter Description
model tree's model

Return

flattened list of treeNodes

Declaration

public static List getAllTreeNodes(TreeModel model) 

Method Source Code

//package com.java2s;
/*// w ww.  j a v a  2  s .  co m
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. appendTreeNodes(TreeModel model, Object node, StringBuilder builder, String indent)
  2. findChildNode(TreeModel model, Object node, String text)
  3. findChildNode(TreeModel model, Object node, String text)
  4. findTreeNode(TreeModel model, Object obj)
  5. getTreeIndents(TreeModel in)
  6. getTreeModelSize(TreeModel in)
  7. isTreePathInModel(TreeModel treeModel, TreePath treePath)
  8. logTreeNodes(TreeModel model, Object node, String indent)