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.UISelectManyCheckbox;
23  import org.apache.myfaces.tobago.config.Configurable;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.layout.Measure;
26  import org.apache.myfaces.tobago.renderkit.SelectManyRendererBase;
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.HtmlInputTypes;
32  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
33  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  
37  import javax.faces.component.NamingContainer;
38  import javax.faces.component.UIComponent;
39  import javax.faces.component.UISelectMany;
40  import javax.faces.context.FacesContext;
41  import javax.faces.model.SelectItem;
42  import java.io.IOException;
43  import java.util.List;
44  
45  public class SelectManyCheckboxRenderer extends SelectManyRendererBase {
46  
47    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
48      UISelectManyCheckbox select = (UISelectManyCheckbox) component;
49      super.prepareRender(facesContext, select);
50      if (select.isInline()) {
51        ComponentUtils.addCurrentMarkup(select, Markup.INLINE);
52      }
53    }
54  
55    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
56      UISelectManyCheckbox select = (UISelectManyCheckbox) component;
57      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
58  
59      String id = select.getClientId(facesContext);
60      List<SelectItem> items = RenderUtils.getItemsToRender(select);
61      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
62      boolean disabled = select.isDisabled();
63      boolean readonly = select.isReadonly();
64      boolean required = select.isRequired();
65      Style style = new Style(facesContext, select);
66      // fixme: use CSS, not the Style Attribute for "display"
67      style.setDisplay(null);
68  
69      writer.startElement(HtmlElements.OL, select);
70      writer.writeIdAttribute(id);
71      writer.writeStyleAttribute(style);
72      writer.writeClassAttribute(Classes.create(select));
73      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
74      if (title != null) {
75        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
76      }
77      boolean first = true;
78      Object[] values = select.getSelectedValues();
79      for (SelectItem item : items) {
80        String itemId = id + NamingContainer.SEPARATOR_CHAR + NamingContainer.SEPARATOR_CHAR + item.getValue().toString();
81        writer.startElement(HtmlElements.LI, select);
82        writer.startElement(HtmlElements.INPUT, select);
83        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX, false);
84        boolean checked = RenderUtils.contains(values, item.getValue());
85        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
86        writer.writeNameAttribute(id);
87        writer.writeIdAttribute(itemId);
88        String formattedValue = RenderUtils.getFormattedValue(facesContext, select, item.getValue());
89        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
90        writer.writeAttribute(HtmlAttributes.DISABLED, item.isDisabled() || disabled);
91        writer.writeAttribute(HtmlAttributes.READONLY, readonly);
92        writer.writeAttribute(HtmlAttributes.REQUIRED, required);
93        if (first) {
94          HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
95          first = false;
96        }
97        Integer tabIndex = select.getTabIndex();
98        if (tabIndex != null) {
99          writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
100       }
101       HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
102       writer.endElement(HtmlElements.INPUT);
103 
104       String label = item.getLabel();
105       if (label != null) {
106         writer.startElement(HtmlElements.LABEL, select);
107         writer.writeAttribute(HtmlAttributes.FOR, itemId, false);
108         writer.writeText(label);
109         writer.endElement(HtmlElements.LABEL);
110       }
111 
112       writer.endElement(HtmlElements.LI);
113     }
114     writer.endElement(HtmlElements.OL);
115 
116   }
117 
118   @Override
119   public Measure getHeight(FacesContext facesContext, Configurable component) {
120     UISelectManyCheckbox select = (UISelectManyCheckbox) component;
121     Measure heightOfOne = super.getHeight(facesContext, component);
122     if (select.isInline()) {
123       return heightOfOne;
124     } else {
125       List<SelectItem> items = RenderUtils.getItemsToRender((UISelectMany) component);
126       return heightOfOne.multiply(items.size());
127     }
128   }
129 }