Search in sources :

Example 1 with UISelectItem

use of jakarta.faces.component.UISelectItem 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 UISelectItem

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

the class UISelectOne method processValidators.

/**
 * Verify that when ever there is a ValueExpression and submitted value is not empty, then
 * visit all the UISelectItem elements within the UISelectOne radio components to check if
 * the submitted value exists in any of the select items.
 *
 * @see jakarta.faces.component.UIInput#processValidators(jakarta.faces.context.FacesContext)
 */
@Override
public void processValidators(FacesContext context) {
    String group = getGroup();
    ValueExpression ve = getValueExpression("value");
    String submittedValue = (String) getSubmittedValue();
    if (group != null && !group.isEmpty() && ve != null && !isEmpty(submittedValue)) {
        final UIComponent form = getRadioNestingForm(context, this);
        form.visitTree(VisitContext.createVisitContext(context), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext visitContext, UIComponent target) {
                if (target instanceof UISelectOne && ((UISelectOne) target).getGroup().equals(group)) {
                    UISelectOne radio = (UISelectOne) target;
                    // and verify if the submitted value exists
                    for (Iterator<UIComponent> iter = radio.getChildren().iterator(); iter.hasNext(); ) {
                        UIComponent component = iter.next();
                        if (component instanceof UISelectItem) {
                            UISelectItem item = (UISelectItem) component;
                            if (item.getItemValue().equals(submittedValue)) {
                                selectItemValueFound = true;
                                return VisitResult.COMPLETE;
                            }
                        }
                    }
                    return VisitResult.REJECT;
                }
                return VisitResult.ACCEPT;
            }
        });
    }
    super.processValidators(context);
}
Also used : VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitContext(jakarta.faces.component.visit.VisitContext) ValueExpression(jakarta.el.ValueExpression) Iterator(java.util.Iterator) SelectItemsIterator(org.apache.myfaces.core.api.shared.SelectItemsIterator) VisitResult(jakarta.faces.component.visit.VisitResult) UISelectItem(jakarta.faces.component.UISelectItem)

Example 3 with UISelectItem

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

the class HtmlListboxRendererTest method testClientBehaviorHolderRendersIdAndNameSelectOneListbox.

/**
 * Components that render client behaviors should always render "id" and "name" attribute
 */
public void testClientBehaviorHolderRendersIdAndNameSelectOneListbox() {
    UISelectItem item1 = new UISelectItem();
    item1.setItemLabel("#1");
    item1.setItemValue("#1");
    UISelectItem item2 = new UISelectItem();
    item2.setItemLabel("#2");
    item2.setItemValue("#2");
    selectOneListbox.addClientBehavior("keypress", new AjaxBehavior());
    try {
        selectOneListbox.getChildren().add(item1);
        selectOneListbox.getChildren().add(item2);
        selectOneListbox.encodeAll(facesContext);
        String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
        Assert.assertTrue(output.matches(".+id=\".+\".+"));
        Assert.assertTrue(output.matches(".+name=\".+\".+"));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 4 with UISelectItem

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

the class HtmlListboxRendererTest method testClientBehaviorHolderRendersIdAndNameSelectManyListbox.

/**
 * Components that render client behaviors should always render "id" and "name" attribute
 */
public void testClientBehaviorHolderRendersIdAndNameSelectManyListbox() {
    UISelectItem item1 = new UISelectItem();
    item1.setItemLabel("#1");
    item1.setItemValue("#1");
    UISelectItem item2 = new UISelectItem();
    item2.setItemLabel("#2");
    item2.setItemValue("#2");
    selectManyListbox.addClientBehavior("keypress", new AjaxBehavior());
    try {
        selectManyListbox.getChildren().add(item1);
        selectManyListbox.getChildren().add(item2);
        selectManyListbox.encodeAll(facesContext);
        String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
        Assert.assertTrue(output.matches(".+id=\".+\".+"));
        Assert.assertTrue(output.matches(".+name=\".+\".+"));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 5 with UISelectItem

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

the class HtmlRadioRendererTest method testClientBehaviorHolderRendersIdAndName.

/**
 * Components that render client behaviors should always render "id" and "name" attribute
 */
public void testClientBehaviorHolderRendersIdAndName() {
    UISelectItem item1 = new UISelectItem();
    item1.setItemLabel("#1");
    item1.setItemValue("#1");
    UISelectItem item2 = new UISelectItem();
    item2.setItemLabel("#2");
    item2.setItemValue("#2");
    selectOneRadio.addClientBehavior("keypress", new AjaxBehavior());
    try {
        selectOneRadio.getChildren().add(item1);
        selectOneRadio.getChildren().add(item2);
        selectOneRadio.encodeAll(facesContext);
        String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
        Assert.assertTrue(output.matches(".+id=\".+\".+"));
        Assert.assertTrue(output.matches(".+name=\".+\".+"));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Aggregations

UISelectItem (jakarta.faces.component.UISelectItem)37 PrintWriter (java.io.PrintWriter)17 UIInput (jakarta.faces.component.UIInput)10 UIViewRoot (jakarta.faces.component.UIViewRoot)10 FacesContext (jakarta.faces.context.FacesContext)10 UIComponent (jakarta.faces.component.UIComponent)8 TCKValidator (com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValidator)6 TCKValueChangeListener (com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKValueChangeListener)6 MethodExpression (jakarta.el.MethodExpression)6 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)6 MethodExpressionValidator (jakarta.faces.validator.MethodExpressionValidator)6 SelectItem (jakarta.faces.model.SelectItem)5 ELContext (jakarta.el.ELContext)4 ValueExpression (jakarta.el.ValueExpression)3 UISelectItems (jakarta.faces.component.UISelectItems)3 Application (jakarta.faces.application.Application)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 TCKConverter (com.sun.ts.tests.jsf.api.jakarta_faces.component.common.TCKConverter)1