KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > GUITreeTools


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.utils;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.swing.JTree JavaDoc;
20 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
21 import javax.swing.tree.TreeNode JavaDoc;
22 import javax.swing.tree.TreePath JavaDoc;
23
24
25 /**
26  * <p>Title: JTree Tools</p>
27  * <p>Description: This class offers backend handling methods to handle
28  * JTree trees, as well as generate flat views that are useful for HTML
29  * output.</p>
30  * <p>Copyright: Copyright (c) 2002</p>
31  * <p>Company: </p>
32  * @author Khue Nguyen
33  * @version 1.0
34  */

35 public class GUITreeTools {
36
37     /**
38      * Returns an array list representation ( simplier to work with when generating
39      * an html view ) of a tree starting from a given node.
40      *
41      * @param tree the tree to convert to a flat array list
42      * @param node the starting node in the tree
43      * @return an ArrayList containing DefaultMutableTreeNode objects that
44      * represent a flat view of the tree in it's current visible state
45      */

46     public static ArrayList JavaDoc getFlatTree(JTree JavaDoc tree, DefaultMutableTreeNode JavaDoc node){
47        ArrayList JavaDoc values = new ArrayList JavaDoc();
48        if ( node != null ){
49            values.add(node);
50            if ( tree.isExpanded(new TreePath JavaDoc(node.getPath())) ){
51                Enumeration JavaDoc childrens = node.children();
52                while( childrens.hasMoreElements() ){
53                    DefaultMutableTreeNode JavaDoc childNode =
54                            (DefaultMutableTreeNode JavaDoc)childrens.nextElement();
55                    ArrayList JavaDoc childDescendants = getFlatTree(tree,childNode);
56                    values.addAll(childDescendants);
57                }
58            }
59        }
60        return values;
61     }
62
63     /**
64      * @param node the node for which to calculate the levels
65      * @return an ArrayList containing Integer that are the level numbers for
66      * which we should render a vertical line to connect a child node to its
67      * parent.
68      */

69     public static ArrayList JavaDoc getLevelsWithVerticalLine(DefaultMutableTreeNode JavaDoc node){
70         ArrayList JavaDoc values = new ArrayList JavaDoc();
71         TreeNode JavaDoc[] treeNodes = node.getPath();
72         for( int i=0; i<treeNodes.length; i++ ){
73             DefaultMutableTreeNode JavaDoc n = (DefaultMutableTreeNode JavaDoc)treeNodes[i];
74             DefaultMutableTreeNode JavaDoc parentNode =
75                     (DefaultMutableTreeNode JavaDoc)n.getParent();
76             if ( parentNode!=null && !n.equals(parentNode.getLastChild()) ){
77                 values.add(new Integer JavaDoc(i));
78             }
79         }
80
81         return values;
82     }
83
84     /**
85      * Update GUI Tree changes. This method looks for two parameters in the
86      * request object : guitree and nodeindex. The guitree parameter may have
87      * 3 values : expand, expandall and collapse. The nodeindex indicates the
88      * node index for which to perform the operation.
89      *
90      * @param tree the tree to modify according to the parameters set in the
91      * request object
92      * @param request the request object containing the parameters that
93      * indicate which tree modifications should be performed.
94      * @todo To be completed
95      */

96     public static void updateGUITree(JTree JavaDoc tree,
97                                      HttpServletRequest JavaDoc request){
98
99         String JavaDoc treeOperation = request.getParameter("guitree");
100         String JavaDoc nodeIndex = request.getParameter("nodeindex");
101
102         if ( tree != null && treeOperation != null && nodeIndex != null ){
103
104             DefaultMutableTreeNode JavaDoc rootNode =
105                     (DefaultMutableTreeNode JavaDoc)tree.getModel().getRoot();
106             if ( rootNode != null ){
107                 ArrayList JavaDoc nodeList =
108                         GUITreeTools.getFlatTree(tree,rootNode);
109                 DefaultMutableTreeNode JavaDoc node
110                         = (DefaultMutableTreeNode JavaDoc)
111                         nodeList.get(Integer.parseInt(nodeIndex));
112                 if ( treeOperation.equals("expand") ){
113                     tree.expandPath(new TreePath JavaDoc(node.getPath()));
114                 } else if ( treeOperation.equals("expandall") ){
115                     expandAllPath(tree,node);
116                 } else if ( treeOperation.equals("collapse") ){
117                     tree.collapsePath(new TreePath JavaDoc(node.getPath()));
118                 }
119             }
120         }
121     }
122
123     /**
124      * Expands all the paths under a given node.
125      * @param tree the tree on which to expand the node and it's children
126      * @param node the starting node under which to expand all the paths.
127      */

128     public static void expandAllPath(JTree JavaDoc tree, DefaultMutableTreeNode JavaDoc node){
129         if ( !node.isLeaf() || node.children().hasMoreElements() ){
130             tree.expandPath(new TreePath JavaDoc(node.getPath()));
131         }
132         Enumeration JavaDoc children = node.children();
133         DefaultMutableTreeNode JavaDoc childNode = null;
134         while( children.hasMoreElements() ){
135             childNode = (DefaultMutableTreeNode JavaDoc)children.nextElement();
136             expandAllPath(tree,childNode);
137         }
138     }
139 }
140
141
Popular Tags