Example #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);
    }
  }
  /**
   * Initializes the <code>items</code> instance variable with an <code>Iterator</code> appropriate
   * to the UISelectItem(s) value.
   */
  private void initializeItems() {

    UIComponent kid = (UIComponent) findNextValidChild();
    if (kid instanceof UISelectItem) {
      UISelectItem ui = (UISelectItem) kid;
      SelectItem item = (SelectItem) ui.getValue();
      if (item == null) {
        item =
            new SelectItem(
                ui.getItemValue(),
                ui.getItemLabel(),
                ui.getItemDescription(),
                ui.isItemDisabled(),
                ui.isItemEscaped(),
                ui.isNoSelectionOption());
      }
      updateSingeItemIterator(item);
      items = singleItemIterator;
    } else if (kid instanceof UISelectItems) {
      UISelectItems ui = (UISelectItems) kid;
      Object value = ui.getValue();
      if (value instanceof SelectItem) {
        updateSingeItemIterator((SelectItem) value);
        items = singleItemIterator;
      } else if (value.getClass().isArray()) {
        items = new ArrayIterator(ctx, (UISelectItems) kid, value);
      } else if (value instanceof Iterable) {
        items = new IterableItemIterator(ctx, (UISelectItems) kid, (Iterable<?>) value);
      } else if (value instanceof Map) {
        items = new MapIterator((Map) value);
      } else {
        throw new IllegalArgumentException();
      }
    }
  }
Example #3
0
  protected List<SelectItem> getSelectItems(FacesContext context, UIInput component) {
    List<SelectItem> selectItems = new ArrayList<SelectItem>();

    for (UIComponent child : component.getChildren()) {
      if (child instanceof UISelectItem) {
        UISelectItem uiSelectItem = (UISelectItem) child;
        Object selectItemValue = uiSelectItem.getValue();

        if (selectItemValue == null) {
          selectItems.add(
              new SelectItem(
                  uiSelectItem.getItemValue(),
                  uiSelectItem.getItemLabel(),
                  uiSelectItem.getItemDescription(),
                  uiSelectItem.isItemDisabled(),
                  uiSelectItem.isItemEscaped(),
                  uiSelectItem.isNoSelectionOption()));
        } else {
          selectItems.add((SelectItem) selectItemValue);
        }

      } else if (child instanceof UISelectItems) {
        UISelectItems uiSelectItems = ((UISelectItems) child);
        Object value = uiSelectItems.getValue();

        if (value != null) {
          if (value instanceof SelectItem) {
            selectItems.add((SelectItem) value);
          } else if (value.getClass().isArray()) {
            for (int i = 0; i < Array.getLength(value); i++) {
              Object item = Array.get(value, i);

              if (item instanceof SelectItem) selectItems.add((SelectItem) item);
              else selectItems.add(createSelectItem(context, uiSelectItems, item));
            }
          } else if (value instanceof Map) {
            Map map = (Map) value;

            for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
              Object key = it.next();

              selectItems.add(
                  createSelectItem(context, uiSelectItems, String.valueOf(key), map.get(key)));
            }
          } else if (value instanceof Collection) {
            Collection collection = (Collection) value;

            for (Iterator it = collection.iterator(); it.hasNext(); ) {
              Object item = it.next();
              if (item instanceof SelectItem) selectItems.add((SelectItem) item);
              else selectItems.add(createSelectItem(context, uiSelectItems, item));
            }
          }
        }
      }
    }

    return selectItems;
  }
Example #4
0
 public static void shiftItemsLast(
     UISelectMany targetSelect, UISelectItems targetItems, UIEditableList hiddenTargetList) {
   String[] selected = (String[]) targetSelect.getSelectedValues();
   SelectItem[] all = (SelectItem[]) targetItems.getValue();
   if (selected == null) {
     // nothing to do
     return;
   }
   all = shiftLast(selected, all);
   targetItems.setValue(all);
   clearTargetList(hiddenTargetList);
   addToTargetList(hiddenTargetList, all);
 }
  // @SuppressWarnings("unchecked")
  protected void addSelectItems(UISelectItems uiItems, List<SelectItem> items) {

    boolean isRendered = uiItems.isRendered();
    if (!isRendered) {
      items.add(null);
      return;
    }

    Object value = uiItems.getValue();
    if (value instanceof SelectItem) {
      items.add((SelectItem) value);
    } else if (value instanceof Object[]) {
      Object[] array = (Object[]) value;
      for (Object array1 : array) {
        // TODO test - this section is untested
        if (array1 instanceof SelectItemGroup) {
          resolveAndAddItems((SelectItemGroup) array1, items);
        } else {
          items.add((SelectItem) array1);
        }
      }
    } else if (value instanceof Collection) {
      Iterator<Object> iter = ((Collection<Object>) value).iterator();
      Object item;
      while (iter.hasNext()) {
        item = iter.next();
        if (item instanceof SelectItemGroup) {
          resolveAndAddItems((SelectItemGroup) item, items);
        } else {
          items.add(new SelectItem(item));
        }
      }
    } else if (value instanceof Map) {
      for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
        Object label = entry.getKey();
        SelectItem item =
            new SelectItem(entry.getValue(), label == null ? (String) null : label.toString());
        // TODO test - this section is untested
        if (item instanceof SelectItemGroup) {
          resolveAndAddItems((SelectItemGroup) item, items);
        } else {
          items.add(item);
        }
      }
    }
  }