Java JTree findByName(JTree tree, String[] names)

Here you can find the source of findByName(JTree tree, String[] names)

Description

find By Name

License

Open Source License

Declaration

public static TreePath findByName(JTree tree, String[] names) 

Method Source Code

//package com.java2s;
/*//  w  ww .j  a  v a 2s  .  c o m
 * Palo Open Office Calc AddIn
 * Copyright (C) 2008 PalOOCa Team,  Tensegrity Software GmbH, 2009

 * The software is licensed under an Open-Source License (GPL).
 * If you want to redistribute the software you must observe the regulations of
 * the GPL . If you want to redistribute the software without the
 * restrictions of the GPL, you have to contact Tensegrity Software GmbH
 * (Tensegrity) for written consent to do so.
 * Tensegrity may offer commercial licenses for redistribution (Dual Licensing)
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.util.Enumeration;

import javax.swing.JTree;

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

public class Main {
    public static TreePath findByName(JTree tree, String[] names) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        return find2(tree, new TreePath(root), names, 0, true);
    }

    private static TreePath find2(JTree tree, TreePath parent,
            Object[] nodes, int depth, boolean byName) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        Object o = node;

        if (o == null)
            return null;

        // If by name, convert node to a string
        if (byName) {
            o = o.toString();
        }

        //        if (o == null)
        //            return null;

        System.err.println(o + " : " + nodes[depth]);

        // If equal, go down the branch
        if ((depth == 0) || o.equals(nodes[depth])) {
            // If at end, return match
            if (depth == nodes.length - 1) {
                return parent;
            }

            // Traverse children
            if (node.getChildCount() >= 0) {
                for (Enumeration e = node.children(); e.hasMoreElements();) {
                    TreeNode n = (TreeNode) e.nextElement();
                    TreePath path = parent.pathByAddingChild(n);
                    TreePath result = find2(tree, path, nodes, depth + 1,
                            byName);
                    // Found a match
                    if (result != null) {
                        return result;
                    }
                }
            }
        }

        // No match at this branch
        return null;
    }
}

Related

  1. autoScrollIsDesirable(JTree tree)
  2. clearTreeIcon(JTree tree)
  3. correctRowHeight(JTree tree)
  4. disposeExpressionsTree()
  5. enableAutoExpansion(final JTree tree)
  6. findTree(Container container)
  7. getCurrentWidth(JTree tree, int row)
  8. getExpansionState(JTree tree, int row)
  9. getProjectTree()