Java JTree Model searchPath(TreeModel model, TreePath oldPath)

Here you can find the source of searchPath(TreeModel model, TreePath oldPath)

Description

Search for a path in the specified tree model, whose nodes have the same name (compared using equals()) as the ones specified in the old path.

License

Apache License

Return

a new path for the specified model, or null if no such path could be found.

Declaration

public static TreePath searchPath(TreeModel model, TreePath oldPath) 

Method Source Code

//package com.java2s;
/*/*from  ww w.  j  ava 2 s. co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public class Main {
    /**
     * Search for a path in the specified tree model, whose nodes have
     * the same name (compared using <code>equals()</code>)
     * as the ones specified in the old path.
     *
     * @return a new path for the specified model, or null if no such path
     *         could be found.
     */
    public static TreePath searchPath(TreeModel model, TreePath oldPath) {
        Object treenode = model.getRoot();
        Object[] oldPathNodes = oldPath.getPath();
        TreePath newPath = new TreePath(treenode);
        for (int i = 0; i < oldPathNodes.length; ++i) {
            Object oldPathNode = oldPathNodes[i];
            if (treenode.toString().equals(oldPathNode.toString())) {
                if (i == (oldPathNodes.length - 1)) {
                    return newPath;
                } else {
                    if (model.isLeaf(treenode)) {
                        return null; // not found
                    } else {
                        int count = model.getChildCount(treenode);
                        boolean foundChild = false;
                        for (int j = 0; j < count; ++j) {
                            Object child = model.getChild(treenode, j);
                            if (child.toString().equals(oldPathNodes[i + 1].toString())) {
                                newPath = newPath.pathByAddingChild(child);
                                treenode = child;
                                foundChild = true;
                                break;
                            }
                        }
                        if (!foundChild) {
                            return null; // couldn't find child with same name
                        }
                    }
                }
            }
        }
        return null;
    }
}

Related

  1. isTreePathInModel(TreeModel treeModel, TreePath treePath)
  2. logTreeNodes(TreeModel model, Object node, String indent)
  3. nodesChanged(DefaultTreeModel rightTreeModel)
  4. redrawAllTreeCells(DefaultTreeModel treeModel)
  5. redrawAllTreeCellsRec(DefaultTreeModel treeModel, TreeNode parent)
  6. setTreeObjects(TreeModel in, Object root, Object[] items, int index)
  7. setTreePaths(TreeModel in, Object root, TreePath[] items, Object[] path, int index)
  8. showTreeComponent(String blank, TreeModel model, Object node)