Search in sources :

Example 11 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlRadioRendererBase method encodeEnd.

@Override
public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    RendererUtils.checkParamValidity(facesContext, uiComponent, UISelectOne.class);
    UISelectOne selectOne = (UISelectOne) uiComponent;
    String layout = getLayout(selectOne);
    // default to LINE_DIRECTION
    Boolean usingTable = Boolean.FALSE;
    if (layout != null) {
        if (layout.equals(PAGE_DIRECTION)) {
            usingTable = Boolean.TRUE;
        } else if (layout.equals(LINE_DIRECTION)) {
            usingTable = Boolean.FALSE;
        } else if (layout.equals(LAYOUT_LIST)) {
            usingTable = null;
        } else {
            log.severe("Wrong layout '" + layout + "' defined for component " + ComponentUtils.getPathToComponent(selectOne));
        }
    }
    ResponseWriter writer = facesContext.getResponseWriter();
    Map<String, List<ClientBehavior>> behaviors = null;
    if (uiComponent instanceof ClientBehaviorHolder) {
        behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
        if (!behaviors.isEmpty()) {
            ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
        }
    }
    String group = selectOne instanceof HtmlSelectOneRadio ? ((HtmlSelectOneRadio) selectOne).getGroup() : null;
    if (group != null && !group.isEmpty()) {
        if (!groupFirst.containsKey(group)) {
            groupFirst.put(group, selectOne);
        }
        List selectItemList = RendererUtils.getSelectItemList(selectOne, facesContext);
        if (selectItemList != null && !selectItemList.isEmpty()) {
            Converter converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOne);
            Object currentValue = null;
            // must contain a value expression.
            if (selectOne.getValueExpression("value") != null) {
                currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(facesContext, selectOne);
            } else {
                currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(facesContext, groupFirst.get(group));
            }
            SelectItem selectItem = (SelectItem) selectItemList.get(0);
            renderGroupOrItemRadio(facesContext, selectOne, selectItem, currentValue, converter, usingTable, group, 0);
        } else {
            // Deferred case: find real component with attached selectItems
            UIForm form = ComponentUtils.findClosest(UIForm.class, uiComponent);
            GetSelectItemListCallback callback = new GetSelectItemListCallback(selectOne, group);
            form.visitTree(VisitContext.createVisitContext(facesContext, null, FIND_SELECT_LIST_HINTS), callback);
            renderGroupOrItemRadio(facesContext, selectOne, callback.getSelectItem(), callback.getCurrentValue(), callback.getConverter(), usingTable, group, callback.getIndex());
        }
    } else {
        // Render as single component
        writer.startElement(usingTable != null ? HTML.TABLE_ELEM : HTML.UL_ELEM, selectOne);
        HtmlRendererUtils.renderHTMLAttributes(writer, selectOne, HTML.SELECT_TABLE_PASSTHROUGH_ATTRIBUTES);
        if (behaviors != null && !behaviors.isEmpty()) {
            writer.writeAttribute(HTML.ID_ATTR, selectOne.getClientId(facesContext), null);
        } else {
            HtmlRendererUtils.writeIdIfNecessary(writer, selectOne, facesContext);
        }
        if (usingTable == Boolean.FALSE) {
            // selectOne);
            writer.startElement(HTML.TR_ELEM, null);
        }
        List selectItemList = RendererUtils.getSelectItemList(selectOne, facesContext);
        Converter converter = HtmlRendererUtils.findUIOutputConverterFailSafe(facesContext, selectOne);
        Object currentValue = RendererUtils.getStringFromSubmittedValueOrLocalValueReturnNull(facesContext, selectOne);
        int itemNum = 0;
        for (int i = 0; i < selectItemList.size(); i++) {
            SelectItem selectItem = (SelectItem) selectItemList.get(i);
            itemNum = renderGroupOrItemRadio(facesContext, selectOne, selectItem, currentValue, converter, usingTable, itemNum);
        }
        if (usingTable == Boolean.FALSE) {
            writer.endElement(HTML.TR_ELEM);
        }
        writer.endElement(usingTable != null ? HTML.TABLE_ELEM : HTML.UL_ELEM);
    }
}
Also used : UIForm(jakarta.faces.component.UIForm) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) HtmlSelectOneRadio(jakarta.faces.component.html.HtmlSelectOneRadio) VisitHint(jakarta.faces.component.visit.VisitHint) ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItem(jakarta.faces.model.SelectItem) Converter(jakarta.faces.convert.Converter) ArrayList(java.util.ArrayList) List(java.util.List) UISelectOne(jakarta.faces.component.UISelectOne)

Example 12 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlRadioRendererBase method renderGroupOrItemRadio.

/**
 * Renders the given SelectItem(Group)
 * @return the itemNum for the next item
 */
protected int renderGroupOrItemRadio(FacesContext facesContext, UIComponent uiComponent, SelectItem selectItem, Object currentValue, Converter converter, Boolean usingTable, String group, Integer itemNum) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    boolean isSelectItemGroup = (selectItem instanceof SelectItemGroup);
    // TODO : Check here for getSubmittedValue. Look at RendererUtils.getValue
    // this is useless object creation
    // Object itemValue = selectItem.getValue();
    UISelectOne selectOne = (UISelectOne) uiComponent;
    if (isSelectItemGroup) {
        if (usingTable == Boolean.TRUE) {
            // selectOne);
            writer.startElement(HTML.TR_ELEM, null);
        }
        // selectOne);
        writer.startElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM, null);
        if (selectItem.isEscape()) {
            writer.writeText(selectItem.getLabel(), HTML.LABEL_ATTR);
        } else {
            writer.write(selectItem.getLabel());
        }
        writer.endElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM);
        if (usingTable == Boolean.TRUE) {
            writer.endElement(HTML.TR_ELEM);
            // selectOne);
            writer.startElement(HTML.TR_ELEM, null);
        }
        // selectOne);
        writer.startElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM, null);
        // selectOne);
        writer.startElement(usingTable != null ? HTML.TABLE_ELEM : HTML.UL_ELEM, null);
        if (usingTable != null) {
            Integer border = 0;
            Object borderObj = uiComponent.getAttributes().get("border");
            if (null != borderObj) {
                border = (Integer) borderObj;
            }
            if (Integer.MIN_VALUE != border) {
                writer.writeAttribute(HTML.BORDER_ATTR, border, "border");
            }
        }
        if (usingTable == Boolean.FALSE) {
            // selectOne);
            writer.startElement(HTML.TR_ELEM, null);
        }
        SelectItemGroup selectItemGroup = (SelectItemGroup) selectItem;
        SelectItem[] selectItems = selectItemGroup.getSelectItems();
        for (SelectItem groupSelectItem : selectItems) {
            itemNum = renderGroupOrItemRadio(facesContext, selectOne, groupSelectItem, currentValue, converter, usingTable, itemNum);
        }
        if (usingTable == Boolean.FALSE) {
            writer.endElement(HTML.TR_ELEM);
        }
        writer.endElement(usingTable != null ? HTML.TABLE_ELEM : HTML.UL_ELEM);
        writer.endElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM);
        if (usingTable == Boolean.TRUE) {
            writer.endElement(HTML.TR_ELEM);
        }
    } else {
        String itemStrValue = RendererUtils.getConvertedStringValue(facesContext, selectOne, converter, selectItem.getValue());
        boolean itemChecked = (itemStrValue == null) ? itemStrValue == currentValue : "".equals(itemStrValue) ? (currentValue == null || itemStrValue.equals(currentValue)) : itemStrValue.equals(currentValue);
        // AND this item (the "no selection option") is not selected
        if (HtmlRendererUtils.isHideNoSelectionOption(uiComponent) && selectItem.isNoSelectionOption() && currentValue != null && !"".equals(currentValue) && !itemChecked) {
            // do not render this selectItem
            return itemNum;
        }
        // writer.write("\t\t");
        boolean renderGroupId = false;
        if (group != null && !group.isEmpty()) {
            // no op
            renderGroupId = true;
        } else {
            if (usingTable == Boolean.TRUE) {
                // selectOne);
                writer.startElement(HTML.TR_ELEM, null);
            }
            // selectOne);
            writer.startElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM, null);
        }
        boolean itemDisabled = selectItem.isDisabled();
        String itemId = renderRadio(facesContext, selectOne, itemStrValue, itemDisabled, itemChecked, renderGroupId, renderGroupId ? null : itemNum);
        // label element after the input
        boolean componentDisabled = isDisabled(facesContext, selectOne);
        boolean disabled = (componentDisabled || itemDisabled);
        HtmlRendererUtils.renderLabel(writer, selectOne, renderGroupId ? selectOne.getClientId(facesContext) : itemId, selectItem, disabled);
        if (group != null && group.length() > 0) {
        // no op
        } else {
            writer.endElement(usingTable != null ? HTML.TD_ELEM : HTML.LI_ELEM);
            if (usingTable == Boolean.TRUE) {
                writer.endElement(HTML.TR_ELEM);
            }
        }
        // we rendered one radio --> increment itemNum
        itemNum++;
    }
    return itemNum;
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItem(jakarta.faces.model.SelectItem) SelectItemGroup(jakarta.faces.model.SelectItemGroup) UISelectOne(jakarta.faces.component.UISelectOne)

Example 13 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlSelectableRendererBase method internalRenderSelect.

protected void internalRenderSelect(FacesContext facesContext, UIComponent uiComponent, boolean disabled, int size, boolean selectMany, Converter converter) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    writer.startElement(HTML.SELECT_ELEM, uiComponent);
    if (uiComponent instanceof ClientBehaviorHolder && !((ClientBehaviorHolder) uiComponent).getClientBehaviors().isEmpty()) {
        writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
    } else {
        HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
    }
    writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
    List<SelectItemInfo> selectItemList;
    if (selectMany) {
        writer.writeAttribute(HTML.MULTIPLE_ATTR, HTML.MULTIPLE_ATTR, null);
        selectItemList = SelectItemsUtils.getSelectItemInfoList((UISelectMany) uiComponent, facesContext);
    } else {
        selectItemList = SelectItemsUtils.getSelectItemInfoList((UISelectOne) uiComponent, facesContext);
    }
    if (size == Integer.MIN_VALUE) {
        // No size given (Listbox) --> size is number of select items
        writer.writeAttribute(HTML.SIZE_ATTR, Integer.toString(selectItemList.size()), null);
    } else {
        writer.writeAttribute(HTML.SIZE_ATTR, Integer.toString(size), null);
    }
    Map<String, List<ClientBehavior>> behaviors = null;
    if (uiComponent instanceof ClientBehaviorHolder) {
        behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
        long commonPropertiesMarked = 0L;
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            commonPropertiesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent);
        }
        if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext)) {
            CommonHtmlAttributesUtil.renderChangeEventProperty(writer, commonPropertiesMarked, uiComponent);
            CommonHtmlAttributesUtil.renderEventProperties(writer, commonPropertiesMarked, uiComponent);
            // Note that on JSF 2.3, selectable components don't need onselect attribute
            // Please see https://issues.apache.org/jira/browse/MYFACES-4190
            CommonHtmlAttributesUtil.renderFieldEventPropertiesWithoutOnchangeAndOnselect(writer, commonPropertiesMarked, uiComponent);
        } else {
            HtmlRendererUtils.renderBehaviorizedOnchangeEventHandler(facesContext, writer, uiComponent, behaviors);
            if (isCommonEventsOptimizationEnabled(facesContext)) {
                Long commonEventsMarked = CommonHtmlEventsUtil.getMarkedEvents(uiComponent);
                CommonHtmlEventsUtil.renderBehaviorizedEventHandlers(facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
                CommonHtmlEventsUtil.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, behaviors);
            } else {
                HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
                HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, uiComponent, behaviors);
            }
        }
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            CommonHtmlAttributesUtil.renderSelectPassthroughPropertiesWithoutDisabledAndEvents(writer, CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent), uiComponent);
        } else {
            HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
        }
    } else {
        if (isCommonPropertiesOptimizationEnabled(facesContext)) {
            CommonHtmlAttributesUtil.renderSelectPassthroughPropertiesWithoutDisabled(writer, CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent), uiComponent);
        } else {
            HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
        }
    }
    if (disabled) {
        writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
    }
    if (HtmlRendererUtils.isReadOnly(uiComponent)) {
        writer.writeAttribute(HTML.READONLY_ATTR, HTML.READONLY_ATTR, null);
    }
    Set lookupSet = HtmlRendererUtils.getSubmittedOrSelectedValuesAsSet(selectMany, uiComponent, facesContext, converter);
    SelectItemsUtils.renderSelectOptions(facesContext, uiComponent, converter, lookupSet, selectItemList);
    // bug #970747: force separate end tag
    writer.writeText(RendererUtils.EMPTY_STRING, null);
    writer.endElement(HTML.SELECT_ELEM);
}
Also used : UISelectMany(jakarta.faces.component.UISelectMany) Set(java.util.Set) ResponseWriter(jakarta.faces.context.ResponseWriter) SelectItemInfo(org.apache.myfaces.renderkit.html.util.SelectItemInfo) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) UISelectOne(jakarta.faces.component.UISelectOne)

Example 14 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlRendererUtils method internalRenderSelect.

private static void internalRenderSelect(FacesContext facesContext, UIComponent uiComponent, boolean disabled, int size, boolean selectMany, Converter converter) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    writer.startElement(HTML.SELECT_ELEM, uiComponent);
    if (uiComponent instanceof ClientBehaviorHolder && !((ClientBehaviorHolder) uiComponent).getClientBehaviors().isEmpty()) {
        writer.writeAttribute(HTML.ID_ATTR, uiComponent.getClientId(facesContext), null);
    } else {
        HtmlRendererUtils.writeIdIfNecessary(writer, uiComponent, facesContext);
    }
    writer.writeAttribute(HTML.NAME_ATTR, uiComponent.getClientId(facesContext), null);
    List selectItemList;
    if (selectMany) {
        writer.writeAttribute(HTML.MULTIPLE_ATTR, HTML.MULTIPLE_ATTR, null);
        selectItemList = RendererUtils.getSelectItemList((UISelectMany) uiComponent, facesContext);
    } else {
        selectItemList = RendererUtils.getSelectItemList((UISelectOne) uiComponent, facesContext);
    }
    if (size == Integer.MIN_VALUE) {
        // No size given (Listbox) --> size is number of select items
        writer.writeAttribute(HTML.SIZE_ATTR, Integer.toString(selectItemList.size()), null);
    } else {
        writer.writeAttribute(HTML.SIZE_ATTR, Integer.toString(size), null);
    }
    if (uiComponent instanceof ClientBehaviorHolder) {
        Map<String, List<ClientBehavior>> behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
        renderBehaviorizedOnchangeEventHandler(facesContext, writer, uiComponent, behaviors);
        renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, behaviors);
        renderBehaviorizedFieldEventHandlersWithoutOnchange(facesContext, writer, uiComponent, behaviors);
        renderHTMLAttributes(writer, uiComponent, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
    } else {
        renderHTMLAttributes(writer, uiComponent, HTML.SELECT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
    }
    if (disabled) {
        writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
    }
    if (isReadOnly(uiComponent)) {
        writer.writeAttribute(HTML.READONLY_ATTR, HTML.READONLY_ATTR, null);
    }
    Set lookupSet = getSubmittedOrSelectedValuesAsSet(selectMany, uiComponent, facesContext, converter);
    renderSelectOptions(facesContext, uiComponent, converter, lookupSet, selectItemList);
    // bug #970747: force separate end tag
    writer.writeText(RendererUtils.EMPTY_STRING, null);
    writer.endElement(HTML.SELECT_ELEM);
}
Also used : UISelectMany(jakarta.faces.component.UISelectMany) Set(java.util.Set) ResponseWriter(jakarta.faces.context.ResponseWriter) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) UISelectOne(jakarta.faces.component.UISelectOne)

Example 15 with UISelectOne

use of jakarta.faces.component.UISelectOne in project myfaces by apache.

the class HtmlRendererUtils method decodeUISelectOne.

/**
 * X-CHECKED: tlddoc h:selectManyListbox
 *
 * @param facesContext
 * @param component
 */
public static void decodeUISelectOne(FacesContext facesContext, UIComponent component) {
    if (!(component instanceof EditableValueHolder)) {
        throw new IllegalArgumentException("Component " + component.getClientId(facesContext) + " is not an EditableValueHolder");
    }
    if (isDisabledOrReadOnly(component)) {
        return;
    }
    Map paramMap = facesContext.getExternalContext().getRequestParameterMap();
    if (component instanceof UISelectOne) {
        String group = ((UISelectOne) component).getGroup();
        if (group != null && !group.isEmpty()) {
            UIForm form = ComponentUtils.findClosest(UIForm.class, component);
            String fullGroupId = form.getClientId(facesContext) + facesContext.getNamingContainerSeparatorChar() + group;
            if (paramMap.containsKey(fullGroupId)) {
                String submittedValue = (String) paramMap.get(fullGroupId);
                String submittedValueNamespace = component.getClientId(facesContext) + facesContext.getNamingContainerSeparatorChar();
                if (submittedValue.startsWith(submittedValueNamespace)) {
                    submittedValue = submittedValue.substring(submittedValueNamespace.length());
                    SelectOneGroupSetSubmittedValueCallback callback = new SelectOneGroupSetSubmittedValueCallback(group, submittedValue, component.getClientId(facesContext), component.getValueExpression("value") != null);
                    form.visitTree(VisitContext.createVisitContext(facesContext, null, MyFacesVisitHints.SET_SKIP_UNRENDERED), callback);
                }
            }
            return;
        }
    }
    String clientId = component.getClientId(facesContext);
    if (paramMap.containsKey(clientId)) {
        // request parameter found, set submitted value
        ((EditableValueHolder) component).setSubmittedValue(paramMap.get(clientId));
    } else {
        // see reason for this action at decodeUISelectMany
        ((EditableValueHolder) component).setSubmittedValue(RendererUtils.EMPTY_STRING);
    }
}
Also used : UIForm(jakarta.faces.component.UIForm) UISelectOne(jakarta.faces.component.UISelectOne) HashMap(java.util.HashMap) Map(java.util.Map) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Aggregations

UISelectOne (jakarta.faces.component.UISelectOne)22 UISelectMany (jakarta.faces.component.UISelectMany)6 ResponseWriter (jakarta.faces.context.ResponseWriter)6 Test (org.junit.Test)6 UIComponent (jakarta.faces.component.UIComponent)5 List (java.util.List)4 Set (java.util.Set)4 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)3 SelectItemsIterator (org.apache.myfaces.core.api.shared.SelectItemsIterator)3 ValueExpression (jakarta.el.ValueExpression)2 UIForm (jakarta.faces.component.UIForm)2 Converter (jakarta.faces.convert.Converter)2 SelectItem (jakarta.faces.model.SelectItem)2 ArrayList (java.util.ArrayList)2 MockValueExpression (org.apache.myfaces.test.el.MockValueExpression)2 EditableValueHolder (jakarta.faces.component.EditableValueHolder)1 UISelectBoolean (jakarta.faces.component.UISelectBoolean)1 UIViewRoot (jakarta.faces.component.UIViewRoot)1 HtmlSelectOneRadio (jakarta.faces.component.html.HtmlSelectOneRadio)1 VisitHint (jakarta.faces.component.visit.VisitHint)1