001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.tree;
017
018 import java.util.Enumeration;
019
020 import javax.swing.JTree;
021 import javax.swing.tree.TreeModel;
022 import javax.swing.tree.TreeNode;
023 import javax.swing.tree.TreePath;
024
025 public class TreeUtils {
026 public static String buildFormattedTreePath(TreeObject treeObject, boolean includeRoot, String delimSymbol) {
027 if (treeObject == null) {
028 return "";
029 }
030 TreeObject parent = treeObject;
031 if (parent == null) {
032 return "";
033 }
034 StringBuffer buffer = new StringBuffer();
035 if (delimSymbol == null) {
036 delimSymbol = "/";
037 }
038 boolean firstTime = true;
039 while (parent != null) {
040 TreeObject p = parent.getParent();
041 if (p == null) {
042 if (includeRoot) {
043 buffer.insert(0, "/" + parent.getDisplayName() + "/");
044 }
045 }
046 else {
047 if (firstTime) {
048 buffer.insert(0, parent.getDisplayName());
049 firstTime = false;
050 }
051 else {
052 buffer.insert(0, parent.getDisplayName() + delimSymbol);
053 }
054 }
055 parent = p;
056 }
057 return buffer.toString();
058 }
059
060 // If expand is true, expands all nodes in the tree.
061 // Otherwise, collapses all nodes in the tree.
062 public static void expandAll(JTree tree, boolean expand) {
063 TreeNode root = (TreeNode)tree.getModel().getRoot();
064 // Traverse tree from root
065 expandAll(tree, new TreePath(root), expand);
066 }
067
068 // If expand is true, expands all nodes in the tree.
069 // Otherwise, collapses all nodes in the tree.
070 public static void expandLevels(JTree tree, int levels, boolean expand) {
071 TreeModel model = tree.getModel();
072 if (model == null) {
073 return;
074 }
075 TreeNode root = (TreeNode)model.getRoot();
076 // Traverse tree from root
077 expandLevels(tree, new TreePath(root), levels, expand);
078 }
079
080 public static void expandLevels(JTree tree, TreePath parent, int levels, boolean expand) {
081 // Traverse children
082 TreeNode node = (TreeNode)parent.getLastPathComponent();
083 if (node.getChildCount() >= 0) {
084 for (Enumeration e = node.children(); e.hasMoreElements();) {
085 TreeNode n = (TreeNode)e.nextElement();
086 TreePath path = parent.pathByAddingChild(n);
087 if (levels > 0) {
088 expandLevels(tree, path, --levels, expand);
089 }
090 }
091 }
092
093 // Expansion or collapse must be done bottom-up
094 if (expand) {
095 tree.expandPath(parent);
096 }
097 else {
098 tree.collapsePath(parent);
099 }
100 }
101
102 public static void expandAll(JTree tree, TreePath parent, boolean expand) {
103 // Traverse children
104 TreeNode node = (TreeNode)parent.getLastPathComponent();
105 if (node.getChildCount() >= 0) {
106 for (Enumeration e = node.children(); e.hasMoreElements();) {
107 TreeNode n = (TreeNode)e.nextElement();
108 TreePath path = parent.pathByAddingChild(n);
109 expandAll(tree, path, expand);
110 }
111 }
112
113 // Expansion or collapse must be done bottom-up
114 if (expand) {
115 tree.expandPath(parent);
116 }
117 else {
118 tree.collapsePath(parent);
119 }
120 }
121
122 }