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 = HtmlRendererUtils.getTobagoResponseWriter(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        if (itemDisabled) {
69          writer.writeClassAttribute(BootstrapClass.CHECKBOX, BootstrapClass.DISABLED);
70        } else {
71          writer.writeClassAttribute(BootstrapClass.CHECKBOX);
72        }
73        writer.startElement(HtmlElements.LABEL);
74        writer.startElement(HtmlElements.INPUT);
75        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX);
76        final String formattedValue = RenderUtils.getFormattedValue(facesContext, select, item.getValue());
77        boolean checked;
78        if (submittedValues == null) {
79          checked = RenderUtils.contains(values, item.getValue());
80        } else {
81          checked = RenderUtils.contains(submittedValues, formattedValue);
82        }
83        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
84        writer.writeNameAttribute(id);
85        writer.writeIdAttribute(itemId);
86        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
87        writer.writeAttribute(HtmlAttributes.DISABLED, itemDisabled);
88        writer.writeAttribute(HtmlAttributes.READONLY, readonly);
89        writer.writeAttribute(HtmlAttributes.REQUIRED, required);
90        if (first) {
91          HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
92          first = false;
93        }
94        writer.writeAttribute(HtmlAttributes.TABINDEX, select.getTabIndex());
95        final String commands = RenderUtils.getBehaviorCommands(facesContext, select);
96        if (commands != null) {
97          writer.writeAttribute(DataAttributes.COMMANDS, commands, true);
98        } else { // old
99          HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
100       }
101       writer.endElement(HtmlElements.INPUT);
102 
103       final String label = item.getLabel();
104       if (label != null) {
105         writer.writeText(label);
106       }
107 
108       writer.endElement(HtmlElements.LABEL);
109       writer.endElement(HtmlElements.LI);
110     }
111   }
112 
113   @Override
114   public void encodeEndField(final FacesContext facesContext, final UIComponent component) throws IOException {
115     final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
116     writer.endElement(HtmlElements.OL);
117   }
118 }