KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > tree2 > HtmlTree


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 package org.apache.myfaces.custom.tree2;
17
18 import javax.faces.context.FacesContext;
19 import javax.faces.component.UICommand;
20 import javax.faces.component.html.HtmlCommandLink;
21 import javax.faces.event.ActionEvent;
22 import javax.faces.el.MethodBinding;
23
24 import java.util.HashSet JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.io.IOException JavaDoc;
27
28
29 /**
30  * Represents "tree data" in an HTML format. Also provides a mechanism for maintaining expand/collapse
31  * state of the nodes in the tree.
32  *
33  * @author Sean Schofield
34  * @version $Revision: 1.9 $ $Date: 2005/03/16 19:32:01 $
35  */

36 public class HtmlTree extends UITreeData
37 {
38     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.Tree2";
39     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Tree2";
40     private static final String JavaDoc NODE_STATE_KEY = "org.apache.myfaces.tree.NODE_STATE_KEY";
41     private UICommand _expandControl;
42     private String JavaDoc _varNodeToggler;
43     private HashSet JavaDoc _expandedNodes = new HashSet JavaDoc();
44     private String JavaDoc _selectedNodeId;
45
46     /**
47      * Constructor
48      */

49     public HtmlTree()
50     {
51         setRendererType(DEFAULT_RENDERER_TYPE);
52         _expandControl = new HtmlCommandLink();
53     }
54
55     // see superclass for documentation
56
public Object JavaDoc saveState(FacesContext context)
57     {
58         Object JavaDoc values[] = new Object JavaDoc[4];
59         values[0] = super.saveState(context);
60         values[1] = _expandedNodes;
61         values[2] = _varNodeToggler;
62         values[3] = _selectedNodeId;
63
64         return ((Object JavaDoc) (values));
65     }
66
67     // see superclass for documentation
68
public void restoreState(FacesContext context, Object JavaDoc state)
69     {
70         Object JavaDoc values[] = (Object JavaDoc[])state;
71         super.restoreState(context, values[0]);
72         _expandedNodes = (HashSet JavaDoc)values[1];
73         setVarNodeToggler((String JavaDoc)values[2]);
74         _selectedNodeId = (String JavaDoc)values[3];
75     }
76
77     // see superclass for documentation
78
public void setNodeId(String JavaDoc nodeId)
79     {
80         super.setNodeId(nodeId);
81
82         if (_varNodeToggler != null)
83         {
84             Map JavaDoc requestMap = getFacesContext().getExternalContext().getRequestMap();
85             requestMap.put(_varNodeToggler, this);
86         }
87     }
88
89     public void processDecodes(FacesContext context)
90     {
91         super.processDecodes(context);
92
93         // store the expand/collapse state information in the session (long story)
94
Map JavaDoc sessionMap = context.getExternalContext().getSessionMap();
95         sessionMap.put(NODE_STATE_KEY + ":" + getId(), _expandedNodes);
96     }
97
98     public void encodeBegin(FacesContext context)
99             throws IOException JavaDoc
100     {
101         /**
102          * The expand/collapse state of the nodes is stored in the session in order to ensure that this information
103          * is preserved across requests where the same tree is reused in a tile (server-side include.) When using
104          * the server-side toggle method without this step, the tree would not remember the expand/collapse state.
105          * Since we didn't think it was appropriate to burden the end user with this information as part of a backing
106          * bean, it just being stored in the session during encode and retrieved during decode.
107          */

108         // restore the expand/collapse state information from the session
109
Map JavaDoc sessionMap = context.getExternalContext().getSessionMap();
110         HashSet JavaDoc nodeState = (HashSet JavaDoc)sessionMap.get(NODE_STATE_KEY + ":" + getId());
111
112         if (nodeState != null)
113         {
114             _expandedNodes = nodeState;
115         }
116
117         super.encodeBegin(context);
118     }
119
120     /**
121      * Gets the expand/collapse control that can be used to handle expand/collapse nodes. This is only used in server-side
122      * mode. It allows the nagivation controls (if any) to be clickable as well as any commandLinks the user has set up in
123      * their JSP.
124      *
125      * @return UICommand
126      */

127     public UICommand getExpandControl()
128     {
129         return _expandControl;
130     }
131
132     public void setVarNodeToggler(String JavaDoc varNodeToggler)
133     {
134         _varNodeToggler = varNodeToggler;
135
136         // create a method binding for the expand control
137
String JavaDoc bindingString = "#{" + varNodeToggler + ".toggleExpanded}";
138         MethodBinding actionBinding = FacesContext.getCurrentInstance().getApplication().createMethodBinding(bindingString, null);
139         _expandControl.setAction(actionBinding);
140     }
141
142     public String JavaDoc toggleExpanded()
143     {
144         String JavaDoc nodeId = getNodeId();
145
146         if (_expandedNodes.contains(nodeId))
147         {
148             _expandedNodes.remove(nodeId);
149         }
150         else
151         {
152             _expandedNodes.add(nodeId);
153         }
154
155         return null;
156     }
157
158     /**
159      * Indicates whether or not the current {@link TreeNode} is expanded.
160      * @return boolean
161      */

162     public boolean isNodeExpanded()
163     {
164         return (_expandedNodes.contains(getNodeId()) && getNode().getChildCount() > 0);
165     }
166
167     protected void processChildNodes(FacesContext context, TreeNode parentNode, int processAction)
168     {
169         super.processChildNodes(context, parentNode, processAction);
170     }
171
172     /**
173      * Implements the {@link ActionListener} interface. Basically, this method is used to listen for
174      * node selection events (when a user has clicked on a leaf node.)
175      *
176      * @param event ActionEvent
177      */

178     public void setNodeSelected(ActionEvent event)
179     {
180         _selectedNodeId = getNodeId();
181     }
182
183     /**
184      * Indicates whether or not the current {@link TreeNode} is selected.
185      * @return boolean
186      */

187     public boolean isNodeSelected()
188     {
189         return getNodeId().equals(_selectedNodeId);
190     }
191 }
192
Popular Tags