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.config.Configurable;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26  import org.apache.myfaces.tobago.internal.util.ObjectUtils;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.renderkit.SelectOneRendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.css.Style;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
34  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
35  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
36  import org.apache.myfaces.tobago.renderkit.util.SelectItemUtils;
37  import org.apache.myfaces.tobago.util.ComponentUtils;
38  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
39  
40  import javax.faces.component.UIComponent;
41  import javax.faces.component.UISelectOne;
42  import javax.faces.context.FacesContext;
43  import javax.faces.model.SelectItem;
44  import java.io.IOException;
45  
46  public class SelectOneRadioRenderer extends SelectOneRendererBase {
47  
48    public void prepareRender(final FacesContext facesContext, final UIComponent component) throws IOException {
49      final UISelectOne select = (UISelectOne) component;
50      super.prepareRender(facesContext, select);
51      if (select instanceof UISelectOneRadio && ((UISelectOneRadio) select).isInline()) {
52        ComponentUtils.addCurrentMarkup(((UISelectOneRadio) select), Markup.INLINE);
53      }
54    }
55  
56    public void encodeEnd(final FacesContext facesContext, final UIComponent component) throws IOException {
57      final UISelectOneRadio select = (UISelectOneRadio) component;
58      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
59  
60      final String id = select.getClientId(facesContext);
61      final Iterable<SelectItem> items = SelectItemUtils.getItemIterator(facesContext, select);
62      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
63      final boolean disabled = select.isDisabled();
64      final boolean readonly = select.isReadonly();
65      final Style style = new Style(facesContext, select);
66      final boolean required = select.isRequired();
67      // fixme: use CSS, not the Style Attribute for "display"
68      style.setDisplay(null);
69  
70      writer.startElement(HtmlElements.OL, select);
71      writer.writeIdAttribute(id);
72      writer.writeStyleAttribute(style);
73      writer.writeClassAttribute(Classes.create(select));
74      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
75      if (title != null) {
76        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
77      }
78      boolean first = true;
79      final Object value = select.getValue();
80      final String submittedValue = (String) select.getSubmittedValue();
81      int i = 0;
82      for (final SelectItem item : items) {
83        final String itemId = id + ComponentUtils.SUB_SEPARATOR + i++;
84        writer.startElement(HtmlElements.LI, select);
85        writer.startElement(HtmlElements.INPUT, select);
86        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO, false);
87        final String formattedValue = RenderUtils.getFormattedValue(facesContext, select, item.getValue());
88        boolean checked;
89        if (submittedValue == null) {
90          checked = ObjectUtils.equals(item.getValue(), value);
91        } else {
92          checked = ObjectUtils.equals(formattedValue, submittedValue);
93        }
94        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
95        writer.writeNameAttribute(id);
96        writer.writeIdAttribute(itemId);
97        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
98        writer.writeAttribute(HtmlAttributes.DISABLED, item.isDisabled() || disabled);
99        writer.writeAttribute(HtmlAttributes.READONLY, readonly);
100       writer.writeAttribute(HtmlAttributes.REQUIRED, required);
101       if (first) {
102         HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
103         first = false;
104       }
105       final Integer tabIndex = select.getTabIndex();
106       if (tabIndex != null) {
107         writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
108       }
109       HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
110       writer.endElement(HtmlElements.INPUT);
111 
112       if (item instanceof org.apache.myfaces.tobago.model.SelectItem) {
113         org.apache.myfaces.tobago.model.SelectItem tobagoItem = (org.apache.myfaces.tobago.model.SelectItem) item;
114         final String image = tobagoItem.getImage();
115         if (image != null) {
116           final String imageToRender
117               = ResourceManagerUtils.getImageOrDisabledImageWithPath(facesContext, image, item.isDisabled());
118           writer.startElement(HtmlElements.IMG, select);
119           writer.writeAttribute(HtmlAttributes.SRC, imageToRender, true);
120           writer.writeAttribute(HtmlAttributes.ALT, "", false);
121           writer.endElement(HtmlElements.IMG);
122         }
123       }
124 
125       final String label = item.getLabel();
126       if (label != null) {
127         writer.startElement(HtmlElements.LABEL, select);
128         writer.writeAttribute(HtmlAttributes.FOR, itemId, false);
129         writer.writeText(label);
130         writer.endElement(HtmlElements.LABEL);
131       }
132 
133       writer.endElement(HtmlElements.LI);
134     }
135     writer.endElement(HtmlElements.OL);
136 
137   }
138 
139   @Override
140   public Measure getHeight(final FacesContext facesContext, final Configurable component) {
141     final UISelectOneRadio select = (UISelectOneRadio) component;
142     final Measure heightOfOne = super.getHeight(facesContext, component);
143     if (select.isInline()) {
144       return heightOfOne;
145     } else {
146       int count = 0;
147       for(SelectItem ignored : SelectItemUtils.getItemIterator(facesContext, (UISelectOne) component)) {
148         count++;
149       }
150       return heightOfOne.multiply(count);
151     }
152   }
153 }