@Override
 protected CheckBoxItem newItem(Group group, Object key) {
   CheckBoxItem box = super.newItem(group, key);
   if (group.hasAssociations()) {
     GroupPanel panel = new GroupPanel(box, group, apps, regions);
     panels.put(group.getId(), panel);
     return panel;
   } else {
     return box;
   }
 }
  @Override
  public void setValue(List<Group> groups, boolean fireEvents) {
    List<Group> before = this.selectedValues;
    super.setValue(groups, false);

    if (groups != null) {
      for (Group g : groups) {
        GroupPanel panel = panels.get(g.getId());
        if (panel != null) {
          panel.setGroup(g);
        }
      }
    }
    if (fireEvents) {
      ValueChangeEvent.fireIfNotEqual(this, before, groups);
    }
  }
 public List<Integer> getSelectedApplicationIds() {
   if (applications.getItemCount() == 0) {
     return group.getApplicationIds();
   }
   List<Integer> result = new ArrayList<Integer>(applications.getItemCount());
   for (int index = 0; index < applications.getItemCount(); index++) {
     if (applications.isItemSelected(index)) {
       int id = Integer.parseInt(applications.getValue(index));
       result.add(id);
     }
   }
   return result;
 }
 void resetApplications(List<Application> apps) {
   if (apps != null && !apps.isEmpty()) {
     applications.clear();
     for (Application a : apps) {
       applications.addItem(a.toDisplay(), a.getId() + "");
     }
     for (int index = 0; index < applications.getItemCount(); index++) {
       int id = Integer.parseInt(applications.getValue(index));
       applications.setItemSelected(index, group.getApplicationIds().contains(id));
     }
   }
   adjustListBox(box.getValue());
 }
    GroupPanel(CheckBoxItem box, Group group, List<Application> apps, List<Region> regions) {
      this.group = group;
      this.box = box;
      box.addValueChangeHandler(
          new ValueChangeHandler<Boolean>() {

            public void onValueChange(ValueChangeEvent<Boolean> event) {
              adjustListBox(event.getValue());
            }
          });

      add(box);
      if (group.hasApplications()) {
        add(new Label("applications"));
        add(applications);
        if (apps != null && !apps.isEmpty()) {
          applications.clear();
          for (Application a : apps) {
            applications.addItem(a.toDisplay(), a.getId() + "");
          }
        } else {
          applications.addItem("loading . . .");
        }
        applications.setEnabled(false);
      }
      if (group.hasRegions()) {
        add(new Label("regions"));
        add(this.regions);
        if (regions != null && !regions.isEmpty()) {
          regions.clear();
          for (Region a : regions) {
            this.regions.addItem(a.toDisplay(), a.getId() + "");
          }
        } else {
          this.regions.addItem("loading . . .");
        }
        this.regions.setEnabled(false);
      }
    }
 void resetRegions(List<Region> regions) {
   if (regions != null && !regions.isEmpty()) {
     this.regions.clear();
     for (Region a : regions) {
       this.regions.addItem(a.toDisplay(), a.getId() + "");
     }
     for (int index = 0; index < this.regions.getItemCount(); index++) {
       int id = Integer.parseInt(this.regions.getValue(index));
       this.regions.setItemSelected(index, group.getRegionIds().contains(id));
     }
   }
   adjustListBox(box.getValue());
 }
 public List<Integer> getSelectedRegionIds() {
   GWT.log("get selected region ids");
   if (regions.getItemCount() == 0) {
     return group.getApplicationIds();
   }
   List<Integer> result = new ArrayList<Integer>(regions.getItemCount());
   for (int index = 0; index < regions.getItemCount(); index++) {
     if (regions.isItemSelected(index)) {
       int id = Integer.parseInt(regions.getValue(index));
       result.add(id);
     }
   }
   GWT.log("get selected region ids" + result);
   return result;
 }
 @Override
 public List<Group> getValue() {
   List<Group> groups = super.getValue();
   GWT.log("get valle" + groups);
   for (Group g : groups) {
     GroupPanel panel = panels.get(g.getId());
     if (panel != null) {
       GWT.log("get valle" + g.hasRegions());
       if (g.hasApplications()) {
         g.setApplicationIds(panel.getSelectedApplicationIds());
       }
       if (panel.hasRegions()) {
         g.setRegionIds(panel.getSelectedRegionIds());
       }
       GWT.log("get valle" + g.getRegionIds());
     }
   }
   return groups;
 }
 public void setGroup(Group g) {
   this.group = g;
   setApplicationIds(g.getApplicationIds());
   setRegionIds(g.getRegionIds());
 }