Hi all,
One problem I had today was rendering a Boolean property on a domain
object in a very specific way (Yes/No/Unspecified). After a few false
starts and a few ugly hacks, I came up with the MappedValueModel,
below.
I'm posting it here in case anyone else finds it useful. It's a
simple little thing, but if it's well-received, I'd like to submit it
to wicket-extensions. Does anyone have any comments or suggestions
for improvement?
Cheers,
Charlie.
import java.util.Map;
import wicket.Component;
import wicket.model.AbstractReadOnlyModel;
import wicket.model.IModel;
public class MappedValueModel<T, U> extends AbstractReadOnlyModel
{
private static final long serialVersionUID = 1L;
private IModel model;
private Map<T, U> valueMap;
public MappedValueModel(IModel model, Map<T, U> valueMap)
{
this.model = model;
this.valueMap = valueMap;
}
@SuppressWarnings("unchecked")
@Override
public Object getObject(Component component)
{
T object = (T)model.getObject(component); // unchecked warning
for(Map.Entry<T, U> entry : valueMap.entrySet()) {
if(entry.getKey() == null)
{
if(object == null)
{
return entry.getValue();
}
}
else if(entry.getKey().equals(object))
{
return entry.getValue();
}
}
return null;
}
@Override
public void detach()
{
model.detach();
}
}
Example usage:
Map<Boolean, String> YES_NO_UNSPECIFIED = new HashMap<Boolean, String>();
YES_NO_UNSPECIFIED.put(Boolean.TRUE, "Yes");
YES_NO_UNSPECIFIED.put(Boolean.FALSE, "No");
YES_NO_UNSPECIFIED.put(null, "Unspecified");
new Label("theBoolean", new MappedValueModel<Boolean,
String>(theWrappedModel, YES_NO_UNSPECIFIED));
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user