views:

37

answers:

1

I'm playing with wicket's form input components. I tried to put an enum to a DropDownMenu:

  public enum Choice { ONE, TWO, THREE }

  cz.oz.wicket.pages.form.FormPage.java
  --------------
  .add( new DropDownChoice("choice",
     Arrays.asList( Choice.values() ), new EnumChoiceRenderer() )
   )

and added a properties file:

cz.oz.wicket.pages.form.FormPage.properties
--------------
Choice.ONE = Jedna
Choice.TWO = Dvě
Choice.THREE = Tři

According to what I've read, it should work.
But I get:

java.util.MissingResourceException: Unable to find property: 'Choice.ONE'

 at org.apache.wicket.Localizer.getString(Localizer.java:344)
 at org.apache.wicket.Localizer.getString(Localizer.java:100)
 at org.apache.wicket.markup.html.form.EnumChoiceRenderer.getDisplayValue(EnumChoiceRenderer.java:82)
 at org.apache.wicket.markup.html.form.EnumChoiceRenderer.getDisplayValue(EnumChoiceRenderer.java:39)
 at org.apache.wicket.markup.html.form.AbstractChoice.appendOptionHtml(AbstractChoice.java:384)
 at org.apache.wicket.markup.html.form.AbstractChoice.onComponentTagBody(AbstractChoice.java:361)
 at org.apache.wicket.Component.renderComponent(Component.java:2619)
...

What's wrong?

Thanks,
Ondra

+1  A: 

The EnumChoiceRenderer doesn't know where to look for the properties file.

You can tell it that the properties file is associated with the page by adding the page as a constructor parameter for the renderer:

  cz.oz.wicket.pages.form.FormPage.java
  --------------
  .add( new DropDownChoice("choice",
     Arrays.asList( Choice.values() ), new EnumChoiceRenderer(this) )
   )
Don Roby
You're right - thanks :)Btw - why does not DropDownChoice's constructor set itself as the renrerer's component reference if that is constructed without an argument? I think that would not harm anything. I feel like filing a RFE. WDYT?
Ondra Žižka
But then it would be looking for the property file associated to DropDownChoice, which it would have to find in the wicket libraries. And there would be no way to anticipate what properties you wanted associated with your particular enum.You could likely define a subclass of DropDownChoice specifically for this enum and make the subclass own the resource, which would allow you to neatly tie these things together.
Don Roby
Good point! I'm still getting used to Wicket's way of cleverly used features of Java. Thanks.
Ondra Žižka