Skip to content

Commit

Permalink
Convert MultipleSelectModel to return a Set of values (not a sorted L…
Browse files Browse the repository at this point in the history
…ist),

the MultipleSelect sorts the values by label
  • Loading branch information
hlship committed Mar 17, 2011
1 parent 110ce45 commit 84fd6b4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,6 +90,34 @@ public class MultipleSelect implements Field

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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.howardlewisship.tapx.core.multiselect;

import java.util.List;
import java.util.Set;

import org.apache.tapestry5.ValueEncoder;

Expand All @@ -18,7 +18,7 @@ public interface MultipleSelectModel<T> extends ValueEncoder<T>
/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 84fd6b4

Please sign in to comment.