Java JTree Path find2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName)

Here you can find the source of find2(JTree tree, TreePath parent, Object[] nodes, int depth, boolean byName)

Description

find

License

Open Source License

Declaration

private static TreePath find2(JTree tree, TreePath parent,
            Object[] nodes, int depth, boolean byName) 

Method Source Code

//package com.java2s;
/*/*from  w ww .  j a va2 s. co  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 {
    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. checkNode(TreePath path)
  2. createTreePath(TreeNode treeNode)
  3. createTreePathFromTreeNode(TreeNode treeNode)
  4. doNode(JTree tree, TreeNode parentNode, TreePath parentPath)
  5. findOrAddNode(JTree tree, TreePath parent, String packageName)
  6. findTreePath(TreePath path, TreeNode node, String pathName)
  7. findV2MetaTreeNode( String nodeXmlPath)
  8. getDepth(JTree tree)