KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > component > html > util > HtmlComponentUtils


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.component.html.util;
17
18 import javax.faces.component.NamingContainer;
19 import javax.faces.component.UIComponent;
20 import javax.faces.component.UIData;
21 import javax.faces.context.FacesContext;
22 import javax.faces.render.Renderer;
23
24 import org.apache.myfaces.renderkit.JSFAttr;
25
26 /**
27  * <p>Utility class for providing basic functionality to the HTML faces
28  * extended components.<p>
29  *
30  * @author Sean Schofield
31  * @version $Revision: 1.5 $ $Date: 2005/01/28 17:19:09 $
32  */

33 public class HtmlComponentUtils
34 {
35     private static final String JavaDoc TRUE = "true";
36     private static final String JavaDoc KEY_COMPONENT_ID_MAP = "KEY_COMPONENT_ID_MAP";
37     
38     /**
39      * Constructor (Private)
40      */

41     private HtmlComponentUtils()
42     {}
43     
44     /**
45      * Gets the client id associated with the component. Checks the forceId
46      * attribute of the component (if present) and uses the orginally supplied
47      * id value if that attribute is true. Also performs the required call
48      * to <code>convertClientId</code> on the {@link Renderer} argument.
49      *
50      * @param component The component for which the client id is needed.
51      * @param renderer The renderer associated with the component.
52      * @param context Additional context information to help in the request.
53      * @return The clientId to use with the specified component.
54      */

55     public static String JavaDoc getClientId(UIComponent component,
56                                      Renderer renderer,
57                                      FacesContext context)
58     {
59         // see if the originally supplied id should be used
60
Boolean JavaDoc forceValue = (Boolean JavaDoc)component.getAttributes().get(JSFAttr.FORCE_ID_ATTR);
61         boolean forceId = false;
62         
63         if (forceValue != null)
64         {
65             forceId = forceValue.booleanValue();
66         }
67         
68         if (forceId && component.getId() != null)
69         {
70             String JavaDoc clientId = component.getId();
71             
72             /**
73              * See if there is a parent naming container. If there is ...
74              */

75             UIComponent parentContainer = HtmlComponentUtils.findParentNamingContainer(component, false);
76             if (parentContainer != null)
77             {
78                 if (parentContainer instanceof UIData)
79                 {
80                     // see if the originally supplied id should be used
81
Boolean JavaDoc forceIdIndexValue = (Boolean JavaDoc)component.getAttributes().get(JSFAttr.FORCE_ID_INDEX_ATTR);
82                     boolean forceIdIndex = true;
83
84                     if (forceIdIndexValue != null)
85                     {
86                         forceIdIndex = forceIdIndexValue.booleanValue();
87                     }
88
89                     // note: user may have specifically requested that we do not add the special forceId [index] suffix
90
if (forceIdIndex)
91                     {
92                         int rowIndex = ( (UIData) parentContainer).getRowIndex();
93                         if (rowIndex != -1) {
94                             clientId = clientId + "[" + rowIndex + "]";
95                         }
96                     }
97                 }
98             }
99             
100             // JSF spec requires that renderer get a chance to convert the id
101
if (renderer != null)
102             {
103                 clientId = renderer.convertClientId(context, clientId);
104             }
105             
106             return clientId;
107         }
108         else
109         {
110             return null;
111         }
112     }
113     
114     /**
115      * Locates the {@link NamingContainer} associated with the givem
116      * {@link UIComponent}.
117      *
118      * @param component The component whose naming locator needs to be found.
119      * @param returnRootIfNotFound Whether or not the root should be returned
120      * if no naming container is found.
121      * @return The parent naming container (or root if applicable).
122      */

123     public static UIComponent findParentNamingContainer(UIComponent component,
124         boolean returnRootIfNotFound)
125     {
126         UIComponent parent = component.getParent();
127         if (returnRootIfNotFound && parent == null)
128         {
129             return component;
130         }
131         while (parent != null)
132         {
133             if (parent instanceof NamingContainer) return parent;
134             if (returnRootIfNotFound)
135             {
136                 UIComponent nextParent = parent.getParent();
137                 if (nextParent == null)
138                 {
139                     return parent; //Root
140
}
141                 parent = nextParent;
142             }
143             else
144             {
145                 parent = parent.getParent();
146             }
147         }
148         return null;
149     }
150 }
151
Popular Tags