2017-10-05 5 views
0

Tree와 TreeItems에 CheckBoxes가있는 GWT 응용 프로그램을 작성합니다. 하나의 루트 CheckBox는 allCheckBox 및 그의 자식 요소 인 rootCheckBox를 가지고 있습니다 (이 checkBoxes에도 children이 있지만 이것에 대해서는 중요하지 않습니다). checkBoxes를 사용하여 대화 상자를 열면 모든 childCheckBox가 선택되면이 확인란이 선택되기를 원합니다. tihs root checkBox가 선택되었을 때 child checkBoxes도 선택되었습니다.모든 자식 (GWT)가 선택 될 때 루트 확인란을 선택하는 방법

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); 
         } 
        } 
       } 
      }); 

어떻게 모든 rootCheckBoxes 다음을 확인하는 경우도 확인 될 allCheckBox 것을 설정 :
이 코드의 내 작품이다?

편집 :이 checkedPermissionsList는 확인 된 rootCheckBox의 목록입니다.

+0

당신이 당신의'childTreeItemCh에 부착 된 청취자가있는 경우 eckox'는 모든 것이 선택되었는지 확인하고 그에 따라 루트를 설정할 수있는 곳입니다. – WLGfx

답변

1

잘 듣는 아이 박스를 반복하고 그들은 모두 선택되어 있는지 확인하고, 부모 상자를 설정해야 할 것입니다 따라

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 
    } 
} 

그것은 모든 상자에 대해 동일한 수신기, 그래서 각

에 추가 전 자바 8 버전에서
listCheckBoxes.forEach(box -> box.addListener(Event.OnClick, listener)); 

:

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); 
} 
+0

문제가 아닌 경우 Java 8없이 게시 할 수 있습니까? – Atenica

+0

@Atenica가 Java 8이 아닌 버전을 추가하기 위해 편집했습니다. – daniu

+0

훌륭합니다! 고마워, +1. – Atenica