Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Convert MultipleSelectModel to return a Set of values (not a sorted L…

…ist),

the MultipleSelect sorts the values by label
  • Loading branch information...
commit 84fd6b4036d3d31ecbfcea93eae4b96f1433e616 1 parent 110ce45
Howard M. Lewis Ship authored
41 tapx-core/src/main/java/com/howardlewisship/tapx/core/components/MultipleSelect.java
View
@@ -27,6 +27,9 @@
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.corelib.components.Palette;
+import org.apache.tapestry5.func.F;
+import org.apache.tapestry5.func.Flow;
+import org.apache.tapestry5.func.Mapper;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONArray;
import org.apache.tapestry5.json.JSONObject;
@@ -87,6 +90,34 @@
private String clientId, controlName;
+ private static class Pair implements Comparable<Pair>
+ {
+ final String label;
+
+ final String clientValue;
+
+ public Pair(String label, String clientValue)
+ {
+ this.label = label;
+ this.clientValue = clientValue;
+ }
+
+ @Override
+ public int compareTo(Pair o)
+ {
+ return this.label.compareTo(o.label);
+ }
+ }
+
+ private final Mapper<Object, Pair> toPair = new Mapper<Object, Pair>()
+ {
+ @Override
+ public Pair map(Object value)
+ {
+ return new Pair(model.toLabel(value), model.toClient(value));
+ }
+ };
+
public static class ProcessSubmission implements ComponentAction<MultipleSelect>
{
private static final long serialVersionUID = -7656903896600563715L;
@@ -167,13 +198,11 @@ void setupRender()
spec.append("values", clientValue);
}
- for (Object value : model.getAvailableValues())
- {
- String clientValue = model.toClient(value);
- String label = model.toLabel(value);
+ Flow<Object> valuesFlow = F.flow(model.getAvailableValues());
- JSONArray row = new JSONArray(clientValue, label);
- spec.append("model", row);
+ for (Pair pair : valuesFlow.map(toPair).sort())
+ {
+ spec.append("model", new JSONArray(pair.clientValue, pair.label));
}
jss.addInitializerCall("tapxMultipleSelect", spec);
4 tapx-core/src/main/java/com/howardlewisship/tapx/core/multiselect/MultipleSelectModel.java
View
@@ -1,6 +1,6 @@
package com.howardlewisship.tapx.core.multiselect;
-import java.util.List;
+import java.util.Set;
import org.apache.tapestry5.ValueEncoder;
@@ -18,7 +18,7 @@
/**
* Returns the values that may be selected by the client, in presentation order (typically, alphabetical order).
*/
- List<T> getAvailableValues();
+ Set<T> getAvailableValues();
/**
* Converts an individual value into a label that may be presented to the client.
5 tapx-core/src/test/java/com/demo/data/StringMultipleSelectModel.java
View
@@ -2,6 +2,7 @@
import java.util.Arrays;
import java.util.List;
+import java.util.Set;
import org.apache.tapestry5.func.F;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
@@ -32,9 +33,9 @@ public String toValue(String clientValue)
}
@Override
- public List<String> getAvailableValues()
+ public Set<String> getAvailableValues()
{
- return F.flow(values).sort().toList();
+ return F.flow(values).toSet();
}
@Override
Please sign in to comment.
Something went wrong with that request. Please try again.