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;
}
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);
}
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());
}
}
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());
}
}
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());
}
}
Aggregations