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