2017-10-05 9 views
0

私はチェックボックス付きのTreeとTreeItemを持つGWTアプリケーションをビルドしています。私はallCheckBoxとその子要素rootCheckBoxと呼ばれる1つのルートCheckBoxを持っています(このチェックボックスには子がありますが、これは問題ではありません)。私はそれが欲しい、ユーザーがcheckBoxesを使ってダイアログを開くと、すべてのchildCheckBoxが選択されると、このチェックボックスが選択されます。私はtihsルートチェックボックスが選択されているときにそれを行いました、子チェックボックスも選択されています。
これは、コードの私の作品です:すべての子供が選択されるとルートチェックボックスが選択されます(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がチェックにもなっていることを設定する方法は?

EDIT:この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 
    } 
} 

それはすべてのボックスに同じリスナーですので、各

に追加前のJava 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

関連する問題