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.UISelectOneRadio;
23  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
24  import org.apache.myfaces.tobago.internal.util.ObjectUtils;
25  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
26  import org.apache.myfaces.tobago.renderkit.css.Classes;
27  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
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.renderkit.util.RenderUtils;
33  import org.apache.myfaces.tobago.renderkit.util.SelectItemUtils;
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.context.FacesContext;
39  import javax.faces.model.SelectItem;
40  import java.io.IOException;
41  
42  public class SelectOneRadioRenderer extends SelectOneRendererBase {
43  
44    @Override
45    protected void encodeBeginField(FacesContext facesContext, UIComponent component) throws IOException {
46      final UISelectOneRadio select = (UISelectOneRadio) component;
47      final TobagoResponseWriter writer = getResponseWriter(facesContext);
48  
49      final String id = select.getClientId(facesContext);
50      final Iterable<SelectItem> items = SelectItemUtils.getItemIterator(facesContext, select);
51      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
52      final boolean disabled = select.isDisabled();
53      final boolean readonly = select.isReadonly();
54      final boolean required = select.isRequired();
55  
56      writer.startElement(HtmlElements.OL);
57      writer.writeStyleAttribute(select.getStyle());
58      writer.writeClassAttribute(Classes.create(select), select.getCustomClass());
59      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
60      if (title != null) {
61        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
62      }
63      boolean first = true;
64      final Object value = select.getValue();
65      final String submittedValue = (String) select.getSubmittedValue();
66      int i = 0;
67      for (final SelectItem item : items) {
68        final boolean itemDisabled = item.isDisabled() || disabled;
69        final String itemId = id + ComponentUtils.SUB_SEPARATOR + i++;
70        writer.startElement(HtmlElements.LI);
71        if (itemDisabled) {
72          writer.writeClassAttribute(BootstrapClass.RADIO, BootstrapClass.DISABLED);
73        } else {
74          writer.writeClassAttribute(BootstrapClass.RADIO);
75        }
76        writer.startElement(HtmlElements.LABEL);
77        writer.startElement(HtmlElements.INPUT);
78        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO);
79        final String formattedValue = ComponentUtils.getFormattedValue(facesContext, select, item.getValue());
80        boolean checked;
81        if (submittedValue == null) {
82          checked = ObjectUtils.equals(item.getValue(), value);
83        } else {
84          checked = ObjectUtils.equals(formattedValue, submittedValue);
85        }
86        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
87        writer.writeNameAttribute(id);
88        writer.writeIdAttribute(itemId);
89        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
90        writer.writeAttribute(HtmlAttributes.DISABLED, itemDisabled);
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        writer.writeAttribute(HtmlAttributes.TABINDEX, select.getTabIndex());
98        final String commands = RenderUtils.getBehaviorCommands(facesContext, select);
99        if (commands != null) {
100         writer.writeAttribute(DataAttributes.COMMANDS, commands, true);
101       } else { // old
102         HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
103       }
104       writer.endElement(HtmlElements.INPUT);
105 
106       if (item instanceof org.apache.myfaces.tobago.model.SelectItem) {
107         org.apache.myfaces.tobago.model.SelectItem tobagoItem = (org.apache.myfaces.tobago.model.SelectItem) item;
108         final String image = tobagoItem.getImage();
109         if (image != null) {
110           final String imageToRender
111               = ResourceManagerUtils.getImageOrDisabledImageWithPath(facesContext, image, item.isDisabled());
112           writer.startElement(HtmlElements.IMG);
113           writer.writeAttribute(HtmlAttributes.SRC, imageToRender, true);
114           writer.writeAttribute(HtmlAttributes.ALT, "", false);
115           writer.endElement(HtmlElements.IMG);
116         }
117       }
118 
119       final String label = item.getLabel();
120       if (label != null) {
121         writer.writeText(label);
122       }
123 
124       writer.endElement(HtmlElements.LABEL);
125       writer.endElement(HtmlElements.LI);
126     }
127   }
128 
129   @Override
130   protected void encodeEndField(FacesContext facesContext, UIComponent component) throws IOException {
131     final TobagoResponseWriter writer = getResponseWriter(facesContext);
132     writer.endElement(HtmlElements.OL);
133   }
134 }