Java JTree Model getTreeIndents(TreeModel in)

Here you can find the source of getTreeIndents(TreeModel in)

Description

return the size of a tree starting at a specific root

License

Apache License

Parameter

Parameter Description
in - the tree model

Return

- the indents 0 = root

Declaration

public static int[] getTreeIndents(TreeModel in) 

Method Source Code

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

import javax.swing.tree.*;

public class Main {
    /**// www.  ja va  2  s .  co  m
    return the size of a tree starting at a specific root
    @param in - the tree model
    @return - the indents 0 = root
    */
    public static int[] getTreeIndents(TreeModel in) {
        int[] ret = new int[getTreeModelSize(in)];
        setTreeIndents(in, in.getRoot(), ret, 0, 0);
        return (ret);
    }

    /**
    return the size of a tree model
    @param in - the tree model
    @return - the size >= 0
    */
    public static int getTreeModelSize(TreeModel in) {
        Object root = in.getRoot();
        if (root == null)
            return (0);
        return (getTreeModelSize(in, root));
    }

    /**
    return the size of a tree starting at a specific root
    @param in - the tree model
    @parem root - starting node - non-null
    @return - the size >= 1
    */
    protected static int getTreeModelSize(TreeModel in, Object root) {
        int ret = 1; // add one for root
        for (int i = 0; i < in.getChildCount(root); i++)
            ret += getTreeModelSize(in, in.getChild(root, i));
        return (ret);
    }

    /**
    return array of indents for a tree 
    @param in - the tree model
    @parem root - starting node - non-null
    @parem items - array of indents
    @parem level - current indent level 0 = root
    @parem index - index on entry
    @return - index of first unused access
    */
    protected static int setTreeIndents(TreeModel in, Object root, int[] items, int level, int index) {
        items[index++] = level;
        for (int i = 0; i < in.getChildCount(root); i++)
            index = setTreeIndents(in, in.getChild(root, i), items, level + 1, index);
        return (index);
    }
}

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. getAllTreeNodes(TreeModel model)
  6. getTreeModelSize(TreeModel in)
  7. isTreePathInModel(TreeModel treeModel, TreePath treePath)
  8. logTreeNodes(TreeModel model, Object node, String indent)
  9. nodesChanged(DefaultTreeModel rightTreeModel)