示例#1
0
  public TeamViewModel() {
    createValidators();

    ListProperty<Person> peopleInOrganisation = new SimpleListProperty<>();
    peopleInOrganisation.bind(
        Bindings.createObjectBinding(
            () -> {
              if (organisationProperty().get() != null) {
                return organisationProperty().get().getPeople();
              } else {
                return FXCollections.observableArrayList();
              }
            },
            organisationProperty()));

    ListProperty<Team> teamsInOrganisation = new SimpleListProperty<>();
    teamsInOrganisation.bind(
        Bindings.createObjectBinding(
            () -> {
              if (organisationProperty().get() != null) {
                return organisationProperty().get().getTeams();
              } else {
                return FXCollections.observableArrayList();
              }
            },
            organisationProperty()));

    ListProperty<Person> peopleInTeams = new SimpleListProperty<>();
    peopleInTeams.bind(
        Bindings.createObjectBinding(
            () -> {
              return teamsInOrganisation
                  .stream()
                  .flatMap(team -> team.observableTeamMembers().stream()) // TODO listen
                  .collect(GoatCollectors.toObservableList());
            },
            teamsInOrganisation));

    eligibleTeamMembers = new SimpleListProperty<>();
    eligibleTeamMembers.bind(
        Bindings.createObjectBinding(
            () -> {
              return peopleInOrganisation
                  .stream()
                  .filter(
                      person ->
                          !peopleInTeams.contains(person) // no team in model
                              || person.getTeam() == modelWrapper.get()) // this team in model
                  .collect(GoatCollectors.toObservableList());
            },
            peopleInOrganisation,
            peopleInTeams));

    eligibleDevs = new SimpleListProperty<>();
    eligibleDevs.bind(
        Bindings.createObjectBinding(
            () -> {
              return teamMembersProperty()
                  .get()
                  .stream() // is in team
                  .filter(
                      person ->
                          !person.equals(productOwnerProperty().get()) // not PO
                              && !person.equals(scrumMasterProperty().get())) // not SM
                  .collect(GoatCollectors.toObservableList());
            },
            teamMembersProperty(),
            productOwnerProperty(),
            scrumMasterProperty()));

    teamMembersProperty()
        .addListener(
            (ListChangeListener.Change<? extends Person> change) -> {
              change.next();
              List<? extends Person> removed = change.getRemoved();
              devTeamProperty().removeAll(removed);
              if (removed.contains(productOwnerProperty().get())) {
                productOwnerProperty().set(null);
              }
              if (removed.contains(scrumMasterProperty().get())) {
                scrumMasterProperty().set(null);
              }
            });
  }