コード例 #1
0
ファイル: ListSelector.java プロジェクト: kalon33/osw-web
  public ListSelector(final RosterItem rosterItem) {

    this.rosterItem = rosterItem;

    addStyleName("listselector");
    StyledFlowPanel buttons = new StyledFlowPanel("buttons");
    add(wrapper);
    add(buttons);

    final TextBox input = new TextBox();
    Button add = new Button("Create new list");
    buttons.add(input);
    buttons.add(add);

    // get all available lists for your complete roster
    OswService service = OswServiceFactory.getService();
    Set<String> groups = service.getRoster().getGroups();

    // try to get all the lists this person is on
    try {
      listed = rosterItem.getGroups();
    } catch (NullPointerException e) {
      listed = new ArrayList<String>();
    }

    // add all lists as checkboxes
    if (groups.size() > 0) {
      for (String list : groups) {
        if (list != null) addCheckbox(list, false);
      }
    }

    add.addClickHandler(
        new ClickHandler() {

          public void onClick(ClickEvent event) {
            // add a new option to the list and check it
            addCheckbox(input.getText(), true);
            // empty the input field
            input.setText("");
          }
        });
  }
コード例 #2
0
ファイル: ListSelector.java プロジェクト: kalon33/osw-web
  public void addCheckbox(String list, Boolean value) {

    final CheckBox checkbox = new CheckBox(list);
    StyledFlowPanel fix = new StyledFlowPanel("fix");
    checkbox.addStyleName("checkbox");

    // manage checks and unchecks
    checkbox.addValueChangeHandler(
        new ValueChangeHandler<Boolean>() {

          @Override
          public void onValueChange(ValueChangeEvent<Boolean> event) {

            // is the item is checked?
            if (event.getValue() == true
                && !listed.contains(checkbox.getText())
                && checkbox.getText() != null
                && checkbox.getText().length() != 0) {
              // set the values
              listed.add(checkbox.getText());
              rosterItem.setGroups(listed);
              // disable during processing
              checkbox.setEnabled(false);

              // show task
              final DefaultTaskInfo task = new DefaultTaskInfo("Adding person to the list", false);
              TaskMonitor.getInstance().addTask(task);

              // save new state
              rosterItem.save(
                  new RequestCallback<RosterItem>() {

                    @Override
                    public void onFailure() {
                      // return to original state and notify user
                      checkbox.setEnabled(true);
                      checkbox.setValue(true);
                      task.complete("Could not add person to list.", Status.failure);
                    }

                    @Override
                    public void onSuccess(RosterItem result) {
                      // enable the checkbox again
                      checkbox.setEnabled(true);
                      task.complete("", Status.succes);
                    }
                  });
            } else if (event.getValue() == false && listed.contains(checkbox.getText())) {
              // set the values
              listed.remove(checkbox.getText());
              rosterItem.setGroups(listed);
              // disable during processing
              checkbox.setEnabled(false);

              // show task
              final DefaultTaskInfo task =
                  new DefaultTaskInfo("Removing person from the list", false);
              TaskMonitor.getInstance().addTask(task);

              // save new state
              rosterItem.save(
                  new RequestCallback<RosterItem>() {

                    @Override
                    public void onFailure() {
                      // return to original state and notify user
                      checkbox.setEnabled(true);
                      checkbox.setValue(true);
                      task.complete("Could not remove person from the list.", Status.failure);
                    }

                    @Override
                    public void onSuccess(RosterItem result) {
                      // enable the checkbox again
                      checkbox.setEnabled(true);
                      task.complete("", Status.succes);
                    }
                  });
            }
          }
        });

    // if this person is already on the list make sure the checkbox is
    // checked
    if (listed != null && list.length() > 0 && listed.contains(list)) {
      checkbox.setValue(true);
    } else if (value == true) {
      // if a new checkbox is added automatically check it and fire a
      // change event
      checkbox.setValue(true);
      checkbox.fireEvent(new ValueChangeEvent<Boolean>(true) {});
    }
    if (checkbox.getText() != null && checkbox.getText().length() != 0) {
      wrapper.add(checkbox);
      wrapper.add(fix);
    }
  }