2

I have created a palette in wicket and I'm able to populate the palette left side window. But I want to make the options in the palette are dynamic, able to do the same. But the problem is that the palette is not getting populate with selected options. I have a dropdown above the palette, depending on the option selected I want to populate the palette.

for example (dropdown(role) with values): 1.scenario: dropdown --> TL,Manager,SE,SSE pallete --> initially there will be no values, at the time of creating a role TL, there will be skill selected in the rite window of the palette. assume TL role is created with 4 skills outof 10 skills. 2.scenario: i want to edit the role TL, in this case when ever i choose the Role TL, the palette should get populate in such away that selected should be at right and available should be at left. i am failing in the second scenario.

Here is the code:

public class DropDownChoicePage extends WebPage {
private String selectedMake;
private final Map<String, List<String>> modelsMap = new HashMap<String, List<String>>();
private List<String> selected = new ArrayList<String>();
private String selectedEngine = "Google";
public DropDownChoicePage(final PageParameters parameters) {
    add(new FeedbackPanel("feedback"));
    modelsMap.put("AUDI", Arrays.asList(new String[] { "A4", "A6", "TT" }));
    modelsMap.put("CADILLAC", Arrays.asList(new String[] { "CTS", "DTS", "ESCALADE", "SRX","DEVILLE" }));
    modelsMap.put("FORD", Arrays.asList(new String[] { "CROWN", "ESCAPE", "EXPEDITION","EXPLORER", "F-150" }));

    IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>()
    {
        private static final long serialVersionUID = 1L;
        @Override
        public List<String> getObject()
        {
            Set<String> keys = modelsMap.keySet();
            List<String> list = new ArrayList<String>(keys);
            return list;
        }
    };
    IModel<List<? extends String>> paletteChoices = new AbstractReadOnlyModel<List<? extends String>>(){
        private static final long serialVersionUID = 1L;
        @Override
        public List<? extends String> getObject() {
            List<String> models = modelsMap.get(selectedMake);
            if (models == null)
            {
                models = Collections.emptyList();
            }else if("FORD".equals(selectedMake)){
                selected.add("CROWN");
                selected.add("ESCAPE");
            }
            return models;
        }
    };
    final DropDownChoice<String> makes = new DropDownChoice<String>("makes",new PropertyModel<String>(this, "selectedMake"), makeChoices);
    IChoiceRenderer<String> renderer = new ChoiceRenderer<String>();
    final Palette<String> palette = new Palette<String>("palette",
            new ListModel<String>(selected),
            paletteChoices,
            renderer, 10, true);
    palette.setOutputMarkupId(true);
    makes.add(new AjaxFormComponentUpdatingBehavior("onchange")
    {
        private static final long serialVersionUID = 1L;
        @Override
        protected void onUpdate(AjaxRequestTarget target)
        {
            selected = new ArrayList<String>();
            target.addComponent(palette);
            System.out.println("AjaxFormComponentUpdatingBehavior(\"onchange\")");
            System.out.println("Selected Make:"+selectedMake);
        }
    });
    Form<?> form = new Form<Void>("form") {
        private static final long serialVersionUID = 1L;
        @Override
        protected void onSubmit() {
            info("Selected search engine : " + selectedEngine);
            selected = new ArrayList<String>();
        }
    };
    add(form);
    form.add(palette);
    form.add(makes);
}
}

Can you please help me out in the second scenario,i.e populating both left and right windows of the palette with available and selected values on change event of the drop down.

thanks, Saran.

Martin
  • 1,274
  • 12
  • 23
user1665207
  • 61
  • 1
  • 6

1 Answers1

1

I think there are two main problems in your code which will prevent the second scenario to work correctly:

(1) In the onchange Ajax Behavior, the following code will NOT update the ListModel of selected choices:

selected = new ArrayList<String>();

Try this instead:

selected.clear();

(2) The code where you add the already selected choices to the list should be in the Ajax Behavior, i.e.:

makes.add(new AjaxFormComponentUpdatingBehavior("onchange") {
    private static final long serialVersionUID = 1L;
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        selected.clear();
        List<String> alreadySelected = getAlreadySelectedChoicesBasedOnDropdownValue();
        selected.addAll(alreadySelected);
        //...
    }
}

Where getAlreadySelectedChoicesBasedOnDropdownValue would return a new list of already selected choices, like:

List<String> getAlreadySelectedChoicesBasedOnDropdownValue() {
    if("FORD".equals(selectedMake)){
        return Arrays.asList("CROWN", "ESCAPE");
    }
}
David Tanzer
  • 2,732
  • 18
  • 30
  • I Tried the same but it is not working :( . i have observed on thing here is that, when ever i select choice apart from FORD, the paletteChoices rendering logic is executing only once which is an expected behaviour, but when i select the option FORD the paletteChoices is getting execute trice i lil confused why it is behaving like this. – user1665207 Jun 26 '14 at 06:28