View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UILabel;
23  import org.apache.myfaces.tobago.context.Markup;
24  import org.apache.myfaces.tobago.internal.util.AccessKeyMap;
25  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
26  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
27  import org.apache.myfaces.tobago.renderkit.css.Classes;
28  import org.apache.myfaces.tobago.renderkit.css.Style;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.component.UIInput;
39  import javax.faces.context.FacesContext;
40  import java.io.IOException;
41  
42  public class LabelRenderer extends LayoutComponentRendererBase {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(LabelRenderer.class);
45  
46    @Override
47    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
48      super.prepareRender(facesContext, component);
49  
50      ComponentUtils.evaluateAutoFor(component, UIInput.class);
51  
52      // adding the markups from the corresponding input component
53      final UILabel label = (UILabel) component;
54      final UIComponent corresponding = ComponentUtils.findFor(label);
55      if (corresponding != null) {
56        Markup markup = label.getCurrentMarkup();
57        markup = ComponentUtils.updateMarkup(corresponding, markup);
58        label.setCurrentMarkup(markup);
59      }
60    }
61  
62    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
63  
64      UILabel label = (UILabel) component;
65      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
66  
67      String forValue = ComponentUtils.findClientIdFor(label, facesContext);
68  
69      String clientId = label.getClientId(facesContext);
70      writer.startElement(HtmlElements.LABEL, label);
71      HtmlRendererUtils.writeDataAttributes(facesContext, writer, label);
72      HtmlRendererUtils.renderDojoDndItem(label, writer, true);
73      final Classes classes = Classes.create(label);
74      writer.writeClassAttribute(classes);
75      writer.writeStyleAttribute(new Style(facesContext, label));
76      writer.writeIdAttribute(clientId);
77      if (forValue != null) {
78        writer.writeAttribute(HtmlAttributes.FOR, forValue, false);
79      }
80      HtmlRendererUtils.renderTip(label, writer);
81  
82      encodeTextContent(facesContext, writer, label);
83  
84      writer.endElement(HtmlElements.LABEL);
85    }
86  
87    /** Encodes the text inside of the label. 
88     * Can be overwritten in other themes.
89     */
90    protected void encodeTextContent(FacesContext facesContext, TobagoResponseWriter writer, UILabel label) 
91        throws IOException {
92      String clientId = label.getClientId(facesContext);
93      LabelWithAccessKey key = new LabelWithAccessKey(label);
94      if (key.getAccessKey() != null) {
95        writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(key.getAccessKey()), false);
96      }
97      if (key.getText() != null) {
98        HtmlRendererUtils.writeLabelWithAccessKey(writer, key);
99      }
100     if (key.getAccessKey() != null) {
101       if (LOG.isInfoEnabled()
102           && !AccessKeyMap.addAccessKey(facesContext, key.getAccessKey())) {
103         LOG.info("Duplicated accessKey : " + key.getAccessKey());
104       }
105       HtmlRendererUtils.addClickAcceleratorKey(facesContext, clientId, key.getAccessKey());
106     }
107   }
108 }