Here you can find the source of searchPath(TreeModel model, TreePath oldPath)
equals()
) as the ones specified in the old path.
public static TreePath searchPath(TreeModel model, TreePath oldPath)
//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; } }