You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2023/02/23 13:30:18 UTC
[myfaces] branch main updated: MYFACES-4560
This is an automated email from the ASF dual-hosted git repository.
tandraschko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces.git
The following commit(s) were added to refs/heads/main by this push:
new e074099c7 MYFACES-4560
e074099c7 is described below
commit e074099c7bb47a90459cbd0169c7f75cef55998a
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Thu Feb 23 14:30:10 2023 +0100
MYFACES-4560
---
.../core/api/shared/SelectItemsIterator.java | 62 ++++++++++++----------
.../myfaces/core/api/shared/SelectItemsUtil.java | 43 +++++++++++++++
.../faces/component/_SelectItemsUtilTest.java | 44 +++++++++++++++
3 files changed, 121 insertions(+), 28 deletions(-)
diff --git a/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsIterator.java b/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsIterator.java
index 75b7f06b2..1a61df7ad 100644
--- a/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsIterator.java
+++ b/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsIterator.java
@@ -207,41 +207,47 @@ public class SelectItemsIterator implements Iterator<SelectItem>
{
Object item = _nestedItems.next();
+
+ // check new params of SelectItems (since 2.0): itemValue, itemLabel, itemDescription,...
+ // Note that according to the spec UISelectItems does not provide Getter and Setter
+ // methods for this values, so we have to use the attribute map
+ Map<String, Object> attributeMap = _currentUISelectItems.getAttributes();
+
+ // write the current item into the request map under the key listed in var, if available
+ boolean wroteRequestMapVarValue = false;
+ Object oldRequestMapVarValue = null;
+ String var = (String) attributeMap.get(SelectItemsUtil.ATTR_VAR);
+ if (var != null && !var.isEmpty())
+ {
+ // save the current value of the key listed in var from the request map
+ oldRequestMapVarValue = _facesContext.getExternalContext().getRequestMap().put(var, item);
+ wroteRequestMapVarValue = true;
+ }
+
if (!(item instanceof SelectItem))
{
- // check new params of SelectItems (since 2.0): itemValue, itemLabel, itemDescription,...
- // Note that according to the spec UISelectItems does not provide Getter and Setter
- // methods for this values, so we have to use the attribute map
- Map<String, Object> attributeMap = _currentUISelectItems.getAttributes();
_currentValue = item;
-
- // write the current item into the request map under the key listed in var, if available
- boolean wroteRequestMapVarValue = false;
- Object oldRequestMapVarValue = null;
- String var = (String) attributeMap.get(SelectItemsUtil.ATTR_VAR);
- if (var != null && !var.isEmpty())
+ item = SelectItemsUtil.createSelectItem(_currentUISelectItems, item, SelectItem::new);
+ }
+ else
+ {
+ item = SelectItemsUtil.updateSelectItem(_currentUISelectItems, (SelectItem) item);
+ }
+
+ // 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)
{
- // save the current value of the key listed in var from the request map
- oldRequestMapVarValue = _facesContext.getExternalContext().getRequestMap().put(var, item);
- wroteRequestMapVarValue = true;
+ _facesContext.getExternalContext().getRequestMap().put(var, oldRequestMapVarValue);
}
-
- item = SelectItemsUtil.createSelectItem(_currentUISelectItems, item, SelectItem::new);
-
- // remove the value with the key from var from the request map, if previously written
- if (wroteRequestMapVarValue)
+ else
{
- // If there was a previous value stored with the key from var in the request map, restore it
- if (oldRequestMapVarValue != null)
- {
- _facesContext.getExternalContext().getRequestMap().put(var, oldRequestMapVarValue);
- }
- else
- {
- _facesContext.getExternalContext().getRequestMap().remove(var);
- }
- }
+ _facesContext.getExternalContext().getRequestMap().remove(var);
+ }
}
+
return (SelectItem) item;
}
throw new NoSuchElementException();
diff --git a/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsUtil.java b/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsUtil.java
index 1de1ddca9..03018a8e8 100644
--- a/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsUtil.java
+++ b/api/src/main/java/org/apache/myfaces/core/api/shared/SelectItemsUtil.java
@@ -44,6 +44,7 @@ public class SelectItemsUtil
public static final String ATTR_ITEM_DISABLED = "itemDisabled";
public static final String ATTR_ITEM_LABEL_ESCAPED = "itemLabelEscaped";
public static final String ATTR_NO_SELECTION_VALUE = "noSelectionValue";
+ public static final String ATTR_NO_SELECTION_OPTION = "noSelectionOption";
public static final String ATTR_VAR = "var";
public static <S extends SelectItem> S createSelectItem(UISelectItem uiSelectItem, Supplier<S> supplier)
@@ -115,6 +116,48 @@ public class SelectItemsUtil
return selectItem;
}
+ public static SelectItem updateSelectItem(UISelectItems uiSelectItems, SelectItem value)
+ {
+ if (value instanceof SelectItemGroup)
+ {
+ return value;
+ }
+
+ Map<String, Object> attrs = uiSelectItems.getAttributes();
+
+ Object itemLabel = attrs.get(ATTR_ITEM_LABEL);
+ if (itemLabel != null)
+ {
+ value.setLabel(String.valueOf(itemLabel));
+ }
+
+ Object itemDisabled = attrs.get(ATTR_ITEM_DISABLED);
+ if (itemDisabled != null)
+ {
+ value.setDisabled(Boolean.parseBoolean(itemDisabled.toString()));
+ }
+
+ Object itemEscaped = attrs.get(ATTR_ITEM_LABEL_ESCAPED);
+ if (itemEscaped != null)
+ {
+ value.setEscape(Boolean.parseBoolean(itemEscaped.toString()));
+ }
+
+ Object noSelection = attrs.get(ATTR_NO_SELECTION_OPTION);
+ if (noSelection != null)
+ {
+ value.setNoSelectionOption(Boolean.parseBoolean(noSelection.toString()));
+ }
+
+ Object itemDescription = attrs.get(ATTR_ITEM_DESCRIPTION);
+ if (itemDescription != null)
+ {
+ value.setDescription(String.valueOf(itemDescription));
+ }
+
+ return value;
+ }
+
public static List<SelectItem> collectSelectItems(FacesContext context, UIComponent component)
{
List<SelectItem> items = new ArrayList<>();
diff --git a/impl/src/test/java/jakarta/faces/component/_SelectItemsUtilTest.java b/impl/src/test/java/jakarta/faces/component/_SelectItemsUtilTest.java
index 9618e1cfb..d9acd0d16 100644
--- a/impl/src/test/java/jakarta/faces/component/_SelectItemsUtilTest.java
+++ b/impl/src/test/java/jakarta/faces/component/_SelectItemsUtilTest.java
@@ -18,10 +18,16 @@
*/
package jakarta.faces.component;
+import jakarta.el.ValueExpression;
+import jakarta.faces.model.SelectItem;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
import org.apache.myfaces.core.api.shared.SelectItemsIterator;
import org.apache.myfaces.core.api.shared.SelectItemsUtil;
import org.apache.myfaces.test.base.junit.AbstractJsfTestCase;
+import org.apache.myfaces.test.el.MockValueExpression;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -163,4 +169,42 @@ public class _SelectItemsUtilTest extends AbstractJsfTestCase
}
+ @Test
+ public void testSelectListAsValue()
+ {
+ List<SelectItem> values = new ArrayList<>();
+
+ values.add(new SelectItem("#1", "D1"));
+ values.add(new SelectItem("#2", "D2"));
+ values.add(new SelectItem("#3", "D3"));
+
+ UISelectItems selectItems = new UISelectItems();
+ selectItems.setValue(values);
+ selectItems.getAttributes().put("var", "item");
+ ValueExpression itemValue = new MockValueExpression("#{item.label}", Object.class);
+ ValueExpression itemLabel = new MockValueExpression("#{item.value}", Object.class);
+ ValueExpression itemDescription = new MockValueExpression("#{item.value}", Object.class);
+
+ selectItems.setValueExpression("itemValue", itemValue);
+ selectItems.setValueExpression("itemLabel", itemLabel);
+ selectItems.setValueExpression("itemDescription", itemDescription);
+ UISelectOne selectOne = new UISelectOne();
+ selectOne.getChildren().add(selectItems);
+
+ SelectItemsIterator iter = new SelectItemsIterator(selectOne, facesContext);
+ List<String> options = new ArrayList<>();
+ List<String> labels = new ArrayList<>();
+ List<String> descriptions = new ArrayList<>();
+ while (iter.hasNext())
+ {
+ SelectItem next = iter.next();
+ options.add((String) next.getValue());
+ labels.add(next.getLabel());
+ descriptions.add(next.getDescription());
+ }
+ Assertions.assertAll(
+ () -> Assertions.assertEquals(values.stream().map(SelectItem::getLabel).collect(Collectors.toList()), options),
+ () -> Assertions.assertEquals(values.stream().map(SelectItem::getValue).collect(Collectors.toList()), labels),
+ () -> Assertions.assertEquals(values.stream().map(SelectItem::getValue).collect(Collectors.toList()), descriptions));
+ }
}