001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.myfaces.renderkit.html;
017:
018: import java.io.IOException;
019:
020: import javax.faces.component.UIComponent;
021: import javax.faces.component.ValueHolder;
022: import javax.faces.component.html.HtmlOutputLabel;
023: import javax.faces.context.FacesContext;
024: import javax.faces.context.ResponseWriter;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.myfaces.shared_impl.renderkit.JSFAttr;
029: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
030: import org.apache.myfaces.shared_impl.renderkit.html.HTML;
031: import org.apache.myfaces.shared_impl.renderkit.html.HtmlRenderer;
032: import org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils;
033:
034: /**
035: * @author Thomas Spiegl (latest modification by $Author: dennisbyrne $)
036: * @author Anton Koinov
037: * @author Martin Marinschek
038: * @version $Revision: 511715 $ $Date: 2007-02-26 05:05:36 +0100 (Mo, 26 Feb 2007) $
039: */
040: public class HtmlLabelRenderer extends HtmlRenderer {
041: private static final Log log = LogFactory
042: .getLog(HtmlLabelRenderer.class);
043:
044: public void encodeBegin(FacesContext facesContext,
045: UIComponent uiComponent) throws IOException {
046: super .encodeBegin(facesContext, uiComponent); //check for NP
047:
048: ResponseWriter writer = facesContext.getResponseWriter();
049:
050: encodeBefore(facesContext, writer, uiComponent);
051:
052: writer.startElement(HTML.LABEL_ELEM, uiComponent);
053: HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent,
054: facesContext);
055: HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
056: HTML.LABEL_PASSTHROUGH_ATTRIBUTES);
057:
058: String forAttr = getFor(uiComponent);
059:
060: if (forAttr != null) {
061: writer.writeAttribute(HTML.FOR_ATTR, getClientId(
062: facesContext, uiComponent, forAttr),
063: JSFAttr.FOR_ATTR);
064: } else {
065: if (log.isWarnEnabled()) {
066: log.warn("Attribute 'for' of label component with id "
067: + uiComponent.getClientId(facesContext)
068: + " is not defined");
069: }
070: }
071:
072: //MyFaces extension: Render a label text given by value
073: //TODO: Move to extended component
074: if (uiComponent instanceof ValueHolder) {
075: String text = RendererUtils.getStringValue(facesContext,
076: uiComponent);
077: if (text != null) {
078: writer.writeText(text, "value");
079: }
080: }
081:
082: writer.flush(); // close start tag
083:
084: encodeAfterStart(facesContext, writer, uiComponent);
085: }
086:
087: protected void encodeAfterStart(FacesContext facesContext,
088: ResponseWriter writer, UIComponent uiComponent)
089: throws IOException {
090: }
091:
092: protected void encodeBefore(FacesContext facesContext,
093: ResponseWriter writer, UIComponent uiComponent)
094: throws IOException {
095: }
096:
097: protected String getFor(UIComponent component) {
098: if (component instanceof HtmlOutputLabel) {
099: return ((HtmlOutputLabel) component).getFor();
100: }
101:
102: return (String) component.getAttributes().get(JSFAttr.FOR_ATTR);
103:
104: }
105:
106: protected String getClientId(FacesContext facesContext,
107: UIComponent uiComponent, String forAttr) {
108: return RendererUtils.getClientId(facesContext, uiComponent,
109: forAttr);
110: }
111:
112: public void encodeEnd(FacesContext facesContext,
113: UIComponent uiComponent) throws IOException {
114: super .encodeEnd(facesContext, uiComponent); //check for NP
115:
116: ResponseWriter writer = facesContext.getResponseWriter();
117:
118: encodeBeforeEnd(facesContext, writer, uiComponent);
119:
120: writer.endElement(HTML.LABEL_ELEM);
121:
122: encodeAfter(facesContext, writer, uiComponent);
123: }
124:
125: protected void encodeBeforeEnd(FacesContext facesContext,
126: ResponseWriter writer, UIComponent uiComponent)
127: throws IOException {
128: }
129:
130: protected void encodeAfter(FacesContext facesContext,
131: ResponseWriter writer, UIComponent uiComponent)
132: throws IOException {
133: }
134: }
|