Search in sources :

Example 6 with UISelectItem

use of javax.faces.component.UISelectItem in project primefaces by primefaces.

the class SelectRenderer method getSelectItems.

protected List<SelectItem> getSelectItems(FacesContext context, UIInput component) {
    List<SelectItem> selectItems = new ArrayList<>();
    boolean hideNoSelectOption = isHideNoSelection(component);
    SelectItem selectItem;
    for (int i = 0; i < component.getChildCount(); i++) {
        UIComponent child = component.getChildren().get(i);
        if (child instanceof UISelectItem) {
            UISelectItem uiSelectItem = (UISelectItem) child;
            Object selectItemValue = uiSelectItem.getValue();
            if (selectItemValue == null) {
                selectItem = new SelectItem(uiSelectItem.getItemValue(), uiSelectItem.getItemLabel(), uiSelectItem.getItemDescription(), uiSelectItem.isItemDisabled(), uiSelectItem.isItemEscaped(), uiSelectItem.isNoSelectionOption());
            } else {
                selectItem = (SelectItem) selectItemValue;
            }
            addSelectItem(component, selectItems, selectItem, hideNoSelectOption);
        } else if (child instanceof UISelectItems) {
            UISelectItems uiSelectItems = ((UISelectItems) child);
            Object value = uiSelectItems.getValue();
            if (value != null) {
                if (value instanceof SelectItem) {
                    addSelectItem(component, selectItems, (SelectItem) value, hideNoSelectOption);
                } else if (value.getClass().isArray()) {
                    for (int j = 0; j < Array.getLength(value); j++) {
                        Object item = Array.get(value, j);
                        if (item instanceof SelectItem) {
                            selectItem = (SelectItem) item;
                        } else {
                            selectItem = createSelectItem(context, uiSelectItems, item, null);
                        }
                        addSelectItem(component, selectItems, selectItem, hideNoSelectOption);
                    }
                } else if (value instanceof Map) {
                    Map<?, ?> map = (Map) value;
                    for (Map.Entry<?, ?> entry : map.entrySet()) {
                        selectItem = createSelectItem(context, uiSelectItems, entry.getValue(), String.valueOf(entry.getKey()));
                        addSelectItem(component, selectItems, selectItem, hideNoSelectOption);
                    }
                } else if (value instanceof List && value instanceof RandomAccess) {
                    List<?> list = (List) value;
                    for (int j = 0; j < list.size(); j++) {
                        Object item = list.get(j);
                        if (item instanceof SelectItem) {
                            selectItem = (SelectItem) item;
                        } else {
                            selectItem = createSelectItem(context, uiSelectItems, item, null);
                        }
                        addSelectItem(component, selectItems, selectItem, hideNoSelectOption);
                    }
                } else if (value instanceof Collection) {
                    Collection<?> collection = (Collection) value;
                    for (Object item : collection) {
                        if (item instanceof SelectItem) {
                            selectItem = (SelectItem) item;
                        } else {
                            selectItem = createSelectItem(context, uiSelectItems, item, null);
                        }
                        addSelectItem(component, selectItems, selectItem, hideNoSelectOption);
                    }
                }
            }
        }
    }
    return selectItems;
}
Also used : UISelectItems(javax.faces.component.UISelectItems) ArrayList(java.util.ArrayList) UIComponent(javax.faces.component.UIComponent) RandomAccess(java.util.RandomAccess) UISelectItem(javax.faces.component.UISelectItem) SelectItem(javax.faces.model.SelectItem) UISelectItem(javax.faces.component.UISelectItem) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 7 with UISelectItem

use of javax.faces.component.UISelectItem in project empire-db by apache.

the class RadioInputControl method syncOptions.

public void syncOptions(HtmlSelectOneRadio input, TextResolver textResolver, Options options, boolean hasEmpty, String nullText) {
    // Compare child-items with options
    Iterator<OptionEntry> ioe = options.iterator();
    OptionEntry oe = (ioe.hasNext() ? ioe.next() : null);
    List<UIComponent> childList = input.getChildren();
    Iterator<UIComponent> ico = childList.iterator();
    int lastIndex = 0;
    while (ico.hasNext()) {
        lastIndex++;
        UIComponent co = ico.next();
        if (!(co instanceof UISelectItem)) {
            continue;
        }
        UISelectItem si = (UISelectItem) co;
        Object ov = si.getItemValue();
        if (ObjectUtils.isEmpty(ov) && hasEmpty) {
            continue;
        }
        if (oe == null) {
            // remove obsolete items
            lastIndex--;
            for (int index = childList.size() - 1; index >= lastIndex; index--) {
                childList.remove(index);
            }
            // done
            return;
        }
        if (ObjectUtils.compareEqual(ov, oe.getValue())) {
            // next
            oe = (ioe.hasNext() ? ioe.next() : null);
            continue;
        }
        // Not equal - do a full reload
        input.getChildren().clear();
        if (hasEmpty) {
            addSelectItem(input, textResolver, new OptionEntry("", nullText));
        }
        for (OptionEntry e : options) {
            // Option entries
            addSelectItem(input, textResolver, e);
        }
        // done
        return;
    }
    // Are there any items left?
    while (oe != null) {
        // add missing item
        addSelectItem(input, textResolver, oe);
        oe = (ioe.hasNext() ? ioe.next() : null);
    }
}
Also used : OptionEntry(org.apache.empire.commons.OptionEntry) UIComponent(javax.faces.component.UIComponent) UISelectItem(javax.faces.component.UISelectItem)

Example 8 with UISelectItem

use of javax.faces.component.UISelectItem in project empire-db by apache.

the class RadioInputControl method addSelectItem.

public void addSelectItem(UIComponent input, TextResolver textResolver, OptionEntry e) {
    UISelectItem selectItem = new UISelectItem();
    // set value
    Object value;
    Object valueExpressionFlag = input.getAttributes().get(RadioInputControl.VALUE_EXPRESSION_FLAG);
    if (ObjectUtils.getBoolean(valueExpressionFlag)) {
        // Use value as is
        value = e.getValue();
    } else {
        // Convert to String
        value = e.getValueString();
    }
    selectItem.setItemValue(value);
    // set text
    String text = e.getText();
    text = textResolver.resolveText(text);
    selectItem.setItemLabel(text);
    // add item
    input.getChildren().add(selectItem);
}
Also used : UISelectItem(javax.faces.component.UISelectItem)

Example 9 with UISelectItem

use of javax.faces.component.UISelectItem in project ctsms by phoenixctms.

the class SelectItemsIterator method initializeItems.

/**
 * <p>
 * Initializes the <code>items</code> instance variable with an
 * <code>Iterator</code> appropriate to the UISelectItem(s) value.
 * </p>
 */
private void initializeItems(Object kid) {
    if (kid instanceof UISelectItem) {
        UISelectItem ui = (UISelectItem) kid;
        SelectItem item = (SelectItem) ui.getValue();
        if (item == null) {
            item = new SelectItem(ui.getItemValue(), ui.getItemLabel(), ui.getItemDescription(), ui.isItemDisabled(), ui.isItemEscaped(), ui.isNoSelectionOption());
        }
        updateSingeItemIterator(item);
        items = singleItemIterator;
    } else if (kid instanceof UISelectItems) {
        UISelectItems ui = (UISelectItems) kid;
        Object value = ui.getValue();
        if (value != null) {
            if (value instanceof SelectItem) {
                updateSingeItemIterator((SelectItem) value);
                items = singleItemIterator;
            } else if (value.getClass().isArray()) {
                items = new ArrayIterator(ctx, (UISelectItems) kid, value);
            } else if (value instanceof Iterable) {
                items = new IterableItemIterator(ctx, (UISelectItems) kid, (Iterable<?>) value);
            } else if (value instanceof Map) {
                items = new MapIterator((Map) value);
            } else {
                throw new IllegalArgumentException();
            }
        }
        if (items != null && !items.hasNext()) {
            items = null;
        }
    }
}
Also used : UISelectItems(javax.faces.component.UISelectItems) SelectItem(javax.faces.model.SelectItem) UISelectItem(javax.faces.component.UISelectItem) Map(java.util.Map) UISelectItem(javax.faces.component.UISelectItem)

Aggregations

UISelectItem (javax.faces.component.UISelectItem)9 UIComponent (javax.faces.component.UIComponent)6 UISelectItems (javax.faces.component.UISelectItems)5 SelectItem (javax.faces.model.SelectItem)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Iterator (java.util.Iterator)3 Map (java.util.Map)3 OptionEntry (org.apache.empire.commons.OptionEntry)2 List (java.util.List)1 RandomAccess (java.util.RandomAccess)1 Options (org.apache.empire.commons.Options)1