user6450105
user6450105

Reputation:

How to set Root checkbox selected when every child is selected(GWT)

I am building GWT app where I have Tree and TreeItems with CheckBoxes. I have one root CheckBox called allCheckBox and his child elements rootCheckBox(this checkBoxes also have theirs children but that is not matter for this). I want that, when user opens dialog with checkBoxes, this checkBox is selected if every childCheckBox is selected. I have done that when tihs root checkBox is selected that child checkBoxes also are selected.
This is my piece of code:

enter cod  GWT_DOMAIN_SERVICE.findAll(new AsyncCallback<List<GwtDomain>>() {

        @Override
        public void onFailure(Throwable caught) {
            exitMessage = MSGS.dialogAddPermissionErrorDomains(caught.getLocalizedMessage());
            exitStatus = false;
            hide();
        }

        @Override
        public void onSuccess(List<GwtDomain> result) {

            for (final GwtDomain gwtDomain : result) {

                GWT_DOMAIN_SERVICE.findActionsByDomainName(gwtDomain.name(), new AsyncCallback<List<GwtAction>>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        exitMessage = MSGS.dialogAddPermissionErrorActions(caught.getLocalizedMessage());
                        exitStatus = false;
                        hide();
                    }

                    @Override
                    public void onSuccess(List<GwtAction> result) {
                        checkedItems = new GwtCheckedItems();
                        checkedItems.setName(gwtDomain);

                        rootCheckBox = new CheckBox();
                        rootCheckBox.setBoxLabel(gwtDomain.toString());
                        listCheckBoxes.add(rootCheckBox);
                        rootTreeItem = new TreeItem(rootCheckBox);
                        childCheckBoxMapList = new HashMap<GwtAction, CheckBox>();
                        checkedItems.setMap(childCheckBoxMapList);
                        for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
                            if (gwtAccessPermission.getPermissionDomain().toString().equals(checkedItems.getName().toString())) {
                                if (gwtAccessPermission.getPermissionAction().toString().equals(GwtAction.ALL.toString())) {
                                    rootCheckBox.setValue(true);
                                }
                            }
                        }
                        if (listOfNewClass.size() == checkedPermissionsList.size()) {
                            allCheckBox.setValue(true);
                        }
                        for (final GwtAction gwtAction : result) {

                            final CheckBox childTreeItemCheckox = new CheckBox();
                            treeItem = new TreeItem(childTreeItemCheckox);
                            childTreeItemCheckox.setBoxLabel(gwtAction.toString());

                            rootTreeItem.addItem(treeItem);

                            childListOfNewClass.add(gwtAction);
                            allTreeItem.addItem(rootTreeItem);
                            childCheckBoxMapList.put(gwtAction, childTreeItemCheckox);
                            for (GwtAccessPermission gwtAccessPermission : checkedPermissionsList) {
                                if (gwtAccessPermission.getPermissionDomain().toString().equals(gwtDomain.toString())) {

                                    if (gwtAccessPermission.getPermissionAction().toString().equals(gwtAction.toString())) {
                                        childTreeItemCheckox.setValue(true);
                                    }
                                }
                            }
                        }

                        listOfNewClass.put(checkedItems, rootCheckBox);

                    }

                });

            }

            allCheckBox.addListener(Events.OnClick, new Listener<BaseEvent>() {

                @Override
                public void handleEvent(BaseEvent be) {
                    if (allCheckBox.getValue()) {
                        for (CheckBox checkBox : listCheckBoxes) {
                            if (!checkBox.getValue()) {
                                checkBox.setValue(true);
                            }
                        }
                    } else {
                        for (CheckBox checkBox : listCheckBoxes) {
                            checkBox.setValue(false);
                        }
                    }
                }
            });

How to set that when all rootCheckBoxes are checked then allCheckBox become also checked?

EDIT: This checkedPermissionsList is List of rootCheckBox which are checked.

Upvotes: 0

Views: 206

Answers (1)

daniu
daniu

Reputation: 15028

Well the listener would have to iterate the child boxes and see if they are all selected, and set the parent box accordingly

Listener<BaseEvent> listener = new Listener<>() {
    public void handleEvent(BaseEvent be) {
        boolean allSet = listCheckBoxes.stream().allMatch(CheckBox::getValue);
        allCheckBox.setValue(allSet); // this will also unselect if appropriate
    }
 }

It's the same listener for all boxes, so add it to each

listCheckBoxes.forEach(box -> box.addListener(Event.OnClick, listener));

In the pre-Java 8 version:

Listener<BaseEvent> listener = new Listener<>() {
public void handleEvent(BaseEvent be) {
    boolean allSet = true;
    for (CheckBox child : listCheckBoxes) { 
        if (!child.getValue()) {
            allSet = false; // found a non-checked box
            break;
        }
    }
    allCheckBox.setValue(allSet); // this will also unselect if appropriate
}

// and set the listener to the children with
for (CheckBox box : listCheckBoxes) {
   box.addListener(Event.Clicked, listener);
}

Upvotes: 0

Related Questions