Ejemplo n.º 1
0
 public static Converter getSelectItemConverter(
     Application facesApplication, Iterator<SelectItem> selectItems) {
   Converter converter = null;
   while (selectItems.hasNext() && converter == null) {
     SelectItem selectItem = selectItems.next();
     if (selectItem instanceof SelectItemGroup) {
       SelectItemGroup selectItemGroup = (SelectItemGroup) selectItem;
       Iterator<SelectItem> groupSelectItems =
           Iterators.forArray(selectItemGroup.getSelectItems());
       // Recursively get the converter from the SelectItems of the SelectItemGroup
       converter = getSelectItemConverter(facesApplication, groupSelectItems);
     } else {
       Class<?> selectItemClass = selectItem.getValue().getClass();
       if (String.class.equals(selectItemClass)) {
         return null; // No converter required for strings
       }
       try {
         converter =
             facesApplication.createConverter(
                 selectItemClass); // Lookup the converter registered for the class
       } catch (FacesException exception) {
         // Converter cannot be created
       }
     }
   }
   return converter;
 }
  @SuppressWarnings("unchecked")
  private SelectItem[] createSelectItems(Object value) {
    List<SelectItem> items = new ArrayList<SelectItem>();

    if (value instanceof SelectItem[]) {
      return (SelectItem[]) value;
    } else if (value instanceof Collection) {
      Collection<Object> col = null;

      try {
        col = (Collection<Object>) value;
      } catch (ClassCastException cce) {
        throw new FacesException(cce);
      }

      for (Object item : col) {
        if (item instanceof SelectItemGroup) {
          SelectItemGroup itemGroup = (SelectItemGroup) item;
          SelectItem[] itemsFromGroup = itemGroup.getSelectItems();
          for (int i = 0; i < itemsFromGroup.length; i++) {
            items.add(itemsFromGroup[i]);
          }
        } else {
          FacesContext.getCurrentInstance()
              .getExternalContext()
              .getRequestMap()
              .put(this.getVar(), item);
          SelectItem selectItem = this.createSelectItem();
          FacesContext.getCurrentInstance()
              .getExternalContext()
              .getRequestMap()
              .remove(this.getVar());
          items.add(selectItem);
        }
      }
    } else if (value instanceof Map) {
      Map<String, Object> map = null;

      try {
        map = (Map<String, Object>) value;
      } catch (ClassCastException cce) {
        throw new FacesException(cce);
      }

      for (Map.Entry<String, Object> item : map.entrySet()) {
        FacesContext.getCurrentInstance()
            .getExternalContext()
            .getRequestMap()
            .put(this.getVar(), item.getValue());
        SelectItem selectItem = this.createSelectItem();
        FacesContext.getCurrentInstance()
            .getExternalContext()
            .getRequestMap()
            .remove(this.getVar());
        items.add(selectItem);
      }
    }

    return items.toArray(new SelectItem[0]);
  }
 private int getSize(UIComponent component) {
   int size = UIComponentUtil.getPrimitiveIntAttribute(component, JsfConstants.SIZE_ATTR);
   if (0 < size) {
     return size;
   }
   size = 0;
   for (Iterator it = new SelectItemsIterator(component); it.hasNext(); ) {
     SelectItem item = (SelectItem) it.next();
     if (item instanceof SelectItemGroup) {
       SelectItemGroup itemGroup = (SelectItemGroup) item;
       size += itemGroup.getSelectItems().length;
     }
     size++;
   }
   return size;
 }
Ejemplo n.º 4
0
 // Create an options list with nested groups
 protected List setupOptions() {
   SelectItemGroup group, subgroup;
   subgroup = new SelectItemGroup("Group C");
   subgroup.setSelectItems(
       new SelectItem[] {new SelectItem("C1"), new SelectItem("C2"), new SelectItem("C3")});
   List options = new ArrayList();
   options.add(new SelectItem("A1"));
   group = new SelectItemGroup("Group B");
   group.setSelectItems(
       new SelectItem[] {
         new SelectItem("B1"), subgroup, new SelectItem("B2"), new SelectItem("B3")
       });
   options.add(group);
   options.add(new SelectItem("A2"));
   options.add(new SelectItem("A3"));
   return (options);
 }
Ejemplo n.º 5
0
 protected void resolveAndAddItems(SelectItemGroup group, List<SelectItem> items) {
   for (SelectItem item : group.getSelectItems()) {
     if (item instanceof SelectItemGroup) {
       resolveAndAddItems((SelectItemGroup) item, items);
     } else {
       items.add(item);
     }
   }
 }
  protected void renderSelectItems(
      FacesContext context,
      UIComponent component,
      ResponseWriter writer,
      Iterator it,
      String[] selectedValues)
      throws IOException {

    while (it.hasNext()) {
      final SelectItem selectItem = (SelectItem) it.next();

      if (selectItem instanceof SelectItemGroup) {
        SelectItemGroup selectItemGroup = (SelectItemGroup) selectItem;
        SelectItem[] selectItems = selectItemGroup.getSelectItems();
        Iterator selectItemsIt = new ArrayIterator(selectItems);
        writer.startElement(JsfConstants.OPTGROUP_ELEM, component);
        RendererUtil.renderAttribute(writer, JsfConstants.LABEL_ATTR, selectItemGroup.getLabel());
        // TODO case: optgroup is disabled
        renderSelectItems(context, component, writer, selectItemsIt, selectedValues);
        writer.endElement(JsfConstants.OPTGROUP_ELEM);
      } else {
        writer.startElement(JsfConstants.OPTION_ELEM, component);
        final Object value = selectItem.getValue();
        RendererUtil.renderAttribute(writer, JsfConstants.VALUE_ATTR, value);

        final boolean disabled = UIComponentUtil.isDisabled(component) || selectItem.isDisabled();
        final String labelClass = getLabelStyleClass(component, disabled);
        if (labelClass != null) {
          RendererUtil.renderAttribute(writer, JsfConstants.CLASS_ATTR, labelClass);
        }
        if (value != null && isSelected(selectedValues, value.toString())) {
          renderSelectedAttribute(writer);
        }
        if (selectItem.isDisabled()) {
          renderDisabledAttribute(writer);
        }
        writer.writeText(selectItem.getLabel(), null);
        writer.endElement(JsfConstants.OPTION_ELEM);
      }
    }
  }