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.UISelectManyListbox;
23  import org.apache.myfaces.tobago.renderkit.SelectManyRendererBase;
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 SelectManyListboxRenderer extends SelectManyRendererBase {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(SelectManyListboxRenderer.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 UISelectManyListbox)) {
51        LOG.error("Wrong type: Need " + UISelectManyListbox.class.getName() 
52            + ", but was " + component.getClass().getName());
53        return;
54      }
55  
56      UISelectManyListbox select = (UISelectManyListbox) component;
57      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
58  
59      String id = select.getClientId(facesContext);
60      List<SelectItem> items = RenderUtils.getSelectItems(select);
61      boolean readonly = select.isReadonly();
62      boolean disabled = items.size() == 0 || select.isDisabled() || readonly;
63  
64      String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, select);
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      writer.writeAttribute(HtmlAttributes.READONLY, readonly);
71      writer.writeAttribute(HtmlAttributes.REQUIRED, select.isRequired());
72      HtmlRendererUtils.renderFocus(id, select.isFocus(), ComponentUtils.isError(select), facesContext, writer);
73      Integer tabIndex = select.getTabIndex();
74      if (tabIndex != null) {
75        writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
76      }
77      Style style = new Style(facesContext, select);
78      writer.writeStyleAttribute(style);
79      writer.writeClassAttribute(Classes.create(select));
80      writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, false);
81      if (title != null) {
82        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
83      }
84      HtmlRendererUtils.renderCommandFacet(select, facesContext, writer);
85      Object[] values = select.getSelectedValues();
86      HtmlRendererUtils.renderSelectItems(select, items, values, writer, facesContext);
87  
88      writer.endElement(HtmlElements.SELECT);
89    }
90  
91  }
92