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      int i = 0;
81      for (final SelectItem item : items) {
82        final String itemId = id + ComponentUtils.SUB_SEPARATOR + i++;
83        writer.startElement(HtmlElements.LI, select);
84        writer.startElement(HtmlElements.INPUT, select);
85        writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO, false);
86        final boolean checked = ObjectUtils.equals(item.getValue(), value);
87        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
88        writer.writeNameAttribute(id);
89        writer.writeIdAttribute(itemId);
90        final String formattedValue = RenderUtils.getFormattedValue(facesContext, select, item.getValue());
91        writer.writeAttribute(HtmlAttributes.VALUE, formattedValue, true);
92        writer.writeAttribute(HtmlAttributes.DISABLED, item.isDisabled() || disabled);
93        writer.writeAttribute(HtmlAttributes.READONLY, readonly);
94        writer.writeAttribute(HtmlAttributes.REQUIRED, required);
95        if (first) {
96          HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
97          first = false;
98        }
99        final Integer tabIndex = select.getTabIndex();
100       if (tabIndex != null) {
101         writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
102       }
103       HtmlRendererUtils.renderCommandFacet(select, itemId, facesContext, writer);
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, select);
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.startElement(HtmlElements.LABEL, select);
122         writer.writeAttribute(HtmlAttributes.FOR, itemId, false);
123         writer.writeText(label);
124         writer.endElement(HtmlElements.LABEL);
125       }
126 
127       writer.endElement(HtmlElements.LI);
128     }
129     writer.endElement(HtmlElements.OL);
130 
131   }
132 
133   @Override
134   public Measure getHeight(final FacesContext facesContext, final Configurable component) {
135     final UISelectOneRadio select = (UISelectOneRadio) component;
136     final Measure heightOfOne = super.getHeight(facesContext, component);
137     if (select.isInline()) {
138       return heightOfOne;
139     } else {
140       int count = 0;
141       for(SelectItem ignored : SelectItemUtils.getItemIterator(facesContext, (UISelectOne) component)) {
142         count++;
143       }
144       return heightOfOne.multiply(count);
145     }
146   }
147 }