/**
   * 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();
      }
    }
  }
Esempio n. 2
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;
  }
Esempio n. 3
0
 protected void addSelectItem(UISelectItem uiItem, List<SelectItem> items) {
   boolean isRendered = uiItem.isRendered();
   if (!isRendered) {
     items.add(null);
     return;
   }
   Object value = uiItem.getValue();
   SelectItem item;
   if (value instanceof SelectItem) {
     item = (SelectItem) value;
   } else {
     Object itemValue = uiItem.getItemValue();
     String itemLabel = uiItem.getItemLabel();
     // JSF throws a null pointer exception for null values and labels,
     // which is a serious problem at design-time.
     item =
         new SelectItem(
             itemValue,
             itemLabel == null ? "" : itemLabel,
             uiItem.getItemDescription(),
             uiItem.isItemDisabled());
   }
   items.add(item);
 }
Esempio n. 4
0
  /**
   * Return a List of {@link javax.faces.model.SelectItem} instances representing the available
   * options for this component, assembled from the set of {@link
   * javax.faces.component.UISelectItem} and/or {@link javax.faces.component.UISelectItems}
   * components that are direct children of this component. If there are no such children, an empty
   * <code>Iterator</code> is returned.
   *
   * @param context The {@link javax.faces.context.FacesContext} for the current request. If null,
   *     the UISelectItems behavior will not work.
   * @param component the component
   * @throws IllegalArgumentException if <code>context</code> is <code>null</code>
   * @return a List of the select items for the specified component
   */
  public static List<SelectItem> getSelectItems(FacesContext context, UIComponent component) {

    if (context == null) {
      throw new IllegalArgumentException(
          MessageUtils.getExceptionMessageString(
              MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"));
    }

    ArrayList<SelectItem> list = new ArrayList<SelectItem>();
    for (UIComponent kid : component.getChildren()) {
      if (kid instanceof UISelectItem) {
        UISelectItem item = (UISelectItem) kid;
        Object value = item.getValue();

        if (value == null) {
          list.add(
              new SelectItem(
                  item.getItemValue(),
                  item.getItemLabel(),
                  item.getItemDescription(),
                  item.isItemDisabled(),
                  item.isItemEscaped()));
        } else if (value instanceof SelectItem) {
          list.add((SelectItem) value);
        } else {
          throw new IllegalArgumentException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
                  component.getId(),
                  value.getClass().getName()));
        }
      } else if (kid instanceof UISelectItems) {
        Object value = ((UISelectItems) kid).getValue();
        if (value instanceof SelectItem) {
          list.add((SelectItem) value);
        } else if (value instanceof SelectItem[]) {
          SelectItem[] items = (SelectItem[]) value;
          // we manually copy the elements so that the list is
          // modifiable.  Arrays.asList() returns a non-mutable
          // list.
          //noinspection ManualArrayToCollectionCopy
          for (SelectItem item : items) {
            list.add(item);
          }
        } else if (value instanceof Collection) {
          for (Object element : ((Collection) value)) {
            if (SelectItem.class.isInstance(element)) {
              list.add((SelectItem) element);
            } else {
              throw new IllegalArgumentException(
                  MessageUtils.getExceptionMessageString(
                      MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
                      component.getId(),
                      value.getClass().getName()));
            }
          }
        } else if (value instanceof Map) {
          Map optionMap = (Map) value;
          for (Object o : optionMap.entrySet()) {
            Entry entry = (Entry) o;
            Object key = entry.getKey();
            Object val = entry.getValue();
            if (key == null || val == null) {
              continue;
            }
            list.add(new SelectItem(val, key.toString()));
          }
        } else {
          throw new IllegalArgumentException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.CHILD_NOT_OF_EXPECTED_TYPE_ID,
                  "UISelectItem/UISelectItems",
                  component.getFamily(),
                  component.getId(),
                  value != null ? value.getClass().getName() : "null"));
        }
      }
    }
    return (list);
  }