KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > tree > TreeHelpers


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.tree;
19
20 import org.apache.beehive.netui.util.logging.Logger;
21
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25
26
27 /**
28  * This class provides a set of static helper methods that deal with a trees.
29  */

30 public class TreeHelpers
31 {
32     private static final Logger logger = Logger.getInstance(TreeHelpers.class);
33
34     /**
35      * If this tree was selected or expanded this will handle that processing.
36      * @param treeId
37      * @param treeRoot
38      * @param request
39      */

40     public static void processTreeRequest(String JavaDoc treeId, TreeElement treeRoot, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
41     {
42         assert(treeId != null) : "parameter treeId must not be null.";
43         assert(treeRoot != null) : "parameter treeRoot must not be null.";
44         assert(request != null) : "paramater request must not be null.";
45
46         // check the root node to see if it is a TreeRootElement. if it is, then we
47
// use it to change the selected node. Otherwise we will do a recursive search
48
// of the tree to select the node.
49
String JavaDoc selectNode = request.getParameter(TreeElement.SELECTED_NODE);
50         if (selectNode != null) {
51             if (treeRoot instanceof ITreeRootElement) {
52                 ITreeRootElement root = (ITreeRootElement) treeRoot;
53                 root.changeSelected(selectNode, request);
54             }
55             else {
56                 setSelected(treeRoot, selectNode, request);
57             }
58         }
59
60         // handle auto expand.
61
String JavaDoc expandNode = null;
62
63         // our we auto expanding this node?
64
expandNode = request.getParameter(TreeElement.EXPAND_NODE);
65
66         // if we are auto expanding flip the expand of this node if it was expanded
67
if (expandNode != null) {
68             TreeElement n = treeRoot.findNode(expandNode);
69             if (n != null) {
70                 n.onExpand(request, response);
71                 n.setExpanded(!n.isExpanded());
72             }
73         }
74     }
75
76     /**
77      * Recursive routine to set the selected node. This will set the selected node
78      * and clear the selection on all other nodes. It will walk the full tree.
79      * @param node
80      * @param selected
81      */

82     protected static void setSelected(TreeElement node, String JavaDoc selected, ServletRequest JavaDoc request)
83     {
84         assert(node != null) : "parameter 'node' must not be null";
85         assert(selected != null) : "parameter 'selected' must not be null";
86         assert(request != null) : "parameter 'requested' must not be null";
87
88         if (node.getName().equals(selected)) {
89             node.onSelect(request);
90             node.setSelected(true);
91         }
92         else {
93             if (node.isSelected()) {
94                 node.onSelect(request);
95                 node.setSelected(false);
96             }
97         }
98
99         TreeElement children[] = node.getChildren();
100         assert(children != null);
101         for (int i = 0; i < children.length; i++) {
102             setSelected(children[i], selected, request);
103         }
104     }
105
106     /**
107      * This will return the currently selected node from a tree.
108      *
109      * <p>Implementation Note: The method that changes the selected node based on the request,
110      * {@link #processTreeRequest(String, TreeElement, HttpServletRequest, HttpServletResponse)},
111      * gets called during the processing of the {@link Tree} tag within a JSP. If the
112      * <code>findSelected</code> method is called from an Action in a Page Flow Controller,
113      * the value of the selected node will have not yet been updated.</p>
114      *
115      * @param root the root element of the tree.
116      * @return a TreeElement that is the currently selected node. This may return null.
117      */

118     public static TreeElement findSelected(TreeElement root)
119     {
120         assert (root != null) : "parameter 'root' must not be null";
121         if (root instanceof ITreeRootElement) {
122             return ((ITreeRootElement) root).getSelectedNode();
123         }
124         return recursiveFindSelected(root);
125     }
126
127     /**
128      * Recursive method that will find the currently selected element in the tree.
129      * @param elem The current element to search.
130      * @return a TreeElement that is selected.
131      */

132     private static TreeElement recursiveFindSelected(TreeElement elem)
133     {
134         assert(elem != null);
135
136         if (elem.isSelected())
137             return elem;
138
139         TreeElement children[] = elem.getChildren();
140         assert(children != null);
141         for (int i = 0; i < children.length; i++) {
142             TreeElement e = recursiveFindSelected(children[i]);
143             if (e != null)
144                 return e;
145         }
146         return null;
147     }
148
149     /**
150      * This is a helper method that will change the selected node. This is provided to
151      * make implementation of ITreeRootElement easier. This is called by the <code>changeSelected</code>
152      * method there to do the work of changing the selected node.
153      * @param root The root of the tree
154      * @param selectedNode The node that is currently selected, it may be null
155      * @param selectNode The String name of the node that will be selected
156      * @param request The ServletRequest
157      * @return a TreeElement representing the new node selected.
158      */

159     public static TreeElement changeSelected(TreeElement root, TreeElement selectedNode, String JavaDoc selectNode, ServletRequest JavaDoc request)
160     {
161         assert(root != null) : "parameter 'root' must not be null";
162         assert(selectNode != null) : "parameter 'selectNode' must not be null";
163         assert(request != null) : "parameter 'request' must not be null";
164
165         // if there is a selectedNode then we need to raise the onSelect
166
// event on that node indicating it will soon not be selected
167
TreeElement n = root.findNode(selectNode);
168         if (n == null) {
169             logger.warn("The tree element '" + selectNode + "' was not found. Selection failed");
170             return null;
171         }
172
173         // change the node that was selected so it is no longer selected
174
if (selectedNode != null) {
175             selectedNode.onSelect(request);
176             selectedNode.setSelected(false);
177         }
178
179         // change the node that is to be selected
180
n.onSelect(request);
181         n.setSelected(true);
182         return n;
183     }
184
185 }
186
Popular Tags