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: SelectManyListboxRenderer.java 697499 2008-09-21 12:50:11Z weber $
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_READONLY;
030    import org.apache.myfaces.tobago.util.ComponentUtil;
031    import org.apache.myfaces.tobago.component.UISelectManyListbox;
032    import org.apache.myfaces.tobago.renderkit.SelectManyRendererBase;
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.Arrays;
044    import java.util.List;
045    
046    public class SelectManyListboxRenderer extends SelectManyRendererBase {
047    
048      private static final Log LOG = LogFactory.getLog(SelectManyListboxRenderer.class);
049    
050      public boolean getRendersChildren() {
051        return true;
052      }
053    
054      public int getComponentExtraWidth(FacesContext facesContext, UIComponent component) {
055        return 0;
056      }
057    
058      public int getLabelWidth(FacesContext facesContext, UIComponent component) {
059        return getConfiguredValue(facesContext, component, "labelWidth");
060      }
061    
062      public int getFixedHeight(FacesContext facesContext, UIComponent component) {
063        int fixedHeight = -1;
064        String height = (String) component.getAttributes().get(ATTR_HEIGHT);
065        if (height != null) {
066          try {
067            fixedHeight = Integer.parseInt(height.replaceAll("\\D", ""));
068          } catch (NumberFormatException e) {
069            LOG.warn("Can't parse " + height + " to int");
070          }
071        }
072    
073        if (fixedHeight == -1) {
074          fixedHeight = super.getFixedHeight(facesContext, component);
075        }
076        return fixedHeight;
077      }
078    
079      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
080        if (!(component instanceof UISelectManyListbox)) {
081          LOG.error("Wrong type: Need " + UISelectManyListbox.class.getName() + ", but was "
082              + component.getClass().getName());
083          return;
084        }
085    
086        UISelectManyListbox selectMany = (UISelectManyListbox) component;
087    
088        List<SelectItem> items = RenderUtil.getSelectItems(selectMany);
089    
090        if (LOG.isDebugEnabled()) {
091          LOG.debug("items.size() = '" + items.size() + "'");
092        }
093    
094        TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
095        String title = HtmlRendererUtil.getTitleFromTipAndMessages(facesContext, selectMany);
096        writer.startElement(HtmlConstants.SELECT, selectMany);
097        String clientId = selectMany.getClientId(facesContext);
098        writer.writeNameAttribute(clientId);
099        writer.writeIdAttribute(clientId);
100        boolean renderDisabled = ComponentUtil.getBooleanAttribute(selectMany, ATTR_DISABLED)
101            || ComponentUtil.getBooleanAttribute(selectMany, ATTR_READONLY);
102          writer.writeAttribute(HtmlAttributes.DISABLED, renderDisabled);
103        Integer tabIndex = selectMany.getTabIndex();
104        if (tabIndex != null) {
105          writer.writeAttribute(HtmlAttributes.TABINDEX, tabIndex);
106        }
107        writer.writeStyleAttribute();
108        writer.writeClassAttribute();
109        writer.writeAttribute(HtmlAttributes.MULTIPLE, HtmlAttributes.MULTIPLE, false);
110        if (title != null) {
111          writer.writeAttribute(HtmlAttributes.TITLE, title, true);
112        }
113        Object[] values = selectMany.getSelectedValues();
114        if (LOG.isDebugEnabled()) {
115          LOG.debug("values = '" + Arrays.toString(values) + "'");
116        }
117        HtmlRendererUtil.renderSelectItems(selectMany, items, values, writer, facesContext);
118    
119        writer.endElement(HtmlConstants.SELECT);
120        HtmlRendererUtil.checkForCommandFacet(selectMany, facesContext, writer);
121      }
122    
123    }
124