01: package org.enhydra.jawe.components;
02:
03: import javax.swing.JTree;
04: import javax.swing.tree.TreeModel;
05: import javax.swing.tree.TreeNode;
06: import javax.swing.tree.TreePath;
07:
08: /**
09: * Utility class for handling the tree.
10: *
11: * @author Zoran Milakovic
12: * @author Sasa Bojanic
13: */
14: public class XPDLTreeUtil {
15:
16: /**
17: * Expands/Collapse specified tree to a certain level.
18: *
19: * @param tree jtree to expand to a certain level
20: * @param level the level of expansion
21: */
22: public static void expandOrCollapsToLevel(JTree tree,
23: TreePath treePath, int level, boolean expand) {
24: try {
25: expandOrCollapsePath(tree, treePath, level, 0, expand);
26: } catch (Exception e) {
27: e.printStackTrace();
28: //do nothing
29: }
30: }
31:
32: public static void expandOrCollapsePath(JTree tree,
33: TreePath treePath, int level, int currentLevel,
34: boolean expand) {
35: // System.err.println("Exp level "+currentLevel+", exp="+expand);
36: if (expand && level <= currentLevel && level > 0)
37: return;
38:
39: TreeNode treeNode = (TreeNode) treePath.getLastPathComponent();
40: TreeModel treeModel = tree.getModel();
41: if (treeModel.getChildCount(treeNode) >= 0) {
42: for (int i = 0; i < treeModel.getChildCount(treeNode); i++) {
43: TreeNode n = (TreeNode) treeModel.getChild(treeNode, i);
44: TreePath path = treePath.pathByAddingChild(n);
45: expandOrCollapsePath(tree, path, level,
46: currentLevel + 1, expand);
47: }
48: if (!expand && currentLevel < level)
49: return;
50: }
51: if (expand) {
52: tree.expandPath(treePath);
53: // System.err.println("Path expanded at level "+currentLevel+"-"+treePath);
54: } else {
55: tree.collapsePath(treePath);
56: // System.err.println("Path collapsed at level "+currentLevel+"-"+treePath);
57: }
58: }
59:
60: }
|