Search in sources :

Example 1 with HtmlSelectOneRadio

use of jakarta.faces.component.html.HtmlSelectOneRadio in project myfaces by apache.

the class HtmlRadioRendererTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    selectOneRadio = new HtmlSelectOneRadio();
    writer = new MockResponseWriter(new StringWriter(), null, null);
    facesContext.setResponseWriter(writer);
    facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
    facesContext.getRenderKit().addRenderer(selectOneRadio.getFamily(), selectOneRadio.getRendererType(), new HtmlRadioRenderer());
    facesContext.getAttributes().put("org.apache.myfaces.RENDERED_FACES_JS", Boolean.TRUE);
}
Also used : StringWriter(java.io.StringWriter) MockResponseWriter(org.apache.myfaces.test.mock.MockResponseWriter) HtmlSelectOneRadio(jakarta.faces.component.html.HtmlSelectOneRadio)

Example 2 with HtmlSelectOneRadio

use of jakarta.faces.component.html.HtmlSelectOneRadio in project myfaces by apache.

the class HtmlEscapeRendererTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    outputText = new HtmlOutputText();
    outputLabel = new HtmlOutputLabel();
    selectOneRadio = new HtmlSelectOneRadio();
    selectOneListbox = new HtmlSelectOneListbox();
    selectOneMenu = new HtmlSelectOneMenu();
    selectManyCheckbox = new HtmlSelectManyCheckbox();
    selectManyListbox = new HtmlSelectManyListbox();
    selectManyMenu = new HtmlSelectManyMenu();
    // TODO remove these two lines once myfaces-test goes alpha, see MYFACES-1155
    facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
    facesContext.getRenderKit().addRenderer(outputText.getFamily(), outputText.getRendererType(), new HtmlTextRenderer());
    facesContext.getRenderKit().addRenderer(outputLabel.getFamily(), outputLabel.getRendererType(), new HtmlLabelRenderer());
    facesContext.getRenderKit().addRenderer(selectOneRadio.getFamily(), selectOneRadio.getRendererType(), new HtmlRadioRenderer());
    facesContext.getRenderKit().addRenderer(selectOneListbox.getFamily(), selectOneListbox.getRendererType(), new HtmlListboxRenderer());
    facesContext.getRenderKit().addRenderer(selectOneMenu.getFamily(), selectOneMenu.getRendererType(), new HtmlMenuRenderer());
    facesContext.getRenderKit().addRenderer(selectManyCheckbox.getFamily(), selectManyCheckbox.getRendererType(), new HtmlCheckboxRenderer());
    facesContext.getRenderKit().addRenderer(selectManyListbox.getFamily(), selectManyListbox.getRendererType(), new HtmlListboxRenderer());
    facesContext.getRenderKit().addRenderer(selectManyMenu.getFamily(), selectManyMenu.getRendererType(), new HtmlMenuRenderer());
    facesContext.getAttributes().put("org.apache.myfaces.RENDERED_FACES_JS", Boolean.TRUE);
}
Also used : HtmlSelectOneListbox(jakarta.faces.component.html.HtmlSelectOneListbox) HtmlSelectOneMenu(jakarta.faces.component.html.HtmlSelectOneMenu) HtmlOutputLabel(jakarta.faces.component.html.HtmlOutputLabel) HtmlSelectOneRadio(jakarta.faces.component.html.HtmlSelectOneRadio) HtmlSelectManyMenu(jakarta.faces.component.html.HtmlSelectManyMenu) HtmlSelectManyListbox(jakarta.faces.component.html.HtmlSelectManyListbox) HtmlOutputText(jakarta.faces.component.html.HtmlOutputText) HtmlSelectManyCheckbox(jakarta.faces.component.html.HtmlSelectManyCheckbox)

Example 3 with HtmlSelectOneRadio

use of jakarta.faces.component.html.HtmlSelectOneRadio in project myfaces by apache.

the class HtmlRadioRendererBase method renderRadio.

/**
 * Renders the input item
 * @return the 'id' value of the rendered element
 */
protected String renderRadio(FacesContext facesContext, UIInput uiComponent, String value, boolean disabled, boolean checked, boolean renderId, Integer itemNum) throws IOException {
    String clientId = uiComponent.getClientId(facesContext);
    String itemId = (itemNum == null) ? null : clientId + facesContext.getNamingContainerSeparatorChar() + itemNum;
    ResponseWriter writer = facesContext.getResponseWriter();
    writer.startElement(HTML.INPUT_ELEM, uiComponent);
    if (itemId != null) {
        writer.writeAttribute(HTML.ID_ATTR, itemId, null);
    } else if (renderId) {
        writer.writeAttribute(HTML.ID_ATTR, clientId, null);
    }
    writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null);
    String group = uiComponent instanceof HtmlSelectOneRadio ? ((HtmlSelectOneRadio) uiComponent).getGroup() : null;
    if (group != null && !group.isEmpty()) {
        UIForm form = ComponentUtils.findClosest(UIForm.class, uiComponent);
        writer.writeAttribute(HTML.NAME_ATTR, form.getClientId(facesContext) + facesContext.getNamingContainerSeparatorChar() + group, null);
    } else {
        writer.writeAttribute(HTML.NAME_ATTR, clientId, null);
    }
    if (disabled) {
        writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
    }
    if (checked) {
        writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null);
    }
    if (group != null && group.length() > 0) {
        if (value != null) {
            writer.writeAttribute(HTML.VALUE_ATTR, clientId + facesContext.getNamingContainerSeparatorChar() + value, null);
        } else {
            writer.writeAttribute(HTML.VALUE_ATTR, clientId + facesContext.getNamingContainerSeparatorChar() + "", null);
        }
    } else {
        if (value != null) {
            writer.writeAttribute(HTML.VALUE_ATTR, value, null);
        } else {
            writer.writeAttribute(HTML.VALUE_ATTR, "", 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);
            CommonHtmlAttributesUtil.renderFieldEventPropertiesWithoutOnchange(writer, commonPropertiesMarked, uiComponent);
        } else {
            HtmlRendererUtils.renderBehaviorizedOnchangeEventHandler(facesContext, writer, uiComponent, itemId != null ? itemId : clientId, behaviors);
            if (isCommonEventsOptimizationEnabled(facesContext)) {
                Long commonEventsMarked = CommonHtmlEventsUtil.getMarkedEvents(uiComponent);
                CommonHtmlEventsUtil.renderBehaviorizedEventHandlers(facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, itemId != null ? itemId : clientId, behaviors);
                CommonHtmlEventsUtil.renderBehaviorizedFieldEventHandlersWithoutOnchange(facesContext, writer, commonPropertiesMarked, commonEventsMarked, uiComponent, itemId != null ? itemId : clientId, behaviors);
            } else {
                HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, uiComponent, itemId != null ? itemId : clientId, behaviors);
                HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(facesContext, writer, uiComponent, itemId != null ? itemId : clientId, behaviors);
            }
        }
        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_STYLE_AND_EVENTS);
    } else {
        HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_STYLE);
    }
    if (isDisabled(facesContext, uiComponent)) {
        writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
    }
    writer.endElement(HTML.INPUT_ELEM);
    return itemId;
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) UIForm(jakarta.faces.component.UIForm) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) HtmlSelectOneRadio(jakarta.faces.component.html.HtmlSelectOneRadio)

Example 4 with HtmlSelectOneRadio

use of jakarta.faces.component.html.HtmlSelectOneRadio 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 5 with HtmlSelectOneRadio

use of jakarta.faces.component.html.HtmlSelectOneRadio in project myfaces by apache.

the class HtmlSelectOneRadioClientBehaviorRendererTest method createComponentToTest.

@Override
protected UIComponent createComponentToTest() {
    UIComponent component = new HtmlSelectOneRadio();
    UISelectItem item = new UISelectItem();
    item.setItemValue("value1");
    component.getChildren().add(item);
    return component;
}
Also used : UIComponent(jakarta.faces.component.UIComponent) HtmlSelectOneRadio(jakarta.faces.component.html.HtmlSelectOneRadio) UISelectItem(jakarta.faces.component.UISelectItem)

Aggregations

HtmlSelectOneRadio (jakarta.faces.component.html.HtmlSelectOneRadio)5 UIForm (jakarta.faces.component.UIForm)2 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)2 ResponseWriter (jakarta.faces.context.ResponseWriter)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 UIComponent (jakarta.faces.component.UIComponent)1 UISelectItem (jakarta.faces.component.UISelectItem)1 UISelectOne (jakarta.faces.component.UISelectOne)1 HtmlOutputLabel (jakarta.faces.component.html.HtmlOutputLabel)1 HtmlOutputText (jakarta.faces.component.html.HtmlOutputText)1 HtmlSelectManyCheckbox (jakarta.faces.component.html.HtmlSelectManyCheckbox)1 HtmlSelectManyListbox (jakarta.faces.component.html.HtmlSelectManyListbox)1 HtmlSelectManyMenu (jakarta.faces.component.html.HtmlSelectManyMenu)1 HtmlSelectOneListbox (jakarta.faces.component.html.HtmlSelectOneListbox)1 HtmlSelectOneMenu (jakarta.faces.component.html.HtmlSelectOneMenu)1 VisitHint (jakarta.faces.component.visit.VisitHint)1 Converter (jakarta.faces.convert.Converter)1 SelectItem (jakarta.faces.model.SelectItem)1 StringWriter (java.io.StringWriter)1