KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > LabelRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.renderkit.dom_html_basic;
35
36 import com.icesoft.faces.context.DOMContext;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.Node JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41 import javax.faces.component.UIComponent;
42 import javax.faces.context.FacesContext;
43 import java.io.IOException JavaDoc;
44
45 /*
46 * Renderer class for a label element.
47 *
48 * This label can be associated with another component. The component that
49 * this label is associated with is known as the 'for' component.
50 * At the time of rendering of this label, its for component may not have
51 * been created yet and thus it's impossible to get the client id from the
52 * for component. This class provides a method for fabricating the client id
53 * of the for component
54 *
55 * An assumption found within this class is that the for component and
56 * the label component are within the same form.
57 *
58 */

59
60 public class LabelRenderer extends DomBasicInputRenderer {
61
62     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
63             throws IOException JavaDoc {
64
65         validateParameters(facesContext, uiComponent, null);
66
67         DOMContext domContext =
68                 DOMContext.attachDOMContext(facesContext, uiComponent);
69
70         if (!domContext.isInitialized()) {
71             Element JavaDoc root = domContext.createElement("label");
72             domContext.setRootNode(root);
73             setRootElementId(facesContext, root, uiComponent);
74             PassThruAttributeRenderer.renderAttributes(
75                     facesContext, uiComponent, null);
76         }
77         Element JavaDoc root = (Element JavaDoc) domContext.getRootNode();
78
79         // Obtain the client id of the 'for' component. If the component
80
// already exists then query the object for its client id.
81
// If the for component does not yet exist then fabricate
82
// its client id.
83
// Note that the value of the "for" attribute is the id of the
84
// for component
85
UIComponent forComponent = null;
86         String JavaDoc forComponentClientId = null;
87         String JavaDoc forAttribute = (String JavaDoc) uiComponent.getAttributes().get("for");
88         if (forAttribute != null) {
89             forComponent = findForComponent(facesContext, uiComponent);
90             if (forComponent != null) {
91                 forComponentClientId = forComponent.getClientId(facesContext);
92             } else {
93                 forComponentClientId = fabricateClientId(uiComponent,
94                                                          facesContext,
95                                                          forAttribute);
96             }
97         }
98         // if the forAttribute exists then we queried or constructed the
99
// for component's client id; render it as the value of the for attribute
100
if (forComponentClientId != null) {
101             root.setAttribute("for", forComponentClientId);
102         }
103         String JavaDoc styleClass =
104                 (String JavaDoc) uiComponent.getAttributes().get("styleClass");
105         if (styleClass != null) {
106             root.setAttribute("class", styleClass);
107         }
108         String JavaDoc currentValue = getValue(facesContext, uiComponent);
109         if (currentValue != null && currentValue.length() != 0) {
110             Node JavaDoc firstChild = root.getFirstChild();
111             if (firstChild != null && firstChild instanceof Text JavaDoc) {
112                 ((Text JavaDoc) firstChild).setData(currentValue);
113             } else {
114                 root.appendChild(
115                         domContext.getDocument().createTextNode(currentValue));
116             }
117         }
118
119         domContext.stepOver();
120         domContext.streamWrite(facesContext, uiComponent);
121     }
122
123     public void encodeChildren(FacesContext facesContext,
124                                UIComponent uiComponent) {
125         validateParameters(facesContext, uiComponent, null);
126     }
127
128     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
129             throws IOException JavaDoc {
130         validateParameters(facesContext, uiComponent, null);
131     }
132
133
134 }
135
Popular Tags