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.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UISelectOneChoice;
23  import org.apache.myfaces.tobago.renderkit.HtmlUtils;
24  import org.apache.myfaces.tobago.renderkit.SelectOneRendererBase;
25  import org.apache.myfaces.tobago.renderkit.css.Classes;
26  import org.apache.myfaces.tobago.renderkit.css.Style;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
29  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
30  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
31  import org.apache.myfaces.tobago.util.ComponentUtils;
32  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  import javax.faces.component.UIComponent;
37  import javax.faces.context.FacesContext;
38  import javax.faces.model.SelectItem;
39  import java.io.IOException;
40  import java.util.List;
41  
42  public class SelectOneChoiceRenderer extends SelectOneRendererBase {
43  
44    private static final Logger LOG = LoggerFactory.getLogger(SelectOneChoiceRenderer.class);
45  
46    public boolean getRendersChildren() {
47      return true;
48    }
49  
50    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
51      if (!(component instanceof UISelectOneChoice)) {
52        LOG.error("Wrong type: Need " + UISelectOneChoice.class.getName()
53            + ", but was " + component.getClass().getName());
54        return;
55      }
56  
57      UISelectOneChoice select = (UISelectOneChoice) component;
58      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
59  
60      String id = select.getClientId(facesContext);
61      List<SelectItem> items = RenderUtils.getSelectItems(select);
62      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
63      boolean disabled = items.size() == 0 || select.isDisabled() || select.isReadonly();
64  
65      writer.startElement(HtmlElements.SELECT, select);
66      writer.writeNameAttribute(id);
67      writer.writeIdAttribute(id);
68      HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
69      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
70      Integer tabIndex = select.getTabIndex();
71      if (tabIndex != null) {
72        writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
73      }
74      Style style = new Style(facesContext, select);
75      writer.writeStyleAttribute(style);
76      writer.writeClassAttribute(Classes.create(select));
77      if (title != null) {
78        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
79      }
80      String onchange = HtmlUtils.generateOnchange(select, facesContext);
81      if (onchange != null) {
82        writer.writeAttribute(HtmlAttributes.ONCHANGE, onchange, true);
83      }
84      HtmlRendererUtils.renderCommandFacet(select, facesContext , writer);
85      HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
86      
87      Object[] values = {select.getValue()};
88  
89      HtmlRendererUtils.renderSelectItems(select, items, values, writer, facesContext);
90  
91      writer.endElement(HtmlElements.SELECT);
92      super.encodeEnd(facesContext, select);
93    }
94  }