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.AbstractUISelectOneListbox;
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  import java.util.List;
37  
38  public class SelectOneListboxRenderer<T extends AbstractUISelectOneListbox> extends SelectOneRendererBase<T> {
39  
40    @Override
41    public HtmlElements getComponentTag() {
42      return HtmlElements.TOBAGO_SELECT_ONE_LISTBOX;
43    }
44  
45    @Override
46    public boolean getRendersChildren() {
47      return true;
48    }
49  
50    @Override
51    public void encodeBeginField(final FacesContext facesContext, final T component) throws IOException {
52      final TobagoResponseWriter writer = getResponseWriter(facesContext);
53  
54      final String clientId = component.getClientId(facesContext);
55      final String fieldId = component.getFieldId(facesContext);
56      final List<SelectItem> items = SelectItemUtils.getItemList(facesContext, component);
57      final boolean disabled = !items.iterator().hasNext() || component.isDisabled() || component.isReadonly();
58      final Markup markup = component.getMarkup();
59      Integer size = component.getSize();
60      size = Math.max(size != null ? size : items.size(), 2); // must be > 1
61  
62      writer.startElement(HtmlElements.SELECT);
63      writer.writeIdAttribute(fieldId);
64      writer.writeNameAttribute(clientId);
65      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
66      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
67      writer.writeAttribute(HtmlAttributes.READONLY, component.isReadonly());
68      writer.writeAttribute(HtmlAttributes.REQUIRED, component.isRequired());
69      renderFocus(clientId, component.isFocus(), component.isError(), facesContext, writer);
70  
71      writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
72  
73      writer.writeClassAttribute(
74          TobagoClass.SELECT_ONE_LISTBOX,
75          TobagoClass.SELECT_ONE_LISTBOX.createMarkup(component.getMarkup()),
76          BootstrapClass.borderColor(ComponentUtils.getMaximumSeverity(component)),
77          BootstrapClass.FORM_CONTROL,
78          component.getCustomClass(),
79          markup != null && markup.contains(Markup.SPREAD) ? TobagoClass.SPREAD : null);
80      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
81      writer.writeAttribute(HtmlAttributes.TITLE, title, true);
82      writer.writeAttribute(HtmlAttributes.SIZE, size);
83      HtmlRendererUtils.renderSelectItems(component, TobagoClass.SELECT_ONE_LISTBOX__OPTION, items, component.getValue(),
84          (String) component.getSubmittedValue(), writer, facesContext);
85    }
86  
87    @Override
88    protected void encodeEndField(final FacesContext facesContext, final T component) throws IOException {
89      final TobagoResponseWriter writer = getResponseWriter(facesContext);
90  
91      writer.endElement(HtmlElements.SELECT);
92  
93      encodeBehavior(writer, facesContext, component);
94    }
95  
96    @Override
97    protected String getFieldId(final FacesContext facesContext, final T component) {
98      return component.getFieldId(facesContext);
99    }
100 }