Yikes. You're converting your domain data to some crazy type (NameValuePair)
just so you can display it differently. Not good.

Map<String, String> choices = new TreeMap<String, String>();
choices.put("foo", "Foo display value");

add(new DropDownChoice("foo", model, choices, new IChoiceRenderer() {
    Object getDisplayValue(Object object) {
        return choices.get(object);
    }
    String getIdValue(Object object, int index) {
        return object;
    }
});

Define that choice renderer in a separate class if you need to reuse it
across drop-downs. Call it a MapChoiceRenderer or something.

You really ought to try to use a more useful data type than a String in here
so you don't need to do lots of conversion later. It looks like you have
time intervals. Use a Joda Interval or TimeOfDay + Duration pair.

Unless you back your solution with a proper date/time library and
auto-generated values, your solution will fail when there is a day on a
daylight savings boundary, as you will either incorrectly display an hour
that doesn't exist, or you will not display the midnight hour twice (or
whatever the rules are for your particular timezone). I'd urge you to use
Joda or java.util.Calendar or similar to obviate the need to think about
this stuff too hard.

Regards,

Al

On Tue, Apr 1, 2008 at 7:26 PM, Andrew Broderick <[EMAIL PROTECTED]>
wrote:

> Since it wasn't there, I extended DropDownChoice to encapsulate the whole
> name/value pair thing:
>
> package ****.****.****.helper.ui;
>
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Map;
>
> import org.apache.wicket.markup.html.form.ChoiceRenderer;
> import org.apache.wicket.markup.html.form.DropDownChoice;
> import org.apache.wicket.model.AbstractPropertyModel;
>
> public class EasyDropDownChoice extends DropDownChoice
> {
>        public EasyDropDownChoice(String componentId, AbstractPropertyModel
> model, String[][] options)
>        {
>                this(componentId, model, toList(options));
>        }
>
>        public EasyDropDownChoice(String componentId, AbstractPropertyModel
> model, Map options)
>        {
>                this(componentId, model, toList(options));
>        }
>
>        public EasyDropDownChoice(String componentId, AbstractPropertyModel
> model, List<NameValuePair> options)
>        {
>                super(componentId, model, options, new
> ChoiceRenderer("value", "id"));
>        }
>
>        private static List<NameValuePair> toList(String[][] choices)
>        {
>                ArrayList<NameValuePair> options = new
> ArrayList<NameValuePair>();
>
>                for (int n = 0; n < choices.length; n++)
>                {
>                        options.add(new NameValuePair(choices[n][0],
> choices[n][1]));
>                }
>
>                return options;
>        }
>
>        private static List<NameValuePair> toList(Map<String, String>
> choices)
>        {
>                ArrayList<NameValuePair> options = new
> ArrayList<NameValuePair>();
>
>                for (String key: choices.keySet())
>                {
>                        options.add(new NameValuePair(key, choices.get
> (key)));
>                }
>
>                return options;
>        }
> }
>
> Which depends on:
>
> package ***.*****.****.helper.ui;
>
> import java.io.Serializable;
>
> public class NameValuePair implements Serializable
> {
>        private String id;
>        private String value;
>
>        public NameValuePair(String id, String value)
>        {
>                this.id = id;
>                this.value = value;
>        }
>
>        public String getId()
>        {
>                return id;
>        }
>
>        public void setId(String id)
>        {
>                this.id = id;
>        }
>
>        public String getValue()
>        {
>                return value;
>        }
>
>        public void setValue(String value)
>        {
>                this.value = value;
>        }
>
> }
>
> To initialize it with an array of choices, you now just have to do this:
>
> String[][] eventTimes = new String[][] { {"7:00:00", "7:00 AM"},
>
>                                       {"8:00:00", "8:00 AM"},
>
>                                       {"9:00:00", "9:00 AM"},
>
>                                       {"10:00:00", "10:00 AM"},
>
>                                       {"11:00:00", "11:00 AM"},
>
>                                       {"12:00:00", "12:00 PM"},
>
>                                       {"13:00:00", "1:00 PM"},
>
>                                       {"14:00:00", "2:00 PM"},
>
>                                       {"15:00:00", "3:00 PM"},
>
>                                       {"16:00:00", "4:00 PM"},
>
>                                       {"17:00:00", "5:00 PM"},
>
>                                       {"18:00:00", "6:00 PM"} };
>
> add(new EasyDropDownChoice("eventTime", new PropertyModel(this,
> "eventTime"), eventTimes));
>
> You can construct it with either a nested string array or a Map. Just have
> to make sure the property it's bound to is of type NameValuePair.
>
> -Andrew
>
> _______________________________________________________
>
> The  information in this email or in any file attached
> hereto is intended only for the personal and confiden-
> tial  use  of  the individual or entity to which it is
> addressed and may contain information that is  propri-
> etary  and  confidential.  If you are not the intended
> recipient of this message you are hereby notified that
> any  review, dissemination, distribution or copying of
> this message is strictly prohibited.  This  communica-
> tion  is  for information purposes only and should not
> be regarded as an offer to sell or as  a  solicitation
> of an offer to buy any financial product. Email trans-
> mission cannot be guaranteed to be  secure  or  error-
> free. P6070214
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>

Reply via email to