001 package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements. See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020 /*
021 * Created 07.02.2003 16:00:00.
022 * $Id: SelectOneListboxRenderer.java 644072 2008-04-02 21:14:53Z bommel $
023 */
024
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_DISABLED;
028 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_HEIGHT;
029 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_REQUIRED;
030 import org.apache.myfaces.tobago.util.ComponentUtil;
031 import org.apache.myfaces.tobago.component.UISelectOneListbox;
032 import org.apache.myfaces.tobago.renderkit.SelectOneRendererBase;
033 import org.apache.myfaces.tobago.renderkit.util.RenderUtil;
034 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
035 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
036 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtil;
037 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
038
039 import javax.faces.component.UIComponent;
040 import javax.faces.context.FacesContext;
041 import javax.faces.model.SelectItem;
042 import java.io.IOException;
043 import java.util.List;
044
045 public class SelectOneListboxRenderer extends SelectOneRendererBase {
046
047 private static final Log LOG = LogFactory.getLog(SelectOneListboxRenderer.class);
048
049 public boolean getRendersChildren() {
050 return true;
051 }
052
053 public int getComponentExtraWidth(FacesContext facesContext, UIComponent component) {
054 return 0;
055 }
056
057 public int getFixedHeight(FacesContext facesContext, UIComponent component) {
058 int fixedHeight = -1;
059 String height = (String) component.getAttributes().get(ATTR_HEIGHT);
060 if (height != null) {
061 try {
062 fixedHeight = Integer.parseInt(height.replaceAll("\\D", ""));
063 } catch (NumberFormatException e) {
064 LOG.warn("Can't parse " + height + " to int");
065 }
066 }
067
068 if (fixedHeight == -1) {
069 fixedHeight = super.getFixedHeight(facesContext, component);
070 }
071 return fixedHeight;
072 }
073
074 public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
075 if (!(component instanceof UISelectOneListbox)) {
076 LOG.error("Wrong type: Need " + UISelectOneListbox.class.getName() + ", but was "
077 + component.getClass().getName());
078 return;
079 }
080
081 TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
082
083 UISelectOneListbox selectOne = (UISelectOneListbox) component;
084 List<SelectItem> items = RenderUtil.getSelectItems(selectOne);
085
086 writer.startElement(HtmlConstants.SELECT, selectOne);
087 String clientId = selectOne.getClientId(facesContext);
088 writer.writeNameAttribute(clientId);
089 writer.writeIdAttribute(clientId);
090 writer.writeAttribute(HtmlAttributes.DISABLED, ComponentUtil.getBooleanAttribute(selectOne, ATTR_DISABLED));
091 Integer tabIndex = selectOne.getTabIndex();
092 if (tabIndex != null) {
093 writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
094 }
095 writer.writeStyleAttribute();
096 writer.writeClassAttribute();
097 HtmlRendererUtil.renderTip(selectOne, writer);
098 writer.writeAttribute(HtmlAttributes.SIZE, 2); // should be greater 1
099 if (!ComponentUtil.getBooleanAttribute(selectOne, ATTR_REQUIRED)) {
100 writer.writeAttribute(HtmlAttributes.ONCHANGE, "Tobago.selectOneListboxChange(this)", false);
101 writer.writeAttribute(HtmlAttributes.ONCLICK, "Tobago.selectOneListboxClick(this)", false);
102 }
103
104 Object[] values = {selectOne.getValue()};
105
106 HtmlRendererUtil.renderSelectItems(selectOne, items, values, writer, facesContext);
107
108 writer.endElement(HtmlConstants.SELECT);
109 super.encodeEnd(facesContext, selectOne);
110 HtmlRendererUtil.checkForCommandFacet(selectOne, facesContext, writer);
111 }
112
113
114 }
115