package org.apache.wicket.examples.compref;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.wicket.examples.WicketExamplePage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Check;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.CheckBoxMultipleChoice;
import org.apache.wicket.markup.html.form.CheckBoxSelector;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.CheckGroupSelector;
import org.apache.wicket.markup.html.form.CheckboxMultipleChoiceSelector;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
@link
public class CheckBoxSelectorPage extends WicketExamplePage
{
private static final long serialVersionUID = 1L;
@param
public CheckBoxSelectorPage(final PageParameters parameters)
{
super(parameters);
final Form<?> form = new Form<>("form");
add(form);
final CheckGroup<Integer> checkgroup = new CheckGroup<>("checkgroup", Arrays.asList(
1, 2, 3, 4));
form.add(checkgroup);
checkgroup.add(new Check<>("check1", Model.of(1)));
checkgroup.add(new Check<>("check2", Model.of(2)));
checkgroup.add(new Check<>("check3", Model.of(3)));
checkgroup.add(new Check<>("check4", Model.of(4)));
checkgroup.add(new CheckGroupSelector("groupSelector"));
form.add(new CheckGroupSelector("groupSelectorOutside", checkgroup));
final CheckBoxMultipleChoice<Integer> choice = new CheckBoxMultipleChoice<>(
"choice", Model.ofList(new ArrayList<>()), Arrays.asList(1, 2, 3, 4));
form.add(choice);
form.add(new CheckboxMultipleChoiceSelector("choiceSelector", choice));
final CheckBox loose1 = new CheckBox("looseCheck1", Model.of(Boolean.FALSE));
final CheckBox loose2 = new CheckBox("looseCheck2", Model.of(Boolean.FALSE));
final CheckBox loose3 = new CheckBox("looseCheck3", Model.of(Boolean.FALSE));
final CheckBox loose4 = new CheckBox("looseCheck4", Model.of(Boolean.FALSE));
form.add(loose1, loose2, loose3, loose4);
form.add(new CheckBoxSelector("looseSelector", loose1, loose2, loose3, loose4));
final List<Integer> extensibleChoices = new ArrayList<>(Arrays.asList(1, 2, 3));
final Set<Integer> selected = new HashSet<>();
final ListView<Integer> listView = new ListView<Integer>("list", extensibleChoices) {
@Override
protected void populateItem(final ListItem<Integer> item) {
final CheckBox check = new CheckBox("check", new Model<Boolean>() {
@Override
public Boolean getObject() {
return selected.contains(item.getModelObject());
}
@Override
public void setObject(Boolean object) {
if (object) {
selected.add(item.getModelObject());
} else {
selected.remove(item.getModelObject());
}
}
});
check.setLabel(() -> item.getModelObject().toString());
item.add(check);
}
};
listView.setReuseItems(true);
form.add(listView);
form.add(new Button("addChoice") {
@Override
public void onSubmit() {
extensibleChoices.add(extensibleChoices.get(extensibleChoices.size() - 1) + 1);
}
});
form.add(new Button("removeChoice") {
@Override
protected void onConfigure()
{
super.onConfigure();
setEnabled(extensibleChoices.size() > 1);
}
public void onSubmit()
{
Integer integer = extensibleChoices.get(extensibleChoices.size() - 1);
extensibleChoices.remove(integer);
selected.remove(integer);
}
});
final CheckBoxSelector extensibleSelector = new CheckBoxSelector("extensibleSelector") {
@Override
protected Iterable<? extends CheckBox> getCheckBoxes()
{
return collectCheckBoxes(listView);
}
};
form.add(extensibleSelector);
}
}