Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2010, Red Hat, Inc. and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.richfaces.renderkit;
 
 import java.util.List;
 import java.util.Map;
 
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.FacesContext;
 import  javax.faces.context.ResponseWriter;
 import  javax.faces.model.SelectItem;
 

Author(s):
abelevich
Brian Leathem
 
 public final class SelectHelper {
     public static final String OPTIONS_SHOWCONTROL = "showControl";
     public static final String OPTIONS_LIST_ITEMS = "items";
     public static final String OPTIONS_ENABLE_MANUAL_INPUT = "enableManualInput";
     public static final String OPTIONS_LIST_SELECT_FIRST = "selectFirst";
     public static final String OPTIONS_INPUT_DEFAULT_LABEL = "defaultLabel";
     public static final Map<StringComponentAttributeSELECT_LIST_HANDLER_ATTRIBUTES =
             Collections.unmodifiableMap(ComponentAttribute.createMap(
             new ComponentAttribute(HtmlConstants.ONCLICK_ATTRIBUTE).setEventNames("listclick").setComponentAttributeName("onlistclick"),
             new ComponentAttribute(HtmlConstants.ONDBLCLICK_ATTRIBUTE).setEventNames("listdblclick").setComponentAttributeName("onlistdblclick"),
             new ComponentAttribute(HtmlConstants.ONMOUSEDOWN_ATTRIBUTE).setEventNames("listmousedown").setComponentAttributeName("onlistmousedown"),
             new ComponentAttribute(HtmlConstants.ONMOUSEUP_ATTRIBUTE).setEventNames("listmouseup").setComponentAttributeName("onlistmouseup"),
             new ComponentAttribute(HtmlConstants.ONMOUSEOVER_ATTRIBUTE).setEventNames("listmouseover").setComponentAttributeName("onlistmouseover"),
             new ComponentAttribute(HtmlConstants.ONMOUSEMOVE_ATTRIBUTE).setEventNames("listmousemove").setComponentAttributeName("onlistmousemove"),
             new ComponentAttribute(HtmlConstants.ONMOUSEOUT_ATTRIBUTE).setEventNames("listmouseout").setComponentAttributeName("onlistmouseout"),
             new ComponentAttribute(HtmlConstants.ONKEYPRESS_ATTRIBUTE).setEventNames("listkeypress").setComponentAttributeName("onlistkeypress"),
             new ComponentAttribute(HtmlConstants.ONKEYDOWN_ATTRIBUTE).setEventNames("listkeydown").setComponentAttributeName("onlistkeydown"),
             new ComponentAttribute(HtmlConstants.ONKEYUP_ATTRIBUTE).setEventNames("listkeyup").setComponentAttributeName("onlistkeyup")));
 
     private SelectHelper() {
     }
 
     public static List<ClientSelectItemgetConvertedSelectItems(FacesContext facesContext, UIComponent component) {
         Iterator<SelectItem> selectItems = SelectUtils.getSelectItems(facesContextcomponent);
         List<ClientSelectItemclientSelectItems = new ArrayList<ClientSelectItem>();
 
         while (selectItems.hasNext()) {
             SelectItem selectItem = selectItems.next();
             clientSelectItems.add(generateClientSelectItem(facesContext,componentselectItem, 0, false));
         }
         return clientSelectItems;
     }
 
     public static ClientSelectItem generateClientSelectItem(FacesContext facesContext, UIComponent component, SelectItem selectItemint sortOrderboolean selected) {
         String convertedStringValue = InputUtils.getConvertedStringValue(facesContextcomponentselectItem.getValue());
         String label = selectItem.getLabel();
         ClientSelectItem clientSelectItem = new ClientSelectItem(selectItemconvertedStringValuelabelsortOrderselected);
         return clientSelectItem;
     }
 
     public static void encodeItems(FacesContext facesContext, UIComponent componentList<ClientSelectItemclientSelectItems,
         String itemHtmlElementString defaultItemCssthrows IOException {
         AbstractSelectComponent select = (AbstractSelectComponentcomponent;
         if (clientSelectItems != null && !clientSelectItems.isEmpty()) {
             ResponseWriter writer = facesContext.getResponseWriter();
             String clientId = component.getClientId(facesContext);
             int i = 0;
             for (ClientSelectItem clientSelectItem : clientSelectItems) {
                 String itemClientId = clientId + "Item" + (i++);
                 clientSelectItem.setClientId(itemClientId);
 
                 writer.startElement(itemHtmlElementselect);
                 writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, itemClientIdnull);
 
                writer.writeAttribute(HtmlConstants.CLASS_ATTRIBUTE,
                    HtmlUtil.concatClasses(defaultItemCssselect.getItemClass()), null);
                String label = clientSelectItem.getLabel();
                if (label != null && label.trim().length() > 0) {
                    writer.writeText(labelnull);
                } else {
                    writer.write("\u00a0");
                }
                writer.endElement(itemHtmlElement);
            }
        }
    }
    public static String getSelectInputLabel(FacesContext facesContext, UIComponent component) {
        AbstractSelectComponent select = (AbstractSelectComponentcomponent;
        Object value = select.getSubmittedValue();
        String label = null;
        if (value == null) {
            value = select.getValue();
            if (value != null) {
                Iterator<SelectItem> items = SelectUtils.getSelectItems(facesContextcomponent);
                while (items.hasNext()) {
                    SelectItem item = items.next();
                    if (value.equals(item.getValue())) {
                        label = item.getLabel();
                    }
                }
            }
        }
        return label;
    }
    public static void addSelectCssToOptions(AbstractSelectComponent abstractSelectMap<StringObjectoptions,
        String[] defaultCss) {
        String itemCss = abstractSelect.getItemClass();
        if (itemCss != null && itemCss.trim().length() > 0) {
            options.put(., HtmlUtil.concatClasses(defaultCss[0], itemCss));
        }
        String selectItemCss = abstractSelect.getSelectItemClass();
        if (selectItemCss != null && selectItemCss.trim().length() > 0) {
            options.put(., HtmlUtil.concatClasses(defaultCss[1], selectItemCss));
        }
        String listCss = abstractSelect.getListClass();
        if (listCss != null && listCss.trim().length() > 0) {
            options.put(., HtmlUtil.concatClasses(defaultCss[2], listCss));
        }
    }
New to GrepCode? Check out our FAQ X