Search code examples
javacheckboxgwtgxt

How to make a list of objects which are checked in GWT?


I am developing an GWT app where I am using check-boxes. I have list of GwtRoles which are in checkboxes, but I don't know how to get those GwtRoles that are checked. This is my code:

@Override
    public void createBody() {

    for (GwtRole gwtRole : roleList) {
                checkBox = new CheckBox();
                checkBox.setBoxLabel(gwtRole.getName());
                for (GwtAccessRole gwtAccessRole : lista) {
                    if (gwtRole.getId().equals(gwtAccessRole.getRoleId())) {
                        checkBox.setValue(true);
                    }

RoleList is list of GwtRoles that are in checkboxes. This lista is a list of items that should be pre-checked when user opens form. I am not really familiar with check-boxes in GWT. I have used CheckBox list and there I had method getChecked() which returns list of all checked GwtRoles, but here with this check-boxes I don't have that option. In this method I should make a list of GwtRoles which are checked:

 @Override
    public void submit() {

        List<GwtAccessRoleCreator> listCreator = new ArrayList<GwtAccessRoleCreator>();

        for (GwtRole role : list) {
            GwtAccessRoleCreator gwtAccessRoleCreator = new GwtAccessRoleCreator();

            gwtAccessRoleCreator.setScopeId(currentSession.getSelectedAccount().getId());

            gwtAccessRoleCreator.setAccessInfoId(accessInfoId);

            gwtAccessRoleCreator.setRoleId(role.getId());
            listCreator.add(gwtAccessRoleCreator);
        }
        GWT_ACCESS_ROLE_SERVICE.createCheck(xsrfToken, currentSession.getSelectedAccount().getId(), userId, listCreator, new AsyncCallback<GwtAccessRole>() {

            @Override
            public void onSuccess(GwtAccessRole arg0) {
                exitStatus = true;
                exitMessage = MSGS.dialogAddConfirmation();
                hide();
            }

Could someone helps me how to set a list of GwtRoles which are checked?


Solution

  • Keep track of your CheckBoxs in a Map and then just return the GwtRoles for which the checkbox is checked.

    private Map<GwtRole, CheckBox> mapping = new HashMap<>();
    
    @Override
    public void createBody() {
        for (GwtRole gwtRole : roleList) {
            CheckBox checkBox = new CheckBox();
            mapping.put(gwtRole, checkBox);
            // Your other code here.
        }
    }
    
    // Java 8
    public List<GwtRole> getChecked()
    {
        return mapping.entrySet().stream()
            .filter(e -> e.getValue().getValue())
            .map(Map.Entry::getKey)
            .collect(Collectors.toList());
    }
    
    // Pre-Java 8
    public List<GwtRole> getChecked()
    {
        List<GwtRole> result = new ArrayList<>();
    
        for(Map.Entry<GwtRole, CheckBox> e : map.entrySet())
        {
            if(e.getValue().getValue())
                result.add(e.getKey());
        }
    
        return result;
    }