You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by sub <b4...@nwldx.com> on 2012/04/02 10:22:00 UTC

Re: Enum with Values in Select

Thiago H de Paula Figueiredo wrote
> 
> On Fri, 30 Mar 2012 10:22:32 -0300, sub &lt;b4679016@&gt; wrote:
> 
>> what I want is:
>>
>> <select id="duration" name="duration">
>> <option value="3600">One Hour</option>
>> <option value="7200">Two Hours</option>
>> <option value="14400">Four Hours</option>
>> <option value="28800">Eight Hours</option>
>> </select>
> 
> Why?
> 

I'm passing the form to a different application, which expects these values.


Thiago H de Paula Figueiredo wrote
> 
> On Fri, 30 Mar 2012 10:22:32 -0300, sub &lt;b4679016@&gt; wrote:
> 
>> How can I achieve this?
> 
> Implement your own ValueEncoder and pass it to the encoder parameter of  
> Select.
> 

Thanks for pointing me into the right direction. It works.


DurationEncoder.java

public class DurationEncoder implements ValueEncoder<Duration> {

	@Override
	public String toClient(Duration duration) {
		return String.valueOf(duration.getValue());
	}

	@Override
	public Duration toValue(String value) {
		return Duration.get(Integer.parseInt(value));
	} 
}


Duration.java

public enum Duration {
	ONE_HOUR(3600), TWO_HOURS(7200), FOUR_HOURS(14400), EIGHT_HOURS(28800);

	private static final Map<Integer, Duration> lookup = new HashMap<Integer,
Duration>();

	static {
		for (Duration d : EnumSet.allOf(Duration.class)) {
			lookup.put(d.getValue(), d);
		}
	}

	private int value;

	private Duration(int value) {
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public static Duration get(int value) {
		return lookup.get(value);
	}

}


View.java

.
.
private DurationEncoder durationEncoder;
.
.
public DurationEncoder getDurationEncoder() {
	if (durationEncoder == null) {
		durationEncoder = new DurationEncoder();
	}
	return durationEncoder;
}


View.tml

<t:select t:id="duration" encoder="durationEncoder" blankOption="never" />





--
View this message in context: http://tapestry.1045711.n5.nabble.com/Enum-with-Values-in-Select-tp5606643p5611794.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org