Wicket DropDownChoice return integer from value

308 Views Asked by At

here you see a working example for a wicket DropDownChoice, but this is not what I would like to have. The returned value of the dropdownchoice should be an integer matching the value. The return value of the dropdownchoice is:

returnedLabel=123 (String)
returnedDDCValue=LabelValue [label=TestB, value=3] (LabelValue)

For production returnedDDCValue should be an integer, filled with (integer) 3. I think that there must be a conversion done inside the ddc. I tried with IChoiceRenderer, but did not succeed. Plz help :)

Cheers

chris

public class DDCTest extends Panel {

    private List<LabelValue> list4ddc;

    public DDCTest(String id) {
        super(id);
        Result result = new Result();

        final FeedbackPanel feedback = new FeedbackPanel("feedback");
        feedback.setOutputMarkupId(true);
        add(feedback);

        this.list4ddc = new ArrayList<LabelValue>();
        this.list4ddc.add(new LabelValue("TestA", 1));
        this.list4ddc.add(new LabelValue("TestB", 3));
        this.list4ddc.add(new LabelValue("TestC", 5));
        this.list4ddc.add(new LabelValue("TestD", 7));

        Form form = new Form("form");
        add(form);

        form.add(new TextField<String>("returnedLabel", new PropertyModel<String>(result,"returnedLabel")));

        form.add( new DropDownChoice("returnedDDCValue", new PropertyModel(result,"returnedDDCValue"), Model.of(list4ddc), new ChoiceRenderer("label", "value")));

        form.add(new AjaxButton("submit", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                info("["+result.toString()+"]");
                target.add(feedback);
            }
        });

    }

    public class Result implements Serializable {
        private String returnedLabel;
        private String returnedDDCValue;

        public Result() {
            super();
            this.returnedLabel = "";
            this.returnedDDCValue = null;
        }

        [ ... Getters and setters ... ]

    }

    public class LabelValue implements Serializable {
        private String label;
        private Integer value;

        public LabelValue(String label, Integer value) {
            super();
            this.label = label;
            this.value = value;
        }

        [ ... Getters and setters ... ]
    }
}
1

There are 1 best solutions below

2
Andrea Del Bene On

I think I've did something very similar to what you need. It's a custom DDC which uses choice's value to set a third model:

public class DropDownChoiceForString<T> extends DropDownChoice<T> {

private IModel<String> targetModel;

public DropDownChoiceForString(String id, IModel<T> model, IModel<String> targetModel, 
        List<? extends T> choices,
        IChoiceRenderer<? super T> renderer) {
    super(id, model, choices, renderer);
    this.targetModel = targetModel;
}

protected DropDownChoiceForString(String id, IModel<T> model, IModel<String> targetModel) {
    this(id, model, targetModel, Collections.<T> emptyList(), null);
}

@Override
protected void onInitialize() {
    super.onInitialize();
    // load the initial choice.
    setModelObject(convertChoiceIdToChoice(targetModel.getObject()));
}

@Override
protected void onDetach() {
    super.onDetach();

    targetModel.detach();
}

@Override
protected void onModelChanged() {
    super.onModelChanged();

    T newSelection = getModelObject();

    int choiceIndex = getChoices().indexOf(newSelection);
    // update the string source with the selected value.
    targetModel.setObject(getChoiceRenderer().getIdValue(newSelection, choiceIndex));
}}

It works like a standard DDC but when its model is changed a target model is updated with the value of the new choice.