001    package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.component.UILabel;
021    import org.apache.myfaces.tobago.context.Markup;
022    import org.apache.myfaces.tobago.internal.util.AccessKeyMap;
023    import org.apache.myfaces.tobago.internal.util.Deprecation;
024    import org.apache.myfaces.tobago.layout.LayoutBase;
025    import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
026    import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
027    import org.apache.myfaces.tobago.renderkit.css.Classes;
028    import org.apache.myfaces.tobago.renderkit.css.Style;
029    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
030    import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
031    import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
032    import org.apache.myfaces.tobago.util.ComponentUtils;
033    import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
034    import org.slf4j.Logger;
035    import org.slf4j.LoggerFactory;
036    
037    import javax.faces.component.UIComponent;
038    import javax.faces.context.FacesContext;
039    import java.io.IOException;
040    
041    public class LabelRenderer extends LayoutComponentRendererBase {
042    
043      private static final Logger LOG = LoggerFactory.getLogger(LabelRenderer.class);
044    
045      @Override
046      public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
047        super.prepareRender(facesContext, component);
048    
049        ComponentUtils.evaluateAutoFor(component);
050    
051        // adding the markups from the corresponding input component
052        final UILabel label = (UILabel) component;
053        final UIComponent corresponding = ComponentUtils.findFor(label);
054        if (corresponding != null) {
055          Markup markup = label.getCurrentMarkup();
056          markup = ComponentUtils.updateMarkup(corresponding, markup);
057          label.setCurrentMarkup(markup);
058        }
059      }
060    
061      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
062    
063        // todo: remove test after 1.5.0, then UILabel is required
064        if (!(component instanceof UILabel)) {
065          Deprecation.LOG.warn("LabelRenderer should only render UILabel but got " + component.getClass().getName()
066              + " id=" + component.getClientId(facesContext));
067        }
068        TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
069    
070        LabelWithAccessKey label = new LabelWithAccessKey(component);
071    
072        String forValue = ComponentUtils.findClientIdFor(component, facesContext);
073    
074        String clientId = component.getClientId(facesContext);
075        writer.startElement(HtmlElements.LABEL, component);
076        HtmlRendererUtils.renderDojoDndItem(component, writer, true);
077        final Classes classes = Classes.create(component);
078        writer.writeClassAttribute(classes);
079        if (component instanceof LayoutBase) {
080          Style style = new Style(facesContext, (LayoutBase) component);
081          writer.writeStyleAttribute(style);
082        }
083        writer.writeIdAttribute(clientId);
084        if (forValue != null) {
085          writer.writeAttribute(HtmlAttributes.FOR, forValue, false);
086        }
087    
088        HtmlRendererUtils.renderTip(component, writer);
089    
090        if (label.getText() != null) {
091          HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
092        }
093    
094        if (label.getAccessKey() != null) {
095          if (LOG.isInfoEnabled()
096              && !AccessKeyMap.addAccessKey(facesContext, label.getAccessKey())) {
097            LOG.info("dublicated accessKey : " + label.getAccessKey());
098          }
099          HtmlRendererUtils.addClickAcceleratorKey(facesContext, clientId, label.getAccessKey());
100        }
101        writer.endElement(HtmlElements.LABEL);
102      }
103    }