Search in sources :

Example 1 with SelectItemGroup

use of jakarta.faces.model.SelectItemGroup in project myfaces by apache.

the class SelectItemsUtil method isNoSelectionOption.

/**
 * @param context the faces context
 * @param uiComponent the component instance
 * @param value the value to check
 * @param converter
 * @param iterator contains instances of SelectItem
 * @return if the value is a SelectItem of selectItemsIter, on which noSelectionOption is true
 */
public static boolean isNoSelectionOption(FacesContext context, UIComponent uiComponent, Object value, Iterator<SelectItem> iterator, Converter converter) {
    while (iterator.hasNext()) {
        SelectItem item = iterator.next();
        if (item instanceof SelectItemGroup) {
            SelectItemGroup itemgroup = (SelectItemGroup) item;
            SelectItem[] selectItems = itemgroup.getSelectItems();
            if (selectItems != null && selectItems.length > 0 && isNoSelectionOption(context, uiComponent, value, Arrays.asList(selectItems).iterator(), converter)) {
                return true;
            }
        } else if (item.isNoSelectionOption()) {
            Object itemValue = _convertOrCoerceValue(context, uiComponent, value, item, converter);
            if (value == itemValue || value.equals(itemValue)) {
                return true;
            }
        }
    }
    return false;
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup)

Example 2 with SelectItemGroup

use of jakarta.faces.model.SelectItemGroup in project myfaces by apache.

the class SelectItemsUtil method matchValue.

/**
 * @param context the faces context
 * @param uiComponent the component instance
 * @param value the value to check
 * @param converter a converter instance
 * @param iterator contains instances of SelectItem
 * @return if the value of a selectitem is equal to the given value
 */
public static boolean matchValue(FacesContext context, UIComponent uiComponent, Object value, Iterator<SelectItem> iterator, Converter converter) {
    while (iterator.hasNext()) {
        SelectItem item = iterator.next();
        if (item instanceof SelectItemGroup) {
            SelectItemGroup itemgroup = (SelectItemGroup) item;
            SelectItem[] selectItems = itemgroup.getSelectItems();
            if (selectItems != null && selectItems.length > 0 && matchValue(context, uiComponent, value, Arrays.asList(selectItems).iterator(), converter)) {
                return true;
            }
        } else {
            Object itemValue = _convertOrCoerceValue(context, uiComponent, value, item, converter);
            if (value == itemValue || value.equals(itemValue)) {
                return true;
            }
        }
    }
    return false;
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup)

Example 3 with SelectItemGroup

use of jakarta.faces.model.SelectItemGroup in project myfaces by apache.

the class HtmlRendererUtils method renderSelectOptions.

/**
 * Renders the select options for a <code>UIComponent</code> that is
 * rendered as an HTML select element.
 *
 * @param context        the current <code>FacesContext</code>.
 * @param component      the <code>UIComponent</code> whose options need to be
 *                       rendered.
 * @param converter      <code>component</code>'s converter
 * @param lookupSet      the <code>Set</code> to use to look up selected options
 * @param selectItemList the <code>List</code> of <code>SelectItem</code> s to be
 *                       rendered as HTML option elements.
 * @throws IOException
 */
public static void renderSelectOptions(FacesContext context, UIComponent component, Converter converter, Set lookupSet, List selectItemList) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    // check for the hideNoSelectionOption attribute
    boolean hideNoSelectionOption = isHideNoSelectionOption(component);
    boolean componentDisabled = isTrue(component.getAttributes().get("disabled"));
    for (int i = 0; i < selectItemList.size(); i++) {
        SelectItem selectItem = (SelectItem) selectItemList.get(i);
        if (selectItem instanceof SelectItemGroup) {
            // component);
            writer.startElement(HTML.OPTGROUP_ELEM, null);
            writer.writeAttribute(HTML.LABEL_ATTR, selectItem.getLabel(), null);
            SelectItem[] selectItems = ((SelectItemGroup) selectItem).getSelectItems();
            renderSelectOptions(context, component, converter, lookupSet, Arrays.asList(selectItems));
            writer.endElement(HTML.OPTGROUP_ELEM);
        } else {
            String itemStrValue = RendererUtils.getConvertedStringValue(context, component, converter, selectItem);
            boolean selected = lookupSet.contains(itemStrValue);
            // (if there is currently no value on UISelectOne, lookupSet contains "")
            if (hideNoSelectionOption && selectItem.isNoSelectionOption() && !lookupSet.isEmpty() && !(lookupSet.size() == 1 && lookupSet.contains("")) && !selected) {
                // do not render this selectItem
                continue;
            }
            writer.write(TABULATOR);
            // component);
            writer.startElement(HTML.OPTION_ELEM, null);
            if (itemStrValue != null) {
                writer.writeAttribute(HTML.VALUE_ATTR, itemStrValue, null);
            } else {
                writer.writeAttribute(HTML.VALUE_ATTR, "", null);
            }
            if (selected) {
                writer.writeAttribute(HTML.SELECTED_ATTR, HTML.SELECTED_ATTR, null);
            }
            boolean disabled = selectItem.isDisabled();
            if (disabled) {
                writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
            }
            String labelClass = null;
            if (componentDisabled || disabled) {
                labelClass = (String) component.getAttributes().get(ComponentAttrs.DISABLED_CLASS_ATTR);
            } else {
                labelClass = (String) component.getAttributes().get(ComponentAttrs.ENABLED_CLASS_ATTR);
            }
            if (labelClass != null) {
                writer.writeAttribute("class", labelClass, "labelClass");
            }
            boolean escape = AttributeUtils.getBooleanAttribute(component, ComponentAttrs.ESCAPE_ATTR, false);
            // check if isEscape() = true first.
            if (escape || selectItem.isEscape()) {
                writer.writeText(selectItem.getLabel(), null);
            } else {
                writer.write(selectItem.getLabel());
            }
            writer.endElement(HTML.OPTION_ELEM);
        }
    }
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup)

Example 4 with SelectItemGroup

use of jakarta.faces.model.SelectItemGroup in project myfaces-tobago by apache.

the class SelectManyRendererBase method getSelectItemsValueConverter.

/**
 * Iterates through the SelectItems with the given Iterator and tries to obtain
 * a by-class-converter based on the Class of SelectItem.getValue().
 *
 * @param iterator
 * @param facesContext
 * @return The first suitable Converter for the given SelectItems or null.
 */
static Converter getSelectItemsValueConverter(final Iterator<SelectItem> iterator, final FacesContext facesContext) {
    // Attention!
    // This code is duplicated in jsfapi component package.
    // If you change something here please do the same in the other class!
    Converter converter = null;
    while (converter == null && iterator.hasNext()) {
        final SelectItem item = iterator.next();
        if (item instanceof SelectItemGroup) {
            final Iterator<SelectItem> groupIterator = Arrays.asList(((SelectItemGroup) item).getSelectItems()).iterator();
            converter = getSelectItemsValueConverter(groupIterator, facesContext);
        } else {
            final Class<?> selectItemsType = item.getValue().getClass();
            // optimization: no conversion for String values
            if (String.class.equals(selectItemsType)) {
                return null;
            }
            try {
                converter = facesContext.getApplication().createConverter(selectItemsType);
            } catch (final FacesException e) {
            // nothing - try again
            }
        }
    }
    return converter;
}
Also used : UISelectItem(jakarta.faces.component.UISelectItem) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup) Converter(jakarta.faces.convert.Converter) FacesException(jakarta.faces.FacesException)

Example 5 with SelectItemGroup

use of jakarta.faces.model.SelectItemGroup in project mojarra by eclipse-ee4j.

the class SelectManyCheckboxListRenderer method encodeEnd.

// ---------------------------------------------------------- Public Methods
@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    rendererParamsNotNull(context, component);
    if (!shouldEncode(component)) {
        return;
    }
    ResponseWriter writer = context.getResponseWriter();
    assert writer != null;
    String alignStr;
    Object borderObj;
    Boolean newTableRow = false;
    int border = 0;
    if (null != (alignStr = (String) component.getAttributes().get("layout"))) {
        if (alignStr.equalsIgnoreCase("list")) {
            newTableRow = null;
        } else {
            newTableRow = alignStr.equalsIgnoreCase("pageDirection");
        }
    }
    if (null != (borderObj = component.getAttributes().get("border"))) {
        border = (Integer) borderObj;
    }
    Converter converter = null;
    if (component instanceof ValueHolder) {
        converter = ((ValueHolder) component).getConverter();
    }
    renderBeginText(component, border, newTableRow, context, true);
    Iterator<SelectItem> items = RenderKitUtils.getSelectItems(context, component);
    Object currentSelections = getCurrentSelectedValues(component);
    Object[] submittedValues = getSubmittedSelectedValues(component);
    OptionComponentInfo optionInfo = new OptionComponentInfo(component);
    int idx = -1;
    while (items.hasNext()) {
        SelectItem curItem = items.next();
        idx++;
        // table.
        if (curItem instanceof SelectItemGroup) {
            // write out the label for the group.
            if (curItem.getLabel() != null) {
                if (newTableRow == TRUE) {
                    writer.startElement("tr", component);
                }
                writer.startElement(newTableRow != null ? "td" : "li", component);
                writer.writeText(curItem.getLabel(), component, "label");
                if (newTableRow != null) {
                    writer.endElement("td");
                    if (newTableRow) {
                        writer.endElement("tr");
                    }
                }
            }
            if (newTableRow != null) {
                if (newTableRow) {
                    writer.startElement("tr", component);
                }
                writer.startElement("td", component);
            }
            writer.writeText("\n", component, null);
            renderBeginText(component, 0, newTableRow, context, false);
            // render options of this group.
            SelectItem[] itemsArray = ((SelectItemGroup) curItem).getSelectItems();
            for (SelectItem element : itemsArray) {
                renderOption(context, component, converter, element, currentSelections, submittedValues, newTableRow, idx++, optionInfo);
            }
            renderEndText(component, newTableRow, context);
            writer.endElement(newTableRow != null ? "td" : "li");
            if (newTableRow == TRUE) {
                writer.endElement("tr");
                writer.writeText("\n", component, null);
            }
        } else {
            renderOption(context, component, converter, curItem, currentSelections, submittedValues, newTableRow, idx, optionInfo);
        }
    }
    renderEndText(component, newTableRow, context);
}
Also used : SelectItemGroup(jakarta.faces.model.SelectItemGroup) ValueHolder(jakarta.faces.component.ValueHolder) ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItem(jakarta.faces.model.SelectItem) Converter(jakarta.faces.convert.Converter)

Aggregations

SelectItemGroup (jakarta.faces.model.SelectItemGroup)23 SelectItem (jakarta.faces.model.SelectItem)17 ResponseWriter (jakarta.faces.context.ResponseWriter)7 PrintWriter (java.io.PrintWriter)5 ArrayList (java.util.ArrayList)4 UISelectItem (jakarta.faces.component.UISelectItem)3 Converter (jakarta.faces.convert.Converter)3 ServletException (jakarta.servlet.ServletException)3 IOException (java.io.IOException)3 FacesException (jakarta.faces.FacesException)2 ValueHolder (jakarta.faces.component.ValueHolder)2 List (java.util.List)2 SelectItemsIterator (com.sun.faces.renderkit.SelectItemsIterator)1 UIComponent (jakarta.faces.component.UIComponent)1 UISelectItems (jakarta.faces.component.UISelectItems)1 UISelectMany (jakarta.faces.component.UISelectMany)1 UISelectOne (jakarta.faces.component.UISelectOne)1 FacesContext (jakarta.faces.context.FacesContext)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1