001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import org.w3c.dom.Element;
038: import org.w3c.dom.Node;
039: import org.w3c.dom.Text;
040:
041: import javax.faces.component.UIComponent;
042: import javax.faces.context.FacesContext;
043: import java.io.IOException;
044:
045: /*
046: * Renderer class for a label element.
047: *
048: * This label can be associated with another component. The component that
049: * this label is associated with is known as the 'for' component.
050: * At the time of rendering of this label, its for component may not have
051: * been created yet and thus it's impossible to get the client id from the
052: * for component. This class provides a method for fabricating the client id
053: * of the for component
054: *
055: * An assumption found within this class is that the for component and
056: * the label component are within the same form.
057: *
058: */
059:
060: public class LabelRenderer extends DomBasicInputRenderer {
061:
062: public void encodeBegin(FacesContext facesContext,
063: UIComponent uiComponent) throws IOException {
064:
065: validateParameters(facesContext, uiComponent, null);
066:
067: DOMContext domContext = DOMContext.attachDOMContext(
068: facesContext, uiComponent);
069:
070: if (!domContext.isInitialized()) {
071: Element root = domContext.createElement("label");
072: domContext.setRootNode(root);
073: setRootElementId(facesContext, root, uiComponent);
074: PassThruAttributeRenderer.renderAttributes(facesContext,
075: uiComponent, null);
076: }
077: Element root = (Element) domContext.getRootNode();
078:
079: // Obtain the client id of the 'for' component. If the component
080: // already exists then query the object for its client id.
081: // If the for component does not yet exist then fabricate
082: // its client id.
083: // Note that the value of the "for" attribute is the id of the
084: // for component
085: UIComponent forComponent = null;
086: String forComponentClientId = null;
087: String forAttribute = (String) uiComponent.getAttributes().get(
088: "for");
089: if (forAttribute != null) {
090: forComponent = findForComponent(facesContext, uiComponent);
091: if (forComponent != null) {
092: forComponentClientId = forComponent
093: .getClientId(facesContext);
094: } else {
095: forComponentClientId = fabricateClientId(uiComponent,
096: facesContext, forAttribute);
097: }
098: }
099: // if the forAttribute exists then we queried or constructed the
100: // for component's client id; render it as the value of the for attribute
101: if (forComponentClientId != null) {
102: root.setAttribute("for", forComponentClientId);
103: }
104: String styleClass = (String) uiComponent.getAttributes().get(
105: "styleClass");
106: if (styleClass != null) {
107: root.setAttribute("class", styleClass);
108: }
109: String currentValue = getValue(facesContext, uiComponent);
110: if (currentValue != null && currentValue.length() != 0) {
111: Node firstChild = root.getFirstChild();
112: if (firstChild != null && firstChild instanceof Text) {
113: ((Text) firstChild).setData(currentValue);
114: } else {
115: root.appendChild(domContext.getDocument()
116: .createTextNode(currentValue));
117: }
118: }
119:
120: domContext.stepInto(uiComponent);
121: }
122:
123: public void encodeChildren(FacesContext facesContext,
124: UIComponent uiComponent) throws IOException {
125: validateParameters(facesContext, uiComponent, null);
126: super .encodeChildren(facesContext, uiComponent);
127: }
128:
129: public void encodeEnd(FacesContext facesContext,
130: UIComponent uiComponent) throws IOException {
131: validateParameters(facesContext, uiComponent, null);
132: DOMContext domContext = DOMContext.attachDOMContext(
133: facesContext, uiComponent);
134: domContext.stepOver();
135: domContext.streamWrite(facesContext, uiComponent);
136: }
137:
138: }
|