예제 #1
0
  /** Move items from components to others. */
  public static void moveAllItems(
      UISelectItems sourceItems,
      UISelectItems targetItems,
      UIEditableList hiddenTargetList,
      boolean setTargetIds) {
    SelectItem[] all = (SelectItem[]) sourceItems.getValue();
    List<SelectItem> toMove = new ArrayList<SelectItem>();
    List<SelectItem> toKeep = new ArrayList<SelectItem>();
    List<String> hiddenIds = new ArrayList<String>();
    if (all != null) {
      for (SelectItem item : all) {
        if (!item.isDisabled()) {
          toMove.add(item);
        } else {
          toKeep.add(item);
        }
      }
    }
    // reset left values
    sourceItems.setValue(toKeep.toArray(new SelectItem[] {}));

    // change right values
    List<SelectItem> newSelectItems = new ArrayList<SelectItem>();
    SelectItem[] oldSelectItems = (SelectItem[]) targetItems.getValue();
    if (oldSelectItems == null) {
      newSelectItems.addAll(toMove);
    } else {
      newSelectItems.addAll(Arrays.asList(oldSelectItems));
      List<String> oldIds = new ArrayList<String>();
      for (SelectItem oldItem : oldSelectItems) {
        String id = oldItem.getValue().toString();
        oldIds.add(id);
      }
      if (setTargetIds) {
        hiddenIds.addAll(0, oldIds);
      }
      for (SelectItem toMoveItem : toMove) {
        String id = toMoveItem.getValue().toString();
        if (!oldIds.contains(id)) {
          newSelectItems.add(toMoveItem);
          if (setTargetIds) {
            hiddenIds.add(id);
          }
        }
      }
    }
    targetItems.setValue(newSelectItems.toArray(new SelectItem[] {}));

    // update hidden values
    int numValues = hiddenTargetList.getRowCount();
    if (numValues > 0) {
      for (int i = numValues - 1; i > -1; i--) {
        hiddenTargetList.removeValue(i);
      }
    }
    for (String newHiddenValue : hiddenIds) {
      hiddenTargetList.addValue(newHiddenValue);
    }
  }
예제 #2
0
 static void addToTargetList(UIEditableList targetList, SelectItem[] items) {
   for (int i = 0; i < items.length; i++) {
     targetList.addValue(items[i].getValue());
   }
 }