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