Java JTree Path getTreePath(TreePath path, JTree tree, boolean compareOnlyLabels)

Here you can find the source of getTreePath(TreePath path, JTree tree, boolean compareOnlyLabels)

Description

get Tree Path

License

BSD License

Declaration

public static TreePath getTreePath(TreePath path, JTree tree, boolean compareOnlyLabels) 

Method Source Code

//package com.java2s;
/*   TouchStone design platform is a software to design protocols for lab        *
 *   experiments. It is published under the terms of a BSD license               *
 *   (see details below)                                                         *
 *   Author: Caroline Appert (appert@lri.fr)                                     *
 *   Copyright (c) 2010 Caroline Appert and INRIA, France.                       *
 *   TouchStone design platform reuses parts of an early version which were      *
 *   programmed by Matthis Gilbert.                                              *
 *********************************************************************************/

import java.util.Enumeration;

import javax.swing.JTree;

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

public class Main {
    public static TreePath getTreePath(TreePath path, JTree tree, boolean compareOnlyLabels) {
        Object[] pathElements = path.getPath();
        TreePath result = null;//from  www .  j a  va 2s  . c om
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        if ((root.toString() == null && pathElements[0].toString() == null)
                || (root.toString().compareTo(pathElements[0].toString()) == 0))
            result = new TreePath(root);

        if (result == null || pathElements.length == 1)
            return result;

        TreePath[] potentialResult = new TreePath[1];
        potentialResult[0] = result;
        boolean found = searchUnexpandedPath(tree, path, 1, root, potentialResult, compareOnlyLabels);
        if (found)
            return potentialResult[0];
        else
            return null;
    }

    public static boolean searchUnexpandedPath(JTree tree, TreePath path, int index, TreeNode node,
            TreePath[] result, boolean compareOnlyLabels) {
        Object[] pathElements = path.getPath();
        if (index >= pathElements.length) {
            return !tree.isExpanded(result[0]);
        }
        Object elementToSearch = pathElements[index];
        Enumeration<?> enumeration = node.children();
        while (enumeration.hasMoreElements()) {
            TreeNode next = (TreeNode) enumeration.nextElement();
            if ((compareOnlyLabels && next.toString().compareTo(elementToSearch.toString()) == 0)
                    || (!compareOnlyLabels && next.equals(elementToSearch))) {
                TreePath[] potentialResult = new TreePath[1];
                potentialResult[0] = result[0].pathByAddingChild(next);
                if (searchUnexpandedPath(tree, path, index + 1, next, potentialResult, compareOnlyLabels)) {
                    result[0] = potentialResult[0];
                    return true;
                }
            }
        }
        return false;
    }
}

Related

  1. getPath(TreeNode treeNode)
  2. getPathStr(TreeNode[] projectNodes)
  3. getTree(List paths)
  4. getTreePath(TreeNode treeNode)
  5. getTreePath(TreeNode treeNode)
  6. getTreePathString(TreePath path)
  7. getUpdatedTreePath(TreePath[] paths, JTree tree)
  8. getUserObject(TreePath path)
  9. getUserObject(TreePath path)