Java JTree Node searchForNode(TreeNode node, String query)

Here you can find the source of searchForNode(TreeNode node, String query)

Description

search For Node

License

Open Source License

Parameter

Parameter Description
root The root node of the tree.
query the the label of the target node.

Return

the path from the root node to the selected node if exists, null otherwise.

Declaration

public static TreePath searchForNode(TreeNode node, String query) 

Method Source Code


//package com.java2s;
/*/*from  www .j  av  a  2s  .  c  o m*/
 * ACIDE - A Configurable IDE
 * Official web site: http://acide.sourceforge.net
 * 
 * Copyright (C) 2007-2014
 * Authors:
 *       - Fernando S?enz P?rez (Team Director).
 *      - Version from 0.1 to 0.6:
 *         - Diego Cardiel Freire.
 *         - Juan Jos? Ortiz S?nchez.
 *          - Delf?n Rup?rez Ca?as.
 *      - Version 0.7:
 *          - Miguel Mart?n L?zaro.
 *      - Version 0.8:
 *         - Javier Salcedo G?mez.
 *      - Version from 0.9 to 0.11:
 *         - Pablo Guti?rrez Garc?a-Pardo.
 *         - Elena Tejeiro P?rez de ?greda.
 *         - Andr?s Vicente del Cura.
 *      - Version from 0.12 to 0.16
 *         - Sem?ramis Guti?rrez Quintana
 *         - Juan Jes?s Marqu?s Ortiz
 *         - Fernando Ord?s Lorente
 *      - Version 0.17
 *         - Sergio Dom?nguez Fuentes
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Enumeration;

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

public class Main {
    /**
     * @param root The root node of the tree.
     * @param query the the label of the target node.
     * @return the path from the root node to the selected node if exists, null otherwise.
     */
    public static TreePath searchForNode(TreeNode node, String query) {
        //Searches for the target node at the level
        if (node.toString().startsWith(query))
            return new TreePath(node);
        if (node.isLeaf())
            return null;
        Enumeration<TreeNode> e = (Enumeration<TreeNode>) node.children();
        // Searches for the target node on the children of the node 
        while (e.hasMoreElements()) {
            TreePath path = searchForNode(e.nextElement(), query);
            if (path != null) {
                TreePath parentPath = new TreePath(node);
                for (int i = 0; i < path.getPathCount(); i++)
                    parentPath = parentPath.pathByAddingChild(path.getPathComponent(i));
                return parentPath;
            }
        }
        return null;
    }
}

Related

  1. listTree(TreeNode node, String prefix, String lineSeparator)
  2. printTree(final DefaultMutableTreeNode tree, final String indent)
  3. recursiveTreeNode(DefaultMutableTreeNode node)
  4. refreshNode(final JTree tree, final TreeNode node)
  5. RemoveChildren(DefaultMutableTreeNode node)
  6. setTreeSelectedNode(JTree tree, TreeNode node)
  7. sort(DefaultMutableTreeNode node)
  8. sortNode(DefaultMutableTreeNode parent, Comparator comparator)
  9. sortTreeDepthFirst(DefaultMutableTreeNode root, Comparator comparator)