views:

308

answers:

2

I'd like to add a label to a wicket panel where the label's model is an enum value. Based on the value of that enum, I'd like to display a different message pulled from the page's properties file.

For example, here's an enum:

public enum ApprovalType { UNAPPROVED, APPROVED, BLOCKED };

I can easily add a label to the panel that has this enum as its model:

add(new Label("approval", new Model(ApprovalType.APPROVED)));

However, this will simply output UNAPPROVED, APPROVED, or BLOCKED. I'd like to output a localized message that is selected based on the value of this enum.

So if I have this properties file, what's the best way to get these messages to output?

UNAPPROVED=Your membership is currently pending approval
APPROVED=Your membership has been approved
BLOCKED=You have been blocked from membership

I can get it working like this:

add(new Label("approval", new ResourceModel(ApprovalType.APPROVED.name());

But the problem is that ResourceModel only accepts a string (resource key). I'd like to pull the value of the resource key automatically from a model (preferrably a CompoundPropertyModel). In other words, I don't want to do this:

Member member = (Member) getDefaultModelObject();
add(new Label("approval", new ResourceModel(member.getApproval().name())));

I'd rather do something like:

add(new EnumLabel("approval");  // assuming I have a CompoundPropertyModel

Is there a component that does this that comes with Wicket? Or do I need to write my own?

Lastly, I discovered this also works:

add(new Label("approval", new StringResourceModel(
    "${}", 
    new PropertyModel<ApprovalType>(getDefaultModel(),"approval")
)));

But this seems overly verbose, and still doesn't utilize the simplicity of using CompoundPropertyModel.

Ideas?

+3  A: 

Not sure if this 'll work, but maybe you could try writing your own EnumModel class that takes the value of an Enum and returns a ResourceModel value..

Please say so if the above isn't clear enough, I'll try to elaborate.

Tim
Yeah, that's probably the right direction to go. I found a detailed example (http://wicketbyexample.com/enums-internationalization-with-wicket/) of using enums and internationalization that goes over this specific problem. But the solution is so verbose, I was really looking for something lighter weight. If you could elaborate on your EnumModel, it would help.
Tauren
The EnumPropertyModel mentioned in the article you're referring to comes pretty close to what I would have tried.. Are you sure it can't be simplified/made to work with a CompoundPropertyModel? I'm not too familiar with the inner workings of Wicket to know if this is possible at all..
Tim
A: 

Maybe this article helps you.

James Selvakumar