Java JTree Path getUpdatedTreePath(TreePath[] paths, JTree tree)

Here you can find the source of getUpdatedTreePath(TreePath[] paths, JTree tree)

Description

get Updated Tree Path

License

Apache License

Declaration

public static TreePath[] getUpdatedTreePath(TreePath[] paths, JTree tree) 

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 TreePath[] getUpdatedTreePath(TreePath[] paths, JTree tree) {
        if (paths == null || tree == null) {
            return null;
        }/*from   w  w w. ja va2 s .c  o  m*/

        TreeNode root = (TreeNode) tree.getModel().getRoot();
        ArrayList<TreePath> result = new ArrayList<TreePath>();
        for (int i = 0; i < paths.length; i++) {
            TreePath updatedPath = findTreePath(new TreePath(root), root, getTreePathString(paths[i]));
            if (updatedPath != null) {
                result.add(updatedPath);
            }
        }
        return result.toArray(new TreePath[result.size()]);
    }

    private static TreePath findTreePath(TreePath path, TreeNode node, String pathName) {
        if (path == null || pathName == null || node == null) {
            return null;
        }

        if (pathName.equals(getTreePathString(path))) {
            return path;
        }

        if (node.getChildCount() >= 0) {
            for (Enumeration<?> e = node.children(); e.hasMoreElements();) {
                TreeNode child = (TreeNode) e.nextElement();
                TreePath childPath = path.pathByAddingChild(child);
                TreePath childMatch = findTreePath(childPath, child, pathName);
                if (childMatch != null) {
                    return childMatch;
                }
            }
        }

        return null;
    }

    private static String getTreePathString(TreePath path) {
        return path.toString();
    }
}

Related

  1. getTree(List paths)
  2. getTreePath(TreeNode treeNode)
  3. getTreePath(TreeNode treeNode)
  4. getTreePath(TreePath path, JTree tree, boolean compareOnlyLabels)
  5. getTreePathString(TreePath path)
  6. getUserObject(TreePath path)
  7. getUserObject(TreePath path)
  8. isDescendant(TreePath path1, final TreePath path2)
  9. isDescendant(TreePath path1, final TreePath path2)