Beispiel #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);
    }
  }
  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);
      }
    }
  }
  protected void encodeOption(
      FacesContext context,
      SelectCheckboxMenu menu,
      Object values,
      Object submittedValues,
      Converter converter,
      SelectItem option,
      int idx)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String itemValueAsString = getOptionAsString(context, menu, converter, option.getValue());
    String name = menu.getClientId(context);
    String id = name + UINamingContainer.getSeparatorChar(context) + idx;
    boolean disabled = option.isDisabled() || menu.isDisabled();

    Object valuesArray;
    Object itemValue;
    if (submittedValues != null) {
      valuesArray = submittedValues;
      itemValue = itemValueAsString;
    } else {
      valuesArray = values;
      itemValue = option.getValue();
    }

    boolean checked = isSelected(context, menu, itemValue, valuesArray, converter);
    if (option.isNoSelectionOption() && values != null && !checked) {
      return;
    }

    // input
    writer.startElement("input", null);
    writer.writeAttribute("id", id, null);
    writer.writeAttribute("name", name, null);
    writer.writeAttribute("type", "checkbox", null);
    writer.writeAttribute("value", itemValueAsString, null);

    if (checked) writer.writeAttribute("checked", "checked", null);
    if (disabled) writer.writeAttribute("disabled", "disabled", null);
    if (menu.getOnchange() != null) writer.writeAttribute("onchange", menu.getOnchange(), null);

    writer.endElement("input");

    // label
    writer.startElement("label", null);
    writer.writeAttribute("for", id, null);
    if (disabled) writer.writeAttribute("class", "ui-state-disabled", null);

    if (option.isEscape()) writer.writeText(option.getLabel(), null);
    else writer.write(option.getLabel());

    writer.endElement("label");
  }
  protected void encodeOption(
      FacesContext context,
      SelectOneListbox listbox,
      SelectItem option,
      Object values,
      Object submittedValues,
      Converter converter)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String itemValueAsString = getOptionAsString(context, listbox, converter, option.getValue());
    boolean disabled = option.isDisabled() || listbox.isDisabled();

    Object valuesArray;
    Object itemValue;
    if (submittedValues != null) {
      valuesArray = submittedValues;
      itemValue = itemValueAsString;
    } else {
      valuesArray = values;
      itemValue = option.getValue();
    }

    boolean selected = isSelected(context, listbox, itemValue, valuesArray, converter);
    if (option.isNoSelectionOption() && values != null && !selected) {
      return;
    }

    writer.startElement("option", null);
    writer.writeAttribute("value", itemValueAsString, null);
    if (disabled) writer.writeAttribute("disabled", "disabled", null);
    if (selected) writer.writeAttribute("selected", "selected", null);

    if (option.isEscape()) {
      writer.writeText(option.getLabel(), null);
    } else {
      writer.write(option.getLabel());
    }

    writer.endElement("option");
  }
  protected void encodeItem(
      FacesContext context,
      SelectOneListbox listbox,
      SelectItem option,
      Object values,
      Object submittedValues,
      Converter converter,
      boolean customContent)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    String itemValueAsString = getOptionAsString(context, listbox, converter, option.getValue());
    boolean disabled = option.isDisabled() || listbox.isDisabled();
    String itemClass =
        disabled ? SelectOneListbox.ITEM_CLASS + " ui-state-disabled" : SelectOneListbox.ITEM_CLASS;

    Object valuesArray;
    Object itemValue;
    if (submittedValues != null) {
      valuesArray = submittedValues;
      itemValue = itemValueAsString;
    } else {
      valuesArray = values;
      itemValue = option.getValue();
    }

    boolean selected = isSelected(context, listbox, itemValue, valuesArray, converter);
    if (option.isNoSelectionOption() && values != null && !selected) {
      return;
    }

    if (selected) {
      itemClass = itemClass + " ui-state-highlight";
    }

    if (customContent) {
      String var = listbox.getVar();
      context.getExternalContext().getRequestMap().put(var, option.getValue());

      writer.startElement("tr", null);
      writer.writeAttribute("class", itemClass, null);
      if (option.getDescription() != null) {
        writer.writeAttribute("title", option.getDescription(), null);
      }

      for (UIComponent child : listbox.getChildren()) {
        if (child instanceof Column && child.isRendered()) {
          writer.startElement("td", null);
          renderChildren(context, child);
          writer.endElement("td");
        }
      }

      writer.endElement("tr");
    } else {
      writer.startElement("li", null);
      writer.writeAttribute("class", itemClass, null);
      if (option.isEscape()) {
        writer.writeText(option.getLabel(), null);
      } else {
        writer.write(option.getLabel());
      }

      writer.endElement("li");
    }
  }