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.UISelectBooleanCheckbox;
23  import org.apache.myfaces.tobago.internal.util.AccessKeyMap;
24  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
25  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
26  import org.apache.myfaces.tobago.renderkit.css.Classes;
27  import org.apache.myfaces.tobago.renderkit.css.Style;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
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 SelectBooleanCheckboxRenderer extends LayoutComponentRendererBase {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(SelectBooleanCheckboxRenderer.class);
45  
46    public void decode(FacesContext facesContext, UIComponent component) {
47  
48      UIInput input = (UIInput) component;
49  
50      if (ComponentUtils.isOutputOnly(input)) {
51        return;
52      }
53  
54      String newValue = (String) facesContext.getExternalContext()
55          .getRequestParameterMap().get(input.getClientId(facesContext));
56  
57      if (LOG.isDebugEnabled()) {
58        LOG.debug("new value = '" + newValue + "'");
59      }
60  
61      input.setSubmittedValue("true".equals(newValue) ? "true" : "false");
62    }
63  
64  //  public Object getConvertedValue(
65  //      FacesContext context, UIComponent component, Object submittedValue)
66  //      throws ConverterException {
67  //
68  //      return Boolean.valueOf((String)submittedValue);
69  //  }
70  
71    //
72    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
73  
74      UISelectBooleanCheckbox select = (UISelectBooleanCheckbox) component;
75      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
76  
77      String id = select.getClientId(facesContext);
78      String currentValue = getCurrentValue(facesContext, select);
79      boolean checked = "true".equals(currentValue);
80      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
81  
82      writer.startElement(HtmlElements.DIV, select);
83      writer.writeStyleAttribute(new Style(facesContext, select));
84      writer.writeClassAttribute(Classes.create(select));
85      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
86      if (title != null) {
87        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
88      }
89  
90      writer.startElement(HtmlElements.INPUT, select);
91      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX, false);
92      writer.writeAttribute(HtmlAttributes.VALUE, "true", false);
93      writer.writeNameAttribute(id);
94      writer.writeIdAttribute(id);
95      writer.writeAttribute(HtmlAttributes.CHECKED, checked);
96      writer.writeAttribute(HtmlAttributes.READONLY, select.isReadonly());
97      writer.writeAttribute(HtmlAttributes.DISABLED, select.isDisabled());
98      writer.writeAttribute(HtmlAttributes.REQUIRED, select.isRequired());
99  
100     HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
101 
102     Integer tabIndex = select.getTabIndex();
103     if (tabIndex != null) {
104       writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
105     }
106     HtmlRendererUtils.renderCommandFacet(select, facesContext, writer);
107     writer.endElement(HtmlElements.INPUT);
108 
109     String label = select.getItemLabel();
110     if (label == null) {
111       label = select.getLabel(); // compatibility since TOBAGO-1093
112     }
113     if (label != null) {
114       LabelWithAccessKey labelWithAccessKey = new LabelWithAccessKey(label);
115       writer.startElement(HtmlElements.LABEL, select);
116       writer.writeAttribute(HtmlAttributes.FOR, id, false);
117       if (labelWithAccessKey.getAccessKey() != null) {
118         writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(labelWithAccessKey.getAccessKey()), false);
119       }
120       HtmlRendererUtils.writeLabelWithAccessKey(writer, labelWithAccessKey);
121       if (labelWithAccessKey.getAccessKey() != null) {
122         if (LOG.isInfoEnabled()
123             && !AccessKeyMap.addAccessKey(facesContext, labelWithAccessKey.getAccessKey())) {
124           LOG.info("duplicated accessKey : " + labelWithAccessKey.getAccessKey());
125         }
126         HtmlRendererUtils.addClickAcceleratorKey(facesContext, id, labelWithAccessKey.getAccessKey());
127       }
128       writer.endElement(HtmlElements.LABEL);
129     }
130 
131     writer.endElement(HtmlElements.DIV);
132 
133   }
134 }