Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top