Here you can find the source of findTreeNode(TreeModel model, Object obj)
Parameter | Description |
---|---|
model | tree's model |
obj | object we need to find the treeNode for |
public static DefaultMutableTreeNode findTreeNode(TreeModel model, Object obj)
//package com.java2s; /*/*from www .j av a2 s. c o m*/ Strandz LGPL - an API that matches the user to the data. Copyright (C) 2007 Chris Murphy Strandz LGPL is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. 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 Lesser 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 The authors can be contacted via www.strandz.org */ import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeModel; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { private static int treeNodeLevel; /** * Given an obj, find the treeNode that holds it. * * @param model tree's model * @param obj object we need to find the treeNode for * @return treeNode that has obj as its getUserObject() */ public static DefaultMutableTreeNode findTreeNode(TreeModel model, Object obj) { DefaultMutableTreeNode result = null; List nodesResult = new ArrayList(); //DefaultMutableTreeNode parent = (DefaultMutableTreeNode) model.getRoot(); nodesResult = getAllTreeNodes(model); for (Iterator iter = nodesResult.iterator(); iter.hasNext();) { DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) iter.next(); Object userObj = treeNode.getUserObject(); /* if(debug) { Err.pr( "Will cf userObj: " + userObj); Err.pr( "With obj: " + obj); if(userObj.toString().equals( "tfEmail")) { Err.debug(); } } */ if (obj.equals(userObj)) { result = treeNode; break; } } /* * Happens when navigate to a different Context which has a * different tree model. if(result == null) { Err.error( "Did not find a " + obj.getClass().getName() + "(has paneIndex gone to -1 ??)"); } */ return result; } /** * Get a flat list of all the treeNodes where the * userObject is of a certain class * * @param model * @param clazz * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model, Class clazz) { return getAllTreeNodes(model, 1, clazz, null); } /** * Get a flat list of all the treeNodes where the * userObject is of a certain class, starting from the passed in treeNode * * @param model tree's model * @param clazz if not null, only get these types of objects * @param root if not null, where to start recursing from * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model, Class clazz, DefaultMutableTreeNode root) { return getAllTreeNodes(model, 1, clazz, root); } /** * Get a flat list of all the treeNodes * * @param model tree's model * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model) { return getAllTreeNodes(model, 1, null, null); } /** * Get a flat list of all the treeNodes at or below a certain level, starting * from the topmost treeNode. * * @param model tree's model * @param startLevel method will not look at anything above this level * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model, int startLevel) { return getAllTreeNodes(model, startLevel, null, null); } /** * Get a flat list of all the treeNodes at or below a certain level, where the * userObject is of a certain class, starting from the topmost treeNode. * * @param model tree's model * @param startLevel method will not look at anything above this level * @param clazz if not null, only get these types of objects * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model, int startLevel, Class clazz) { return getAllTreeNodes(model, startLevel, clazz, null); } /** * Get a flat list of all the treeNodes at or below a certain level, where the * userObject is of a certain class, starting from a particular root, or the * topmost treeNode if root is null. * * @param model tree's model * @param startLevel method will not look at anything above this level * @param clazz if not null, only get these types of objects * @param root if not null, where to start recursing from * @return flattened list of treeNodes */ public static List getAllTreeNodes(TreeModel model, int startLevel, Class clazz, DefaultMutableTreeNode root) { List nodesResult = new ArrayList(); // DefaultTreeModel model = (DefaultTreeModel)tree.getModel(); // Err.pr( "^^ model is type: " + model.getClass().getName()); if (root == null) { root = (DefaultMutableTreeNode) model.getRoot(); } treeNodeLevel = 0; return getAllTreeNodes(model, root, nodesResult, startLevel, clazz); } private static List getAllTreeNodes(TreeModel model, DefaultMutableTreeNode parent, List nodesResult, int startLevel, Class clazz) { treeNodeLevel++; if (parent != null) { for (int i = 0; i < model.getChildCount(parent); i++) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) model.getChild(parent, i); nodesResult = getAllTreeNodes(model, node, nodesResult, startLevel, clazz); if (treeNodeLevel >= startLevel) { if (clazz == null || node.getUserObject().getClass() == clazz) { // Err.pr( "At treeNodeLevel: " + treeNodeLevel + " will add " + node); nodesResult.add(node); } } } } treeNodeLevel--; return nodesResult; } }