KickJava   Java API By Example, From Geeks To Geeks.

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


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
17 package org.apache.myfaces.custom.tree2;
18
19
20 import org.apache.myfaces.component.html.util.AddResource;
21 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
22 import org.apache.myfaces.renderkit.html.HTML;
23 import org.apache.myfaces.renderkit.JSFAttr;
24
25 import javax.faces.component.NamingContainer;
26 import javax.faces.component.UIComponent;
27 import javax.faces.component.UICommand;
28 import javax.faces.component.UIGraphic;
29 import javax.faces.component.UIViewRoot;
30 import javax.faces.component.UIParameter;
31 import javax.faces.context.FacesContext;
32 import javax.faces.context.ResponseWriter;
33 import javax.faces.render.Renderer;
34
35 import java.io.IOException JavaDoc;
36 import java.io.UnsupportedEncodingException JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Map JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.net.URLDecoder JavaDoc;
41 import javax.servlet.http.Cookie JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * @author Sean Schofield
46  * @author Chris Barlow
47  * @author Hans Bergsten (Some code taken from an example in his O'Reilly JavaServer Faces book. Copied with permission)
48  * @version $Revision: 1.13 $ $Date: 2005/03/16 19:32:01 $
49  */

50 public class HtmlTreeRenderer extends Renderer
51 {
52     protected static final String JavaDoc TOGGLE_SPAN = "org.apache.myfaces.tree.TOGGLE_SPAN";
53     protected static final String JavaDoc ROOT_NODE_ID = "0";
54
55     private static final String JavaDoc JAVASCRIPT_ENCODED = "org.apache.myfaces.tree.JAVASCRIPT_ENCODED";
56     private static final String JavaDoc NAV_COMMAND = "org.apache.myfaces.tree.NAV_COMMAND";
57     private static final String JavaDoc ENCODING = "UTF-8";
58     private static final String JavaDoc ATTRIB_DELIM = ";";
59     private static final String JavaDoc ATTRIB_KEYVAL = "=";
60     private static final String JavaDoc NODE_STATE_EXPANDED = "x";
61     private static final String JavaDoc NODE_STATE_CLOSED = "c";
62     private final static String JavaDoc SEPARATOR = String.valueOf(NamingContainer.SEPARATOR_CHAR);
63
64     private static final int NOTHING = 0;
65     private static final int CHILDREN = 1;
66     private static final int EXPANDED = 2;
67     private static final int LINES = 4;
68     private static final int LAST = 8;
69
70     // see superclass for documentation
71
public boolean getRendersChildren()
72     {
73         return true;
74     }
75
76     public void decode(FacesContext context, UIComponent component)
77     {
78         super.decode(context, component);
79
80         // see if one of the nav nodes was clicked, if so, then toggle appropriate node
81
String JavaDoc nodeId = null;
82         HtmlTree tree = (HtmlTree)component;
83         String JavaDoc originalNodeId = tree.getNodeId();
84
85         if (getBoolean(component, JSFAttr.CLIENT_SIDE_TOGGLE, true))
86         {
87             Map JavaDoc cookieMap = context.getExternalContext().getRequestCookieMap();
88             Cookie JavaDoc treeCookie = (Cookie JavaDoc)cookieMap.get(component.getId());
89             if (treeCookie == null || treeCookie.getValue() == null)
90             {
91                 return;
92             }
93
94             String JavaDoc nodeState = null;
95             Map JavaDoc attrMap = getCookieAttr(treeCookie);
96             Iterator JavaDoc i = attrMap.keySet().iterator();
97             while (i.hasNext())
98             {
99                 nodeId = (String JavaDoc)i.next();
100                 nodeState = (String JavaDoc)attrMap.get(nodeId);
101
102                 if (NODE_STATE_EXPANDED.equals(nodeState))
103                 {
104                     tree.setNodeId(nodeId);
105                     if (!tree.isNodeExpanded())
106                     {
107                         tree.toggleExpanded();
108                     }
109                     tree.setNodeId(originalNodeId);
110                 }
111                 else if (NODE_STATE_CLOSED.equals(nodeState))
112                 {
113                     tree.setNodeId(nodeId);
114                     if (tree.isNodeExpanded())
115                     {
116                         tree.toggleExpanded();
117                     }
118                     tree.setNodeId(originalNodeId);
119                 }
120             }
121         }
122         else
123         {
124             nodeId = (String JavaDoc)context.getExternalContext().getRequestParameterMap().get(tree.getId() + SEPARATOR + NAV_COMMAND);
125
126             if (nodeId == null || nodeId.equals(""))
127             {
128                 return;
129             }
130
131             tree.setNodeId(nodeId);
132             tree.toggleExpanded();
133             tree.setNodeId(originalNodeId);
134         }
135     }
136
137     public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc
138     {
139         // write javascript functions
140
encodeJavascript(context, component);
141     }
142
143     /**
144      * Renders the whole tree. It generates a <code>&lt;span></code> element with an <code>id</code>
145      * attribute if the component has been given an explicit ID. The model nodes are rendered
146      * recursively by the private <code>encodeNodes</code> method.
147      *
148      * @param context FacesContext
149      * @param component The component whose children are to be rendered
150      * @throws IOException
151      */

152     public void encodeChildren(FacesContext context, UIComponent component) throws IOException JavaDoc
153     {
154         HtmlTree tree = (HtmlTree)component;
155         boolean showRootNode = getBoolean(tree, JSFAttr.SHOW_ROOT_NODE, true);
156
157         if (!component.isRendered()) return;
158
159         if (tree.getValue() == null) return;
160
161         ResponseWriter out = context.getResponseWriter();
162         String JavaDoc clientId = null;
163
164         if (component.getId() != null && !component.getId().startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
165         {
166             clientId = component.getClientId(context);
167         }
168
169         boolean isOuterSpanUsed = false;
170
171         if (clientId != null)
172         {
173             isOuterSpanUsed = true;
174             out.startElement("span", component);
175             out.writeAttribute("id", clientId, "id");
176         }
177
178         if (showRootNode)
179         {
180             // encode the tree (starting with the root node)
181
encodeTree(context, out, tree, null, 0);
182         }
183         else
184         {
185             tree.setNodeId("0");
186             TreeNode rootNode = tree.getNode();
187             List JavaDoc rootChildren = rootNode.getChildren();
188             int kidId = 0;
189
190             for (int i = 0; i < rootChildren.size(); i++)
191             {
192                 encodeTree(context, out, tree, ROOT_NODE_ID, kidId++);
193             }
194         }
195
196         tree.setNodeId(null);
197
198         if (isOuterSpanUsed)
199         {
200             out.endElement("span");
201         }
202     }
203
204     /**
205      * Encodes the tree and its children.
206      *
207      * @param context FacesContext
208      * @param out ResponseWriter
209      * @param tree HtmlTree
210      * @param parentId The parent's node id (where parent is the parent of the node we are about to render.)
211      * @param childCount If this node is a child of another node, the count indicates which child number it is
212      * (used to construct the id of the next node to render.)
213      * @throws IOException
214      */

215     protected void encodeTree(FacesContext context, ResponseWriter out, HtmlTree tree, String JavaDoc parentId, int childCount)
216         throws IOException JavaDoc
217     {
218         boolean clientSideToggle = getBoolean(tree, JSFAttr.CLIENT_SIDE_TOGGLE, true);
219
220         String JavaDoc nodeId = (parentId != null) ? parentId + NamingContainer.SEPARATOR_CHAR + childCount : ROOT_NODE_ID;
221         String JavaDoc spanId = TOGGLE_SPAN + ":" + tree.getId() + ":" + nodeId;
222
223         tree.setNodeId(nodeId);
224         TreeNode node = tree.getNode();
225
226         // encode the current node
227
HtmlRendererUtils.writePrettyLineSeparator(context);
228         beforeNodeEncode(context, out, tree);
229         encodeCurrentNode(context, out, tree);
230         afterNodeEncode(context, out);
231
232         // only encode the children if clientSideToggle is true or if this node is expanded (regardless of clientSideToggle)
233
if (clientSideToggle == true || tree.isNodeExpanded())
234         {
235             int kidId = 0;
236             String JavaDoc currId = tree.getNodeId();
237             List JavaDoc children = node.getChildren();
238
239             // if client side toggling is on, add a span to be used for displaying/hiding children
240
if (clientSideToggle)
241             {
242                 out.startElement(HTML.SPAN_ELEM, tree);
243                 out.writeAttribute(HTML.ID_ATTR, spanId, null);
244
245                 if (tree.isNodeExpanded())
246                 {
247                     out.writeAttribute(HTML.STYLE_ATTR, "display:block", null);
248                 }
249                 else
250                 {
251                     out.writeAttribute(HTML.STYLE_ATTR, "display:none", null);
252                 }
253             }
254
255             for (int i = 0; i < children.size(); i++)
256             {
257                 encodeTree(context, out, tree, currId, kidId++);
258             }
259
260             if (clientSideToggle)
261             {
262                 out.endElement(HTML.SPAN_ELEM);
263             }
264         }
265     }
266
267     /**
268      * Encodes the current node. It is protected so that custom {@link Renderer}s can extend it. That might be useful
269      * if you would like to render additional per node information besides the tree node.
270      *
271      * @param context FacesContext
272      * @param out ResponseWriter
273      * @param tree HtmlTree
274      * @throws IOException
275      */

276     protected void encodeCurrentNode(FacesContext context, ResponseWriter out, HtmlTree tree)
277         throws IOException JavaDoc
278     {
279         TreeNode node = tree.getNode();
280
281         // set configurable values
282
boolean showRootNode = getBoolean(tree, JSFAttr.SHOW_ROOT_NODE, true);
283         boolean showNav = getBoolean(tree, JSFAttr.SHOW_NAV, true);
284         boolean showLines = getBoolean(tree, JSFAttr.SHOW_LINES, true);
285         boolean clientSideToggle = getBoolean(tree, JSFAttr.CLIENT_SIDE_TOGGLE, true);
286
287         if (clientSideToggle)
288         {
289             // we must show the nav icons if client side toggle is enabled (regardless of what user says)
290
showNav = true;
291         }
292
293         UIComponent nodeTypeFacet = tree.getFacet(node.getType());
294         UIComponent nodeImgFacet = null;
295
296         if (nodeTypeFacet == null)
297         {
298             throw new IllegalArgumentException JavaDoc("Unable to locate facet with the name: " + node.getType());
299         }
300
301         // render node padding
302
String JavaDoc[] pathInfo = tree.getPathInformation(tree.getNodeId());
303         int paddingLevel = pathInfo.length - 1;
304
305         for (int i = (showRootNode ? 0 : 1); i < paddingLevel; i++)
306         {
307             boolean lastChild = tree.isLastChild((String JavaDoc)pathInfo[i]);
308             String JavaDoc lineSrc = (!lastChild && showLines)
309                              ? getImageSrc(context, tree, "line-trunk.gif")
310                              : getImageSrc(context, tree, "spacer.gif");
311
312             out.startElement(HTML.TD_ELEM, tree);
313             out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
314             out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
315             out.writeURIAttribute("background", lineSrc, null);
316             out.startElement(HTML.IMG_ELEM, tree);
317             out.writeURIAttribute(HTML.SRC_ATTR, lineSrc, null);
318             out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
319             out.writeAttribute(HTML.HEIGHT_ATTR, "18", null);
320             out.writeAttribute(HTML.BORDER_ATTR, "0", null);
321             out.endElement(HTML.IMG_ELEM);
322             out.endElement(HTML.TD_ELEM);
323         }
324
325         if (showNav)
326         {
327             nodeImgFacet = encodeNavigation(context, out, tree);
328         }
329
330         // render node
331
out.startElement(HTML.TD_ELEM, tree);
332         if (nodeImgFacet != null)
333         {
334             encodeRecursive(context, nodeImgFacet);
335         }
336         encodeRecursive(context, nodeTypeFacet);
337         out.endElement(HTML.TD_ELEM);
338     }
339
340     protected void beforeNodeEncode(FacesContext context, ResponseWriter out, HtmlTree tree)
341         throws IOException JavaDoc
342     {
343         out.startElement(HTML.TABLE_ELEM, null);
344         out.writeAttribute(HTML.CELLPADDING_ATTR, "0", null);
345         out.writeAttribute(HTML.CELLSPACING_ATTR, "0", null);
346         out.writeAttribute(HTML.BORDER_ATTR, "0", null);
347         out.startElement(HTML.TR_ELEM, null);
348     }
349
350     protected void afterNodeEncode(FacesContext context, ResponseWriter out)
351         throws IOException JavaDoc
352     {
353         out.endElement(HTML.TR_ELEM);
354         out.endElement(HTML.TABLE_ELEM);
355     }
356
357     /**
358      * Handles the encoding related to the navigation functionality.
359      *
360      * @param context FacesContext
361      * @param out ResponseWriter
362      * @param tree HtmlTree
363      * @return The additional navigation image to display inside the node (if any). Only used with client-side toggle.
364      * @throws IOException
365      */

366     private UIComponent encodeNavigation(FacesContext context, ResponseWriter out, HtmlTree tree)
367         throws IOException JavaDoc
368     {
369         TreeNode node = tree.getNode();
370         String JavaDoc nodeId = tree.getNodeId();
371         String JavaDoc spanId = TOGGLE_SPAN + ":" + tree.getId() + ":" + nodeId;//TOGGLE_SPAN + nodeId;
372
boolean showLines = getBoolean(tree, JSFAttr.SHOW_LINES, true);
373         boolean clientSideToggle = getBoolean(tree, JSFAttr.CLIENT_SIDE_TOGGLE, true);
374         UIComponent nodeTypeFacet = tree.getFacet(node.getType());
375         String JavaDoc navSrc = null;
376         String JavaDoc altSrc = null;
377         UIComponent nodeImgFacet = null;
378
379         int bitMask = NOTHING;
380         bitMask += (node.getChildCount()>0) ? CHILDREN : NOTHING;
381         bitMask += (tree.isNodeExpanded()) ? EXPANDED : NOTHING;
382         bitMask += (tree.isLastChild(tree.getNodeId())) ? LAST : NOTHING;
383         bitMask += (showLines) ? LINES : NOTHING;
384
385         switch (bitMask)
386         {
387             case (NOTHING):
388
389             case (LAST):
390                 navSrc = "spacer.gif";
391                 break;
392
393             case (LINES):
394                 navSrc = "line-middle.gif";
395                 break;
396
397             case (LINES + LAST):
398                 navSrc = "line-last.gif";
399                 break;
400
401             case (CHILDREN):
402
403             case (CHILDREN + LAST):
404                 navSrc = "nav-plus.gif";
405                 altSrc = "nav-minus.gif";
406                 break;
407
408             case (CHILDREN + LINES):
409
410                 navSrc = "nav-plus-line-middle.gif";
411                 altSrc = "nav-minus-line-middle.gif";
412                 break;
413
414             case (CHILDREN + LINES + LAST):
415
416                 navSrc = "nav-plus-line-last.gif";
417                 altSrc = "nav-minus-line-last.gif";
418                 break;
419
420             case (CHILDREN + EXPANDED):
421
422             case (CHILDREN + EXPANDED + LAST):
423                 navSrc = "nav-minus.gif";
424                 altSrc = "nav-plus.gif";
425                 break;
426
427             case (CHILDREN + EXPANDED + LINES):
428                 navSrc = "nav-minus-line-middle.gif";
429                 altSrc = "nav-plus-line-middle.gif";
430                 break;
431
432             case (CHILDREN + EXPANDED + LINES + LAST):
433                 navSrc = "nav-minus-line-last.gif";
434                 altSrc = "nav-plus-line-last.gif";
435                 break;
436
437             default:
438                 throw new IllegalArgumentException JavaDoc("Invalid bit mask of " + bitMask);
439         }
440
441         // adjust navSrc and altSrc so that the images can be retrieved using the extensions filter
442
String JavaDoc navSrcUrl = getImageSrc(null, tree, navSrc);
443         navSrc = getImageSrc(context, tree, navSrc);
444         altSrc = getImageSrc(context, tree, altSrc);
445
446         // render nav cell
447
out.startElement(HTML.TD_ELEM, tree);
448         out.writeAttribute(HTML.WIDTH_ATTR, "19", null);
449         out.writeAttribute(HTML.HEIGHT_ATTR, "100%", null);
450         out.writeAttribute("valign", "top", null);
451
452         if ((bitMask & LINES)!=0 && (bitMask & LAST)==0)
453         {
454             out.writeURIAttribute("background", getImageSrc(context, tree, "line-trunk.gif"), null);
455         }
456
457         // add the appropriate image for the nav control
458
UIGraphic image = new UIGraphic();
459         image.setId(context.getViewRoot().createUniqueId());
460         image.setUrl(navSrcUrl);
461         Map JavaDoc imageAttrs = image.getAttributes();
462         imageAttrs.put(HTML.WIDTH_ATTR, "19");
463         imageAttrs.put(HTML.HEIGHT_ATTR, "18");
464         imageAttrs.put(HTML.BORDER_ATTR, "0");
465
466         if (clientSideToggle)
467         {
468             /**
469              * With client side toggle, user has the option to specify open/closed images for the node (in addition to
470              * the navigtion ones provided by the component.)
471              */

472             String JavaDoc expandImgSrc = "";
473             String JavaDoc collapseImgSrc = "";
474             String JavaDoc nodeImageId = "";
475
476             UIComponent expandFacet = nodeTypeFacet.getFacet("expand");
477             if (expandFacet != null)
478             {
479                 UIGraphic expandImg = (UIGraphic)expandFacet;
480                 expandImgSrc = expandImg.getUrl();
481                 if (expandImg.isRendered())
482                 {
483                     expandImg.setId(context.getViewRoot().createUniqueId());
484                     nodeImageId = expandImg.getClientId(context);
485                     nodeImgFacet = expandFacet;
486                 }
487             }
488
489             UIComponent collapseFacet = nodeTypeFacet.getFacet("collapse");
490             if (collapseFacet != null)
491             {
492                 UIGraphic collapseImg = (UIGraphic)collapseFacet;
493                 collapseImgSrc = collapseImg.getUrl();
494                 if (collapseImg.isRendered())
495                 {
496                     collapseImg.setId(context.getViewRoot().createUniqueId());
497                     nodeImageId = collapseImg.getClientId(context);
498                     nodeImgFacet = collapseFacet;
499                 }
500             }
501
502             if (node.getChildCount() > 0)
503             {
504                 String JavaDoc onClick = new StringBuffer JavaDoc()
505                     .append("treeNavClick('")
506                     .append(spanId)
507                     .append("', '")
508                     .append(image.getClientId(context))
509                     .append("', '")
510                     .append(navSrc)
511                     .append("', '")
512                     .append(altSrc)
513                     .append("', '")
514                     .append(nodeImageId)
515                     .append("', '")
516                     .append(expandImgSrc)
517                     .append("', '")
518                     .append(collapseImgSrc)
519                     .append("', '")
520                     .append(tree.getId())
521                     .append("', '")
522                     .append(nodeId)
523                     .append("');")
524                     .toString();
525
526                 imageAttrs.put(HTML.ONCLICK_ATTR, onClick);
527                 imageAttrs.put(HTML.STYLE_ATTR, "cursor:hand;cursor:pointer");
528             }
529             encodeRecursive(context, image);
530         }
531         else
532         {
533             // set up the expand control and remove whatever children (if any) this control had previously
534
UICommand expandControl = tree.getExpandControl();
535             expandControl.setId(context.getViewRoot().createUniqueId());
536             expandControl.getChildren().clear();
537
538             UIParameter param = new UIParameter();
539             param.setName(tree.getId() + NamingContainer.SEPARATOR_CHAR + NAV_COMMAND);
540             param.setValue(tree.getNodeId());
541             expandControl.getChildren().add(param);
542             expandControl.getChildren().add(image);
543
544             encodeRecursive(context, expandControl);
545         }
546         out.endElement(HTML.TD_ELEM);
547
548         return nodeImgFacet;
549     }
550
551     private void encodeRecursive(FacesContext context, UIComponent component) throws IOException JavaDoc
552     {
553         /**@todo consider moving this common functionality to a base class or utility class */
554         if (!component.isRendered()) return;
555
556         component.encodeBegin(context);
557
558         if (component.getRendersChildren())
559         {
560             component.encodeChildren(context);
561         }
562         else
563         {
564             List JavaDoc childList = component.getChildren();
565
566             for (int i=0; i < childList.size(); i++)
567             {
568                 UIComponent child = (UIComponent)childList.get(i);
569                 encodeRecursive(context, child);
570             }
571         }
572
573         component.encodeEnd(context);
574     }
575
576     /**
577      * Encodes any stand-alone javascript functions that are needed. Uses either the extension filter, or a
578      * user-supplied location for the javascript files.
579      *
580      * @param context FacesContext
581      * @param component UIComponent
582      * @throws IOException
583      */

584     private void encodeJavascript(FacesContext context, UIComponent component) throws IOException JavaDoc
585     {
586         // check to see if javascript has already been written (which could happen if more than one tree on the same page)
587
if (context.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED))
588         {
589             return;
590         }
591
592         // render javascript function for client-side toggle (it won't be used if user has opted for server-side toggle)
593
ResponseWriter out = context.getResponseWriter();
594         String JavaDoc javascriptLocation = (String JavaDoc)component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
595         if (javascriptLocation == null)
596         {
597             AddResource.addJavaScriptHere(HtmlTreeRenderer.class, "javascript/tree.js", context);
598             AddResource.addJavaScriptHere(HtmlTreeRenderer.class, "javascript/cookielib.js", context);
599         }
600         else
601         {
602             out.startElement(HTML.SCRIPT_ELEM, null);
603             out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
604             out.writeAttribute(HTML.SRC_ATTR,
605                                javascriptLocation + "/tree.js", null);
606             out.endElement(HTML.SCRIPT_ELEM);
607
608             out.startElement(HTML.SCRIPT_ELEM, null);
609             out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
610             out.writeAttribute(HTML.SRC_ATTR,
611                                javascriptLocation + "/cookielib.js", null);
612             out.endElement(HTML.SCRIPT_ELEM);
613         }
614
615         context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
616     }
617
618     /**
619      * Get the appropriate image src location. Uses the extension filter mechanism for images if no image
620      * location has been specified.
621      *
622      * @param context The {@link FacesContex}. NOTE: If <code>null</code> then context path information
623      * will not be used with extensions filter (assuming no imageLocation specified.)
624      * @param component UIComponent
625      * @param imageName The name of the image file to use.
626      * @return The image src information.
627      */

628     private String JavaDoc getImageSrc(FacesContext context, UIComponent component, String JavaDoc imageName)
629     {
630         String JavaDoc imageLocation = (String JavaDoc)component.getAttributes().get(JSFAttr.IMAGE_LOCATION);
631         if (imageLocation == null)
632         {
633             return AddResource.getResourceMappedPath(HtmlTreeRenderer.class,
634                 "images/" + imageName, context);
635         }
636         else
637         {
638             return imageLocation + "/" + imageName;
639         }
640     }
641
642     /**
643      * Helper method for getting the boolean value of an attribute. If the attribute is not specified,
644      * then return the default value.
645      *
646      * @param component The component for which the attributes are to be checked.
647      * @param attributeName The name of the boolean attribute.
648      * @param defaultValue The default value of the attribute (to be returned if no value found).
649      * @return boolean
650      */

651     protected boolean getBoolean(UIComponent component, String JavaDoc attributeName, boolean defaultValue)
652     {
653         Boolean JavaDoc booleanAttr = (Boolean JavaDoc)component.getAttributes().get(attributeName);
654
655         if (booleanAttr == null)
656         {
657             return defaultValue;
658         }
659         else
660         {
661             return booleanAttr.booleanValue();
662         }
663     }
664
665     private Map JavaDoc getCookieAttr(Cookie JavaDoc cookie)
666     {
667         Map JavaDoc attribMap = new HashMap JavaDoc();
668         try
669         {
670             String JavaDoc cookieValue = URLDecoder.decode(cookie.getValue(),ENCODING);
671             String JavaDoc[] attribArray = cookieValue.split(ATTRIB_DELIM);
672             for (int j = 0; j < attribArray.length; j++)
673             {
674                 int index = attribArray[j].indexOf(ATTRIB_KEYVAL);
675                 String JavaDoc name = attribArray[j].substring(0, index);
676                 String JavaDoc value = attribArray[j].substring(index + 1);
677                 attribMap.put(name, value);
678             }
679         }
680         catch (UnsupportedEncodingException JavaDoc e)
681         {
682             throw new RuntimeException JavaDoc("Error parsing tree cookies", e);
683         }
684         return attribMap;
685     }
686 }
687
Popular Tags