Thanks for the input, John - but I the plot thickens here. It seems that
there is actually a Wicket bug that needs to get fixed. After a lot of
tinkering I figured out why I was getting this error. The VERY FIRST
TIME my renderer is being called it actually calls getIdValue() with an
Integer, not with the Object that I'm using. In my debugger I saw the
same method being called with different object types (Integer and
IntegerSelectChoice). Take a look at my (obviously silly, but working)
work around:

/* Manages the display/id value for our dropdown choices. */
                private class CountChoiceRenderer implements
IChoiceRenderer {
                        /**
                         * Get the display value from the SelectChoice
(what the user sees)
                         *
                         * @param object a SelectChoice object
                         * @return display
                         */
                        @Todo(comment = "Replace Object with <T> once
Wicket incl. generics is available" )
                        public String getDisplayValue(Object object) {
                                return
((IntegerSelectChoice)object).getDisplay();
                        }

                        /**
                         * Get key value (what is returned from the
browser)
                         *
                         * @param object a SelectChoice object
                         * @param index not used
                         * @return object.getKeyAsString()
                         */
                        @Todo(comment = "Replace Object with <T> once
Wicket incl. generics is available" )
                        public String getIdValue(Object object, int
index) {
                                String result = "A";
                                if (object instanceof Integer) {
                                        // WICKET BUG!!!
                                        LOG.debug("Integer returned");
                                } else {
                                        result =
((IntegerSelectChoice)object).getKeyAsString();
                                }
                                return result;
                        }

                }

Now, it works, but only because I'm forcing my renderer to omit the case
when it's being called with an Integer. My log gets hit four times and
there are four dropdowns of that type in my panel. Very weird stuff
going on here. Of course there's a chance that I screwed something up,
but I have been over my code with a fine toothed comb and everything
looks clean. I'm only using IntegerSelectChoice for populating my
DropDownChoice, never Integers directly.

Michael

-----Original Message-----
From: John Krasnay [mailto:[EMAIL PROTECTED] 
Sent: Friday, May 09, 2008 6:27 PM
To: users@wicket.apache.org
Subject: Re: DropDownChoice throws IllegalArgumentException with Integer
values

On Fri, May 09, 2008 at 03:39:07PM -0700, Michael Mehrle wrote:
> I like your solution, but you are using an IntegerSelectChoice as your
> model, which won't work for me. I need to set my model to a regular
> Integer since that's what's being persisted on the backend.
> 
> Michael

Quite right. I really don't like the idea of SelectChoice (a
view-related class) in a business model. Try this, Michael...

public MyBusinessClass {
  private int period; // getter/setter omitted for clarity
}

MyBusinessClass myObject = // blah
List periods = Arrays.asList(new Integer[] { 1, 7, 14, 30, 365 });

new DropDownChoice("period", 
  new PropertyModel(myObject, "period"),
  periods, 
  new ChoiceRenderer() {
    public String getDisplayValue(Object object) {
      int period = ((Integer) object).intValue();
      switch (period) {
        case 1: return "Day";
        case 7: return "Week";
        case 14: return "Fortnight";
        case 30: return "Month";
        case 365: return "Year";
        default: throw new RuntimeException();
      }
    }
  }
);

If like me you have to localize all your strings, it becomes even
simpler...

new DropDownChoice("period", 
  new PropertyModel(myObject, "period"),
  periods, 
  new ChoiceRenderer() {
    public String getDisplayValue(Object object) {
      return getString("period_" + object.toString());
    }
  }
);

...then in your properties file...

period_1=Day
period_7=Week
# ...and so on

jk

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to