3

I've written (well, modified) a custom component for radio buttons appearing inside a datatable based on this article. I've modified it slightly for JSF 2.2 by using the @FacesRenderer and @FacesComponent tags and omitting the custom tag class. Everything works fine in my XHTML page

xmlns:custom="http://mynamespace"
...
<h:dataTable id="mySampleTable3"
             value="#{myBackingBean.sampleTable3}"
             var="item"
             width="100%"
             border="1"
             first="0">           
    <f:facet name="header">
         <h:panelGrid id="gridHeader3" columns="2" width="100%" >
              <h:outputText id="outputText3" 
                       value="Radios grouped in row(With our custom tag)"/>   
         </h:panelGrid>
    </f:facet>
    <h:column> 
         <f:facet name="header">
                 <h:outputText value="Emp Name" />
         </f:facet>
         <h:outputText id="empName" value="#{item.empName}"/>
    </h:column>         
    <h:column> 
         <f:facet name="header">
                 <h:outputText value="Excellent" />
         </f:facet>
         <custom:customradio id="myRadioId2" 
                             name="myRadioRow" 
                             value="#{item.radAns}"
                             itemValue="E" />                     
    </h:column>         
    <h:column> 
         <f:facet name="header">
                  <h:outputText value="Good" />
         </f:facet>
         <custom:customradio id="myRadioId3" 
                             name="myRadioRow" 
                             value="#{item.radAns}"
                             itemValue="G" />                     
     </h:column>   
     ...      
</h:dataTable>

and the datatable of radio buttons work correctly (they are grouped by row). The problem arises when I use this component dynamically. So I have

HtmlPanelGrid radioGrid = (HtmlPanelGrid) getApplication()
                          .createComponent(HtmlPanelGrid.COMPONENT_TYPE);

...

UICustomSelectOneRadio customSelectOneRadio = (UICustomSelectOneRadio) getApplication().
                    createComponent(UICustomSelectOneRadio.COMPONENT_TYPE);


customSelectOneRadio.setId("rb_" + question.getQuestionId() + "_" + row + "_" + col);
customSelectOneRadio.setName("myRadioRow");

String binding = "#{" + beanName + "." + propName + 
              "[\"" + question.getQuestionId().toString() + "_" + 
              subQuestion.getId() + "\"]}";

ValueExpression ve = getExpressionFactory().
                    createValueExpression(getELContext(), binding, String.class);
customSelectOneRadio.setValueExpression("value", ve);

customSelectOneRadio.setItemValue(value);

radioGrid.getChildren().add(customSelectOneRadio);

Unfortunately, although the datatable is rendered, the radio buttons aren't. Blank columns appear. I was wondering if anyone knows if there is some method I should be overriding in my renderer. Mine looks like:

@FacesRenderer(componentFamily = UICustomSelectOneRadio.COMPONENT_FAMILY, rendererType = HTMLCustomSelectOneRadioRenderer.RENDERER_TYPE)
public class HTMLCustomSelectOneRadioRenderer extends Renderer {

    public static final String RENDERER_TYPE = "com.myapp.jsf.renderers.HTMLCustomSelectOneRadioRenderer";

    @Override
    public void decode(FacesContext context, UIComponent component) {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }

        UICustomSelectOneRadio customSelectOneRadio;
        if (component instanceof UICustomSelectOneRadio) {
            customSelectOneRadio = (UICustomSelectOneRadio) component;
        } else {
            return;
        }

        Map map = context.getExternalContext().getRequestParameterMap();
        String name = getName(customSelectOneRadio, context);
        if (map.containsKey(name)) {
            String value = (String) map.get(name);
            if (value != null) {
                setSubmittedValue(component, value);
            }

        }
    }

    private void setSubmittedValue(UIComponent component, Object obj) {
        if (component instanceof UIInput) {
            ((UIInput) component).setSubmittedValue(obj);
        }
    }


    private String getName(UICustomSelectOneRadio customSelectOneRadio,
            FacesContext context) {

        UIComponent parentUIComponent
            = getParentDataTableFromHierarchy(customSelectOneRadio);
        if (parentUIComponent == null) {
            return customSelectOneRadio.getClientId(context);
        } else {
            if (customSelectOneRadio.getOverrideName() != null
                    && customSelectOneRadio.getOverrideName().equals("true")) {
                return customSelectOneRadio.getName();
            } else {
                String id = customSelectOneRadio.getClientId(context);
                int lastIndexOfColon = id.lastIndexOf(":");
                String partName = "";
                if (lastIndexOfColon != -1) {
                    partName = id.substring(0, lastIndexOfColon + 1);
                    if (customSelectOneRadio.getName() == null) {
                        partName = partName + "generatedRad";
                    } else {
                        partName = partName + customSelectOneRadio.getName();
                    }
                }
                return partName;
            }
        }
    }

    private UIComponent getParentDataTableFromHierarchy(UIComponent uiComponent) {
        if (uiComponent == null) {
            return null;
        }
        if (uiComponent instanceof UIData) {
            return uiComponent;
        } else {
            //try to find recursively in the Component tree hierarchy
            return getParentDataTableFromHierarchy(uiComponent.getParent());
        }
    }

    @Override
    public void encodeEnd(FacesContext context, UIComponent component)
            throws IOException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }

        UICustomSelectOneRadio customSelectOneRadio
            = (UICustomSelectOneRadio) component;

        if (component.isRendered()) {
            ResponseWriter writer = context.getResponseWriter();

            writer.startElement("input", component);
            writer.writeAttribute("type", "radio", "type");
            writer.writeAttribute("id", component.getClientId(context), "id");
            writer.writeAttribute("name", getName(customSelectOneRadio, context), "name");

            if (customSelectOneRadio.getStyleClass() != null && customSelectOneRadio.getStyleClass().trim().
                    length() > 0) {
                writer.writeAttribute("class", customSelectOneRadio.getStyleClass().trim(), "class");
            }
            if (customSelectOneRadio.getStyle() != null && customSelectOneRadio.getStyle().trim().length() > 0) {
                writer.writeAttribute("style", customSelectOneRadio.getStyle().trim(), "style");
            }

         ...

            if (customSelectOneRadio.getValue() != null
                    && customSelectOneRadio.getValue().equals(customSelectOneRadio.getItemValue())) {
                writer.writeAttribute("checked", "checked", "checked");
            }

            if (customSelectOneRadio.getItemLabel() != null) {
                writer.write(customSelectOneRadio.getItemLabel());
            }
            writer.endElement("input");
        }
    }

}

To quickly summarise, this is working when I use a custom tag in an facelets/XHTML file, but not when I use it dynamically, e.g.

<h:panelGroup id="dynamicPanel" layout="block" binding="#{documentController.dynamicPanel}"/>

If anyone has any ideas on what I could be missing I'd be grateful.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Quantum
  • 91
  • 7

1 Answers1

0

Gotcha! It all revolved around changing the rendererType property. From the Renderer class above, you can see I had it set to my own, i.e.

public static final String RENDERER_TYPE = "com.myapp.jsf.renderers.HTMLCustomSelectOneRadioRenderer";

(1) I changed it to

public static final String RENDERER_TYPE = "javax.faces.Radio";

(2) I also changed my xxx.taglib.xml file to reflect this, e.g.

<namespace>http://mycomponents</namespace>
<tag>
    <tag-name>customradio</tag-name>
    <component>
        <component-type>CustomRadio</component-type>
        <renderer-type>javax.faces.Radio</renderer-type>     /*<----- See Here */
    </component>
</tag>

(3) But, here's the final step to note. I also had to create the following constructor in my UICustomSelectOneRadio component. It didn't work without this either.

public UICustomSelectOneRadio() {
    this.setRendererType("javax.faces.Radio"); 
}

Remember - I extended my component from UIInput, so javax.faces.Text would have been set. Leaving my component family as "CustomRadio" was fine.

I'm still not 100% comfortable with renderer types etc., but for anyone who wants a little info, BalusC explains it well here.

Community
  • 1
  • 1
Quantum
  • 91
  • 7