Java JTree Node findTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode, String nodeXmlPath)

Here you can find the source of findTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode, String nodeXmlPath)

Description

find Tree Node With Xml Path

License

BSD License

Declaration

public static DefaultMutableTreeNode findTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode,
            String nodeXmlPath) 

Method Source Code


//package com.java2s;
/*L//from   w  ww  .  j  a v a 2s .c o  m
 * Copyright SAIC, SAIC-Frederick.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caadapter/LICENSE.txt for details.
 */

import javax.swing.tree.DefaultMutableTreeNode;

import java.util.StringTokenizer;

public class Main {
    public static DefaultMutableTreeNode findTreeNodeWithXmlPath(DefaultMutableTreeNode treeNode,
            String nodeXmlPath) {
        if (nodeXmlPath == null) {
            System.out.println("UIHelper.findTreeNodeWithXmlPath()..invalid node to search:" + nodeXmlPath);
            return null;
        }

        StringTokenizer st = new StringTokenizer(nodeXmlPath, "/@");
        boolean foundRoot = false;
        DefaultMutableTreeNode e = treeNode;
        while (st.hasMoreTokens() && e != null) {
            String tmp = st.nextToken();
            if (!foundRoot) {
                if (e.toString().equals(tmp)) {
                    foundRoot = true;
                    continue;
                } else
                    continue;
            } else {
                int childCount = e.getChildCount();
                DefaultMutableTreeNode found = null;
                for (int i = 0; i < childCount; i++) {
                    DefaultMutableTreeNode child = (DefaultMutableTreeNode) e.getChildAt(i);
                    if (child.toString().equals(tmp)) {
                        found = child;
                        break;
                    }
                }
                if (found == null) {
                    return null;
                } else {
                    e = found;
                }
            }
        }

        if (!foundRoot)
            return null;
        return e;
    }
}

Related

  1. expandTree(final JTree tree, final DefaultMutableTreeNode node)
  2. expandTreeRecurser(JTree tree, DefaultMutableTreeNode node)
  3. export(File file, DefaultMutableTreeNode node)
  4. findFirstTreeNodeMatchUserObject(DefaultMutableTreeNode treeNode, Object userObject)
  5. findTreeNodesWithLowestLevel(List multipleTreeNodes)
  6. getAllChildCount(TreeNode node)
  7. getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root)
  8. getAllUserObject(TreeNode node, Set userObjectSet)
  9. getLevel(TreeNode treeNode)