Wow, you tryed hard to do this.
I had no more ideas, I'll try with this.

Thank you very much.

Tito

2011/6/5 Per Newgro <per.new...@gmx.ch>

> Hi Tito,
>
> i hope i got a valid solution for you. Please check it:
>
> a) You need to use a bean. I've tried it with simple strings like
> (1,2,7,24)
> and it wasn't working. I think because the renderer is called only with
> index 0.
> I don't know if this is a palette bug or a needed feature.
> b) you have to add the validator to the recorder component. This gets the
> list
> of ids selected in component. (Debug from here
> String[] ids = getIdsOfSelectedChoices(validatable);)
>
> hth
> Per
>
> <code>
>
> package ch.newgro.validpalette;
>
> import java.io.Serializable;
> import java.util.ArrayList;
> import java.util.Collection;
> import java.util.Collections;
> import java.util.List;
>
> import org.apache.wicket.extensions.markup.html.form.palette.Palette;
> import
> org.apache.wicket.extensions.markup.html.form.palette.component.Recorder;
> import org.apache.wicket.markup.html.WebPage;
> import org.apache.wicket.markup.html.form.ChoiceRenderer;
> import org.apache.wicket.markup.html.form.Form;
> import org.apache.wicket.markup.html.form.IChoiceRenderer;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
> import org.apache.wicket.model.util.ListModel;
> import org.apache.wicket.validation.IValidatable;
> import org.apache.wicket.validation.validator.AbstractValidator;
>
> /**
>  * Homepage
>  */
> public class HomePage extends WebPage {
>
>    class Bean implements Serializable {
>
>        public final int id;
>        public final String name;
>
>        public Bean(int id, String name) {
>            this.id = id;
>            this.name = name;
>        }
>
>        @Override
>        public String toString() {
>            return super.toString().concat(";
> id:").concat(String.valueOf(id)).concat("; name: ").concat(name);
>        }
>    }
>
>    private final List<Bean> beans = new ArrayList<Bean>(beans());
>
>    public HomePage() {
>        Form<Void> form;
>        add(form = new Form<Void>("form"));
>        IModel<List<Bean>> model = new ListModel<Bean>(beans);
>        Palette<Bean> palette = new Palette<Bean>("palette", model,
> choicesModel(), choiceRenderer(), 10, false) {
>            @Override
>            protected Recorder<Bean> newRecorderComponent() {
>                final Recorder<Bean> result = super.newRecorderComponent();
>                result.add(new AbstractValidator<Object>() {
>                    @Override
>                    protected void onValidate(IValidatable<Object>
> validatable) {
>                        String[] ids = getIdsOfSelectedChoices(validatable);
>                        printSelectedChoicesOf(result, ids);
>                    }
>
>                    private String[]
> getIdsOfSelectedChoices(IValidatable<Object> validatable) {
>                        return ((String) validatable.getValue()).split(",");
>                    }
>
>                    private void printSelectedChoicesOf(final Recorder<Bean>
> recorder, String[] idsOfSelectedChocies) {
>                        IChoiceRenderer<Bean> renderer =
> recorder.getPalette().getChoiceRenderer();
>                        if (idsOfSelectedChocies.length > 0) {
>                            for (String id : idsOfSelectedChocies) {
>                                Collection<? extends Bean> choices =
> recorder.getPalette().getChoices();
>                                for (Bean choice : choices) {
>                                    if (renderer.getIdValue(choice,
> 0).equals(id)) {
>                                        System.out.println(choice);
>                                    }
>                                }
>                            }
>                        }
>                    }
>                });
>                return result;
>            }
>        };
>        form.add(palette);
>    }
>
>    private List<Bean> beans() {
>        return Collections.emptyList(); // Arrays.asList(new Bean(0, "1"),
> new Bean(4, "11"), new Bean(8, "21"));
>    }
>
>    private IModel<List<? extends Bean>> choicesModel() {
>        return Model.ofList(allNames());
>    }
>
>    private List<? extends Bean> allNames() {
>        ArrayList<Bean> result = new ArrayList<Bean>();
>        result.add(bean(0, "1"));
>        result.add(bean(1, "2"));
>        result.add(bean(2, "3"));
>        result.add(bean(3, "4"));
>        result.add(bean(4, "11"));
>        result.add(bean(5, "12"));
>        result.add(bean(6, "13"));
>        result.add(bean(7, "14"));
>        result.add(bean(8, "21"));
>        result.add(bean(9, "22"));
>        result.add(bean(10, "23"));
>        result.add(bean(11, "24"));
>        return result;
>    }
>
>    private Bean bean(int id, String name) {
>        return new Bean(id, name);
>    }
>
>    private IChoiceRenderer<Bean> choiceRenderer() {
>        return new ChoiceRenderer<Bean>("name", "id");
>    }
>
> }
>
> </code>
>
>
>  No problem, thanks for your answer.
>> We always try to find a solution which is fine for me.
>>
>> When I call getSelectedChoices in Validation method it give us an empty
>> List
>> back. It is like the list it is not uploaded before entering the
>> validation
>> method.
>>
>> Thanks for helping me.
>>
>> Regards
>> Tito
>>
>> 2011/6/3 Per Newgro<per.new...@gmx.ch>
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
> For additional commands, e-mail: users-h...@wicket.apache.org
>
>

Reply via email to