0

I have a single setting that I want to use in multiple locations. I need to get and set an Integer value: 0, 1, 2, -1. The displayed string are not stored.

Fragment of the html:

<select wicket:id="thingPref">
    <option>1</option>
    <option>2</option>
</select>

Fragment of the Java.

import org.apache.wicket.extensions.markup.html.form.select.SelectOption;
import org.apache.wicket.markup.html.form.DropDownChoice;

private SelectOption thingPreference; 

private void buildCommon()
{
    thingPreference = new SelectOption(Integer.toString(setting()), new Model<String>("dummy"));
    List<SelectOption> prefChoices = new ArrayList<SelectOption>();
    prefChoices.add(new SelectOption<String>("0", new Model<String>("Current Thing")));
    prefChoices.add(new SelectOption<String>("1", new Model<String>("One Prior Thing")));
    prefChoices.add(new SelectOption<String>("2", new Model<String>("Two Prior Things")));
    prefChoices.add(new SelectOption<String>("-1", new Model<String>("All Things")));
    DropDownChoice<SelectOption> prefselect = new DropDownChoice<SelectOption>("thingPref", 
            new PropertyModel<SelectOption>(this, "thingPreference"), prefChoices)
    {
        private static final long serialVersionUID = 1L;

        protected boolean wantOnSelectionChangedNotifications()
        {
            return true;
        }

        protected void onSelectionChanged(final String newSelection)
        {
            System.out.format("onSelectionChanged(%s)%n", newSelection);
        }
    };
    prefselect.setNullValid(false);
    prefselect.setRequired(true);
    add(prefselect);
}

Developer Tools shows html like this

<option value="3">[SelectOption [Component id = -1]]</option>

when I need

<option value="-1">All Things</option>

The solution does not look to be as simple as adding a renderer and I am not sure that the setting should use an enum. I am using Wicket 7.6. Any ideas?

JoeAB
  • 53
  • 9

2 Answers2

1

org.apache.wicket.extensions.markup.html.form.select.SelectOption should be used with org.apache.wicket.extensions.markup.html.form.select.Select, not with org.apache.wicket.markup.html.form.DropDownChoice.

For your use case you can use either DropDownChoice or Select. In both cases you will need custom IChoiceRenderer.

Select should be preferred than DropDownChoice when you need more control on each and every option. DropDownChoice works with a list of POJOs.

martin-g
  • 17,243
  • 2
  • 23
  • 35
0

I applied a custom renderer to show the data options.

private static final List<Integer> TermSettings = Arrays.asList(0, 1, 2, -1);

ChoiceRenderer<Integer> choiceRenderer = new ChoiceRenderer<Integer>()
{
    private static final long serialVersionUID = 1L;

    @Override
    public Object getDisplayValue(Integer value)
    {
        switch (value)
        {
            case 0:
                return "Current Term";
            case 1:
                return "One Prior Term";
            case 2:
                return "Two Prior Terms";
            case -1:
                return "All Terms";
            default:
                throw new IllegalStateException(value + " is not mapped!");
        }
    }

    @Override
    public String getIdValue(Integer object, int index)
    {
        Integer idvalue = TermSettings.get(index);
        String strvalue = String.valueOf(idvalue);
        return strvalue;
    }
};
JoeAB
  • 53
  • 9