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.standard.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UISelectManyCheckbox;
23  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
24  import org.apache.myfaces.tobago.renderkit.css.Classes;
25  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
29  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
30  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
31  import org.apache.myfaces.tobago.renderkit.util.SelectItemUtils;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  
35  import javax.faces.component.UIComponent;
36  import javax.faces.context.FacesContext;
37  import javax.faces.model.SelectItem;
38  import java.io.IOException;
39  
40  public class SelectManyCheckboxRenderer extends SelectManyRendererBase {
41  
42    @Override
43    public void encodeBeginField(final FacesContext facesContext, final UIComponent component) throws IOException {
44      final UISelectManyCheckbox select = (UISelectManyCheckbox) component;
45      final TobagoResponseWriter writer = getResponseWriter(facesContext);
46  
47      final String id = select.getClientId(facesContext);
48      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
49      final boolean disabled = select.isDisabled();
50      final boolean readonly = select.isReadonly();
51      final boolean required = select.isRequired();
52  
53      writer.startElement(HtmlElements.OL);
54      writer.writeStyleAttribute(select.getStyle());
55      writer.writeClassAttribute(Classes.create(select), select.getCustomClass());
56      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
57      if (title != null) {
58        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
59      }
60      boolean first = true;
61      final Object[] values = select.getSelectedValues();
62      final String[] submittedValues = getSubmittedValues(select);
63      int i = 0;
64      for (final SelectItem item : SelectItemUtils.getItemIterator(facesContext, select)) {
65        final boolean itemDisabled = item.isDisabled() || disabled;
66        final String itemId = id + ComponentUtils.SUB_SEPARATOR + i++;
67        writer.startElement(HtmlElements.LI);
68        writer.writeClassAttribute(
69            BootstrapClass.FORM_CHECK,
70            itemDisabled ? BootstrapClass.DISABLED: null);
71        writer.startElement(HtmlElements.LABEL);
72        writer.writeClassAttribute(BootstrapClass.FORM_CHECK_LABEL);
73        writer.startElement(HtmlElements.INPUT);
74        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX);
75        final String formattedValue = ComponentUtils.getFormattedValue(facesContext, select, item.getValue());
76        boolean checked;
77        if (submittedValues == null) {
78          checked = RenderUtils.contains(values, item.getValue());
79        } else {
80          checked = RenderUtils.contains(submittedValues, formattedValue);
81        }
82        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
83        writer.writeNameAttribute(id);
84        writer.writeIdAttribute(itemId);
85        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
86        writer.writeAttribute(HtmlAttributes.DISABLED, itemDisabled);
87        writer.writeAttribute(HtmlAttributes.READONLY, readonly);
88        writer.writeAttribute(HtmlAttributes.REQUIRED, required);
89        if (first) {
90          HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
91          first = false;
92        }
93        writer.writeAttribute(HtmlAttributes.TABINDEX, select.getTabIndex());
94        final String commands = RenderUtils.getBehaviorCommands(facesContext, select);
95        if (commands != null) {
96          writer.writeAttribute(DataAttributes.COMMANDS, commands, true);
97        } else { // old
98          HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
99        }
100       writer.endElement(HtmlElements.INPUT);
101 
102       final String label = item.getLabel();
103       if (label != null) {
104         writer.writeText(label);
105       }
106 
107       writer.endElement(HtmlElements.LABEL);
108       writer.endElement(HtmlElements.LI);
109     }
110   }
111 
112   @Override
113   public void encodeEndField(final FacesContext facesContext, final UIComponent component) throws IOException {
114     final TobagoResponseWriter writer = getResponseWriter(facesContext);
115     writer.endElement(HtmlElements.OL);
116   }
117 }