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.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.context.Markup;
23  import org.apache.myfaces.tobago.internal.component.AbstractUISelectOneChoice;
24  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
25  import org.apache.myfaces.tobago.internal.util.SelectItemUtils;
26  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
27  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
32  
33  import javax.faces.context.FacesContext;
34  import javax.faces.model.SelectItem;
35  import java.io.IOException;
36  
37  public class SelectOneChoiceRenderer<T extends AbstractUISelectOneChoice> extends SelectOneRendererBase<T> {
38  
39    @Override
40    public HtmlElements getComponentTag() {
41      return HtmlElements.TOBAGO_SELECT_ONE_CHOICE;
42    }
43  
44    @Override
45    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
46      if (isInside(facesContext, HtmlElements.TOBAGO_IN)) {
47        encodeBeginField(facesContext, component);
48      } else {
49        super.encodeBeginInternal(facesContext, component);
50      }
51    }
52  
53    @Override
54    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
55      if (isInside(facesContext, HtmlElements.TOBAGO_IN)) {
56        encodeEndField(facesContext, component);
57      } else {
58        super.encodeEndInternal(facesContext, component);
59      }
60    }
61  
62    @Override
63    public boolean getRendersChildren() {
64      return true;
65    }
66  
67    @Override
68    protected void encodeBeginField(final FacesContext facesContext, final T component) throws IOException {
69      final TobagoResponseWriter writer = getResponseWriter(facesContext);
70  
71      final String clientId = component.getClientId(facesContext);
72      final String fieldId = getFieldId(facesContext, component);
73      final Iterable<SelectItem> items = SelectItemUtils.getItemIterator(facesContext, component);
74      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
75      final boolean disabled = !items.iterator().hasNext() || component.isDisabled() || component.isReadonly();
76      final Markup markup = component.getMarkup();
77  
78      writer.startElement(HtmlElements.SELECT);
79      writer.writeIdAttribute(fieldId);
80      writer.writeNameAttribute(clientId);
81      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
82      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
83      writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
84  
85      writer.writeClassAttribute(
86          TobagoClass.SELECT_ONE_CHOICE,
87          TobagoClass.SELECT_ONE_CHOICE.createMarkup(markup),
88          isInside(facesContext, HtmlElements.TOBAGO_IN) ? BootstrapClass.FORM_SELECT : BootstrapClass.FORM_CONTROL,
89          BootstrapClass.borderColor(ComponentUtils.getMaximumSeverity(component)),
90          component.getCustomClass());
91      if (title != null) {
92        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
93      }
94      renderFocus(clientId, component.isFocus(), component.isError(), facesContext, writer);
95  
96      HtmlRendererUtils.renderSelectItems(component, TobagoClass.SELECT_ONE_CHOICE__OPTION, items, component.getValue(),
97          (String) component.getSubmittedValue(), writer, facesContext);
98    }
99  
100   @Override
101   protected void encodeEndField(final FacesContext facesContext, final T component) throws IOException {
102     final TobagoResponseWriter writer = getResponseWriter(facesContext);
103 
104     writer.endElement(HtmlElements.SELECT);
105 
106     encodeBehavior(writer, facesContext, component);
107   }
108 
109   @Override
110   protected String getFieldId(final FacesContext facesContext, final T component) {
111     return isInside(facesContext, HtmlElements.TOBAGO_IN)
112         ? component.getClientId(facesContext)
113         : component.getFieldId(facesContext);
114   }
115 }