Search in sources :

Example 1 with UISelectItems

use of jakarta.faces.component.UISelectItems in project myfaces by apache.

the class SelectItemsIterator method hasNext.

@SuppressWarnings("unchecked")
@Override
public boolean hasNext() {
    if (_nextItem != null) {
        return true;
    }
    if (_nestedItems != null) {
        if (_nestedItems.hasNext()) {
            return true;
        }
        _nestedItems = null;
        _currentComponent = null;
        _currentValue = null;
    }
    if (_children.hasNext()) {
        UIComponent child = _children.next();
        // that conform this condition, just return false.
        while (!(child instanceof UISelectItem) && !(child instanceof UISelectItems)) {
            // Try to skip it
            if (_children.hasNext()) {
                // Skip and do the same check
                child = _children.next();
            } else {
                // since there are no more components to iterate.
                return false;
            }
        }
        if (child instanceof UISelectItem) {
            UISelectItem uiSelectItem = (UISelectItem) child;
            Object item = uiSelectItem.getValue();
            if (item == null) {
                // no value attribute --> create the SelectItem out of the other attributes
                item = SelectItemsUtil.createSelectItem(uiSelectItem, SelectItem::new);
            } else if (!(item instanceof SelectItem)) {
                ValueExpression expression = uiSelectItem.getValueExpression("value");
                throw new IllegalArgumentException("ValueExpression '" + (expression == null ? null : expression.getExpressionString()) + "' of UISelectItem : " + ComponentUtils.getPathToComponent(child) + " does not reference an Object of type SelectItem");
            }
            _nextItem = (SelectItem) item;
            _currentComponent = child;
            _currentValue = item;
            return true;
        } else if (child instanceof UISelectItems) {
            _currentUISelectItems = ((UISelectItems) child);
            Object value = _currentUISelectItems.getValue();
            _currentComponent = child;
            if (value instanceof SelectItem) {
                _nextItem = (SelectItem) value;
                return true;
            } else if (value != null && value.getClass().isArray()) {
                // value is any kind of array (primitive or non-primitive)
                // --> we have to use class Array to get the values
                int length = Array.getLength(value);
                Collection<Object> items = new ArrayList<>(length);
                for (int i = 0; i < length; i++) {
                    items.add(Array.get(value, i));
                }
                _nestedItems = items.iterator();
                return hasNext();
            } else if (value instanceof Iterable) {
                // value is Iterable --> Collection, DataModel,...
                _nestedItems = ((Iterable<?>) value).iterator();
                return hasNext();
            } else if (value instanceof Map) {
                Map<Object, Object> map = ((Map<Object, Object>) value);
                Collection<SelectItem> items = new ArrayList<>(map.size());
                for (Map.Entry<Object, Object> entry : map.entrySet()) {
                    items.add(new SelectItem(entry.getValue(), entry.getKey().toString()));
                }
                _nestedItems = items.iterator();
                return hasNext();
            } else {
                Level level = _facesContext.isProjectStage(ProjectStage.Production) ? Level.FINE : Level.WARNING;
                if (log.isLoggable(level)) {
                    ValueExpression expression = _currentUISelectItems.getValueExpression("value");
                    log.log(level, "ValueExpression {0} of UISelectItems with component-path {1}" + " does not reference an Object of type SelectItem," + " array, Iterable or Map, but of type: {2}", new Object[] { (expression == null ? null : expression.getExpressionString()), ComponentUtils.getPathToComponent(child), (value == null ? null : value.getClass().getName()) });
                }
            }
        } else {
            _currentComponent = null;
            _currentValue = null;
        }
    }
    return false;
}
Also used : UISelectItems(jakarta.faces.component.UISelectItems) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) UISelectItem(jakarta.faces.component.UISelectItem) UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) ValueExpression(jakarta.el.ValueExpression) Level(java.util.logging.Level) Map(java.util.Map)

Example 2 with UISelectItems

use of jakarta.faces.component.UISelectItems in project myfaces by apache.

the class HtmlMenuRendererTest method testSelectManyHtmlPropertyPassTru.

public void testSelectManyHtmlPropertyPassTru() throws Exception {
    HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateBasicAttrs(false);
    List items = new ArrayList();
    items.add(new SelectItem("mars"));
    UISelectItems selectItems = new UISelectItems();
    selectItems.setValue(items);
    selectManyMenu.getChildren().add(selectItems);
    HtmlCheckAttributesUtil.checkRenderedAttributes(selectManyMenu, facesContext, writer, attrs);
    if (HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
        Assert.fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
    }
}
Also used : UISelectItems(jakarta.faces.component.UISelectItems) UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HtmlRenderedAttr(org.apache.myfaces.test.utils.HtmlRenderedAttr)

Example 3 with UISelectItems

use of jakarta.faces.component.UISelectItems in project faces by jakartaee.

the class TestServlet method uiSelectItemsMapGetValueTest.

public void uiSelectItemsMapGetValueTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    UISelectItems command = (UISelectItems) createComponent();
    HashMap<String, String> employeeLvl = new HashMap<String, String>();
    employeeLvl.put("MGR", "4");
    employeeLvl.put("VP", "6");
    employeeLvl.put("DIR", "5");
    employeeLvl.put("WB", "0");
    command.setValue(employeeLvl);
    if (!Arrays.equals(employeeLvl.entrySet().toArray(), ((HashMap<?, ?>) command.getValue()).entrySet().toArray())) {
        out.println(JSFTestUtil.FAIL + " UISelectItem.getValue() didn't return" + " the value as set by UISelectItem.setValue().");
        out.println("HashMap objects were not equal!");
        return;
    }
    out.println(JSFTestUtil.PASS);
}
Also used : UISelectItems(jakarta.faces.component.UISelectItems) HashMap(java.util.HashMap) PrintWriter(java.io.PrintWriter)

Example 4 with UISelectItems

use of jakarta.faces.component.UISelectItems in project myfaces by apache.

the class SelectItemsUtil method collectSelectItems.

public static List<SelectItem> collectSelectItems(FacesContext context, UIComponent component) {
    List<SelectItem> items = new ArrayList<>();
    for (int i = 0; i < component.getChildCount(); i++) {
        UIComponent child = component.getChildren().get(i);
        if (child instanceof UISelectItems) {
            UISelectItems uiSelectItems = (UISelectItems) child;
            createSelectItems(context, uiSelectItems, uiSelectItems.getValue(), SelectItem::new, items::add);
        } else if (child instanceof UISelectItem) {
            items.add(createSelectItem(child, null, SelectItem::new));
        }
    }
    return items;
}
Also used : UISelectItems(jakarta.faces.component.UISelectItems) UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) ArrayList(java.util.ArrayList) UIComponent(jakarta.faces.component.UIComponent) UISelectItem(jakarta.faces.component.UISelectItem)

Example 5 with UISelectItems

use of jakarta.faces.component.UISelectItems in project myfaces by apache.

the class SelectItemsUtils method renderSelectOptions.

public static void renderSelectOptions(FacesContext context, UIComponent component, Converter converter, Set lookupSet, List<SelectItemInfo> selectItemList) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    // check for the hideNoSelectionOption attribute
    boolean hideNoSelectionOption = isHideNoSelectionOption(component);
    boolean componentDisabled = isTrue(component.getAttributes().get("disabled"));
    for (int i = 0; i < selectItemList.size(); i++) {
        SelectItemInfo selectItemInfo = selectItemList.get(i);
        SelectItem selectItem = selectItemInfo.getItem();
        if (selectItem instanceof SelectItemGroup) {
            // component);
            writer.startElement(HTML.OPTGROUP_ELEM, selectItemInfo.getComponent());
            writer.writeAttribute(HTML.LABEL_ATTR, selectItem.getLabel(), null);
            SelectItem[] selectItems = ((SelectItemGroup) selectItem).getSelectItems();
            List<SelectItemInfo> selectItemsGroupList = new ArrayList<>(selectItems.length);
            for (SelectItem item : selectItems) {
                selectItemsGroupList.add(new SelectItemInfo(item, null));
            }
            renderSelectOptions(context, component, converter, lookupSet, selectItemsGroupList);
            writer.endElement(HTML.OPTGROUP_ELEM);
        } else {
            String itemStrValue = org.apache.myfaces.renderkit.RendererUtils.getConvertedStringValue(context, component, converter, selectItem);
            boolean selected = lookupSet.contains(itemStrValue);
            // (if there is currently no value on UISelectOne, lookupSet contains "")
            if (hideNoSelectionOption && selectItem.isNoSelectionOption() && !lookupSet.isEmpty() && !(lookupSet.size() == 1 && lookupSet.contains("")) && !selected) {
                // do not render this selectItem
                continue;
            }
            writer.write(TABULATOR);
            boolean wroteRequestMapVarValue = false;
            Object oldRequestMapVarValue = null;
            String var = null;
            if (selectItemInfo != null && selectItemInfo.getComponent() instanceof UISelectItems) {
                var = (String) selectItemInfo.getComponent().getAttributes().get(ComponentAttrs.VAR_ATTR);
                if (var != null && !var.isEmpty()) {
                    // save the current value of the key listed in var from the request map
                    oldRequestMapVarValue = context.getExternalContext().getRequestMap().put(var, selectItemInfo.getValue());
                    wroteRequestMapVarValue = true;
                }
            }
            writer.startElement(HTML.OPTION_ELEM, selectItemInfo.getComponent());
            if (itemStrValue != null) {
                writer.writeAttribute(HTML.VALUE_ATTR, itemStrValue, null);
            } else {
                writer.writeAttribute(HTML.VALUE_ATTR, "", null);
            }
            if (selected) {
                writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null);
            }
            boolean disabled = selectItem.isDisabled();
            if (disabled) {
                writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
            }
            String labelClass = null;
            if (componentDisabled || disabled) {
                labelClass = (String) component.getAttributes().get(ComponentAttrs.DISABLED_CLASS_ATTR);
            } else {
                labelClass = (String) component.getAttributes().get(ComponentAttrs.ENABLED_CLASS_ATTR);
            }
            if (labelClass != null) {
                writer.writeAttribute("class", labelClass, "labelClass");
            }
            boolean escape = AttributeUtils.getBooleanAttribute(component, ComponentAttrs.ESCAPE_ATTR, false);
            // check if isEscape() = true first.
            if (escape || selectItem.isEscape()) {
                writer.writeText(selectItem.getLabel(), null);
            } else {
                writer.write(selectItem.getLabel());
            }
            writer.endElement(HTML.OPTION_ELEM);
            // remove the value with the key from var from the request map, if previously written
            if (wroteRequestMapVarValue) {
                // If there was a previous value stored with the key from var in the request map, restore it
                if (oldRequestMapVarValue != null) {
                    context.getExternalContext().getRequestMap().put(var, oldRequestMapVarValue);
                } else {
                    context.getExternalContext().getRequestMap().remove(var);
                }
            }
        }
    }
}
Also used : UISelectItems(jakarta.faces.component.UISelectItems) ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup) ArrayList(java.util.ArrayList)

Aggregations

UISelectItems (jakarta.faces.component.UISelectItems)13 SelectItem (jakarta.faces.model.SelectItem)9 UISelectItem (jakarta.faces.component.UISelectItem)8 ArrayList (java.util.ArrayList)8 HtmlRenderedAttr (org.apache.myfaces.test.utils.HtmlRenderedAttr)5 PrintWriter (java.io.PrintWriter)4 List (java.util.List)4 UIComponent (jakarta.faces.component.UIComponent)2 Map (java.util.Map)2 ValueExpression (jakarta.el.ValueExpression)1 ResponseWriter (jakarta.faces.context.ResponseWriter)1 SelectItemGroup (jakarta.faces.model.SelectItemGroup)1 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1 Level (java.util.logging.Level)1