Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. Rich Faces - Natural Ajax for Java Server Faces (JSF) Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library 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 library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 package org.richfaces.component.util;
 
 import java.util.Map;
 
 import  javax.faces.application.ProjectStage;
 import  javax.faces.component.UIComponent;
 import  javax.faces.component.UISelectItem;
 import  javax.faces.component.UISelectItems;
 import  javax.faces.context.FacesContext;
 import  javax.faces.model.SelectItem;
 
 import  org.richfaces.log.Logger;
 import  org.richfaces.log.RichfacesLogger;
 
 import  com.google.common.collect.AbstractIterator;
 import  com.google.common.collect.Iterators;

Author(s):
Maksim Kaszynski
 
 public final class SelectUtils {
     private static final Logger LOG = RichfacesLogger.APPLICATION.getLogger();
 
     private static final class GenericObjectSelectItem extends SelectItem {
         private static final long serialVersionUID = -714217221281952395L;
         private static final RendererUtils UTILS = RendererUtils.getInstance();
         private static final String VAR = "var";
         private static final String ITEM_VALUE = "itemValue";
         private static final String ITEM_LABEL = "itemLabel";
         private static final String ITEM_DESCRIPTION = "itemDescription";
         private static final String ITEM_ESCAPED = "itemLabelEscaped";
         private static final String ITEM_DISABLED = "itemDisabled";
         private static final String NO_SELECTION_OPTION = "noSelectionOption";
 
         private GenericObjectSelectItem() {
         }
 
         private void updateItem(FacesContext facesContext, UIComponent sourceComponentObject value) {
             String var = (StringsourceComponent.getAttributes().get();
 
             Map<StringObjectrequestMap = facesContext.getExternalContext().getRequestMap();
             Object oldVarValue = null;
             if (var != null) {
                 oldVarValue = requestMap.put(varvalue);
             }
             try {
                 Map<StringObjectattrs = sourceComponent.getAttributes();
                 Object itemValueResult = attrs.get();
                 Object itemLabelResult = attrs.get();
                 Object itemDescriptionResult = attrs.get();
 
                 setValue(itemValueResult != null ? itemValueResult : value);
                 setLabel(itemLabelResult != null ? itemLabelResult.toString() : value.toString());
                 setDescription(itemDescriptionResult != null ? itemDescriptionResult.toString() : null);
                 setEscape(.isBooleanAttribute(sourceComponent));
                 setDisabled(.isBooleanAttribute(sourceComponent));
                 setNoSelectionOption(.isBooleanAttribute(sourceComponent));
             } finally {
                 if (var != null) {
                     if (oldVarValue != null) {
                         requestMap.put(varoldVarValue);
                     } else {
                         requestMap.remove(var);
                     }
                 }
             }
         }
     }
 
     private static final class MapItemsIterator extends AbstractIterator<SelectItem> {
         private Iterator<?> data;
 
         public MapItemsIterator(Map<?, ?> map) {
             super();
             this. = map.entrySet().iterator();
        }
        @Override
        protected SelectItem computeNext() {
            if (.hasNext()) {
                Entry<?, ?> next = (Entry<?, ?>) .next();
                return new SelectItem(next.getValue(), next.getKey().toString());
            }
            return endOfData();
        }
    }
    private static final class GenericItemsIterator extends AbstractIterator<SelectItem> {
        private FacesContext facesContext;
        private UIComponent component;
        private Iterator<?> data;
        public GenericItemsIterator(FacesContext facesContext, UIComponent componentIterator<?> data) {
            super();
            this. = facesContext;
            this. = component;
            this. = data;
        }
        @Override
        protected SelectItem computeNext() {
            if (.hasNext()) {
                Object next = .next();
                if (next instanceof SelectItem) {
                    return (SelectItem) next;
                } else {
                    GenericObjectSelectItem genericItem = new GenericObjectSelectItem();
                    genericItem.updateItem(next);
                    return genericItem;
                }
            }
            return endOfData();
        }
    }
    private static final class SelectItemsIterator extends AbstractIterator<SelectItem> {
        private Iterator<UIComponent> children;
        private Iterator<SelectItem> items = Iterators.emptyIterator();
        private FacesContext context;
        public SelectItemsIterator(FacesContext contextIterator<UIComponent> children) {
            this. = context;
            this. = children;
        }
        @Override
        protected SelectItem computeNext() {
            while (.hasNext() || .hasNext()) {
                if (.hasNext()) {
                    SelectItem nextItem = .next();
                    if (!.hasNext()) {
                        // free iterator
                         = Iterators.emptyIterator();
                    }
                    return nextItem;
                } else {
                     = createIterator(.next());
                }
            }
            return endOfData();
        }
        private Iterator<SelectItem> createUISelectItemIterator(UISelectItem selectItem) {
            Object value = selectItem.getValue();
            SelectItem result = null;
            if (value == null) {
                result = new SelectItem(selectItem.getItemValue(), selectItem.getItemLabel(), selectItem.getItemDescription(),
                    selectItem.isItemDisabled(), selectItem.isItemEscaped(), selectItem.isNoSelectionOption());
            } else if (value instanceof SelectItem) {
                result = (SelectItem) value;
            } else {
                ValueExpression expression = selectItem.getValueExpression("value");
                throw new IllegalArgumentException("ValueExpression '"
                    + (expression == null ? null : expression.getExpressionString()) + "' of UISelectItem : "
                    + RichfacesLogger.getComponentPath(selectItem) + " does not reference an Object of type SelectItem");
            }
            return Iterators.singletonIterator(result);
        }
        private Iterator<SelectItem> createSelectItemsIterator(UIComponent componentObject value) {
            if (value == null) {
                return Iterators.emptyIterator();
            } else if (value instanceof SelectItem) {
                return Iterators.singletonIterator((SelectItem) value);
            } else if (value instanceof Object[]) {
                Iterator<Objectdata = Iterators.forArray((Object[]) value);
                return new GenericItemsIterator(componentdata);
            } else if (value instanceof Iterable<?>) {
                Iterator<?> data = ((Iterable<?>) value).iterator();
                return new GenericItemsIterator(componentdata);
            } else if (value instanceof Map) {
                return new MapItemsIterator((Map<?, ?>) value);
            } else {
                Logger.Level level = Logger.Level.INFO;
                if (!.isProjectStage(ProjectStage.Production)) {
                    level = Logger.Level.WARNING;
                }
                if (.isLogEnabled(level)) {
                    ValueExpression expression = component.getValueExpression("value");
                    .log(level, String.format("ValueExpression %s of UISelectItems with component-path %s"
                        + " does not reference an Object of type SelectItem," + " array, Iterable or Map, but of type: %s",
                        (expression == null ? null : expression.getExpressionString()),
                        RichfacesLogger.getComponentPath(component), (value == null ? null : value.getClass().getName())));
                }
            }
            return Iterators.emptyIterator();
        }
        private Iterator<SelectItem> createUISelectItemsIterator(UISelectItems selectItems) {
            Object value = selectItems.getValue();
            return createSelectItemsIterator(selectItemsvalue);
        }
        private Iterator<SelectItem> createUISelectItemsInterfaceIterator(UIComponent component) {
            Object value = ((SelectItemsInterface)component).getItemValues();
            return createSelectItemsIterator(componentvalue);
        }
        private Iterator<SelectItem> createIterator(UIComponent child) {
            if (!child.isRendered()) {
                return Iterators.emptyIterator();
            }
            if (child instanceof UISelectItem) {
                return createUISelectItemIterator((UISelectItem) child);
            } else if (child instanceof UISelectItems) {
                return createUISelectItemsIterator((UISelectItems) child);
            } else if (child instanceof SelectItemsInterface) {
                return createUISelectItemsInterfaceIterator(child);
            }
            return Iterators.emptyIterator();
        }
    }
    private SelectUtils() {
    }

    
Gathers all select items from specified component's children

Parameters:
context Faces context
component UIComponent with UISelectItem or UISelectItems children
Returns:
list of SelectItem taken from f:selectItem and f:selectItems
    public static Iterator<SelectItem> getSelectItems(FacesContext context, UIComponent component) {
        Iterator<UIComponent> children = component.getChildren().iterator();
        if (component instanceof SelectItemsInterface) {
            Iterator<UIComponent> self = Iterators.singletonIterator(component);
            children = Iterators.concat(selfchildren);
        }
        Iterator<SelectItem> iterator = new SelectItemsIterator(contextchildren);
        return iterator;
    }
New to GrepCode? Check out our FAQ X