I’m using a CompoundPropertyModel (via PropertyListView) to print properties of an object (User).
One of the properties of the User object is a boolean. I would like the boolean to be rendered using a custom conversion (false -> “disabled”, true -> “enabled”).
How can I achieve this without adding new method to the User object?
add(new PropertyListView<User>("users", new LoadableUsersModel()) {
@Override
protected void populateItem(ListItem<User> item) {
item.add(new Label("firstname"));
item.add(new Label("surname"));
item.add(new Label("username"));
item.add(new Label("email"));
item.add(new Label("active"));
}
});
There are several ways to achieve this:
Get your Model Object from
item, and use anif-else.Implement an
IConverterand return the desired value in itsconvertToString()method. Return an instance of the converter in an override of thegetConverter()method of theLabel. In this example it’s all anonymous classes, it’d be better to define them as independent classes, at least the converter, and have a static method to use just a single instance.Use an
AbstractReadOnlyModelto return the desired value, feeding it with theactiveproperty, or a Model with it:As a side note, if
enabledanddisabledare literals that are to be shown in the page, you might be interested in localizing them in a xml resource file, and usegetString()instead.