Friday, June 1, 2012

Disabling / Enabling all components of Panel Form Layout at single click

I have one business requirement where i need to make all the UI component of the form disable single click on the button.

Moreover panelFormLayout does not have any property call disable or readonly.

Although i know we can do this using bind each component on the backing and setting the readonly /disable  property true or  putting some condition  on readonly /disable ,but this is any how very tedious  because manually we need to put condition on each individual component .

But making this scenario automatic i have done following step ..

1-First i bound  the panelForm  element  to backing bean.
2-And getting all the child compoenet through following statement
List<UIComponent> uiComponentList = formClass.getChildren();
This will give us all the child element
3-Then i  loop  through all element and  checked  what is instance of component and after getting instance of the component i am setting disable property true.

Code for making disable are following
        List<UIComponent> uiComponentList = formClass.getChildren();
        for (UIComponent uiComponent : uiComponentList) {
            if (uiComponent instanceof RichInputText) {
                ((RichInputText)uiComponent).setDisabled(true);
            } else if (uiComponent instanceof RichSelectBooleanCheckbox) {
                ((RichSelectBooleanCheckbox)uiComponent).setDisabled(true);
            } else if (uiComponent instanceof RichSelectBooleanRadio) {
                ((RichSelectBooleanRadio)uiComponent).setDisabled(true);
            } else if (uiComponent instanceof RichSelectManyChoice) {
                ((RichSelectManyChoice)uiComponent).setDisabled(true);
            }

        }

Code for making enable are following

              List<UIComponent> uiComponentList = formClass.getChildren();
        for (UIComponent uiComponent : uiComponentList) {
            if (uiComponent instanceof RichInputText) {
                ((RichInputText)uiComponent).setDisabled(false);
            } else if (uiComponent instanceof RichSelectBooleanCheckbox) {
                ((RichSelectBooleanCheckbox)uiComponent).setDisabled(false);
            } else if (uiComponent instanceof RichSelectBooleanRadio) {
                ((RichSelectBooleanRadio)uiComponent).setDisabled(false);
            } else if (uiComponent instanceof RichSelectManyChoice) {
                ((RichSelectManyChoice)uiComponent).setDisabled(false);
            }

        }


*In this code you can add your codes as your requirement.


Jsf page code is following:
 
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:form id="f1">
        <af:panelFormLayout id="pfl1" inlineStyle="height:52px;">
          <af:commandButton text="Disable" id="cb1"
                            actionListener="#{backingBeanScope.classOne.commandButton}"/>
          <af:commandButton text="Enable" id="cb2"
                            actionListener="#{backingBeanScope.classOne.commandButtonEnable}"/>
        </af:panelFormLayout>
        <af:panelFormLayout id="pfl2"
                            binding="#{backingBeanScope.classOne.formClass}">
          <af:inputText label="Label 1" id="it1"/>
          <af:inputText label="Label 2" id="it2"
                       />
          <af:inputText label="Label 3" id="it3"/>
          <af:inputText label="Label 4" id="it4"/>
          <af:selectBooleanCheckbox text="selectBooleanCheckbox 1"
                                    label="Label 1" id="sbc1"/>
          <af:selectBooleanRadio text="selectBooleanRadio 1" label="Label 1"
                                 id="sbr1"/>
          <af:selectManyChoice label="Select" id="smc1">
            <af:selectItem label="A" value="A" id="si1"/>
            <af:selectItem label="B" value="B" id="si2"/>
          </af:selectManyChoice>
        </af:panelFormLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>

Thanks
Prateek

2 comments:

  1. Hi Prateek,
    Thank you for the post.
    How can I implement "formClass.getChildren()" as it shows invalid.
    Do I have to import a library or any other to make it valid ?

    Regards,
    Lalama

    ReplyDelete
    Replies
    1. forClass is binding for panel form layout where all the child component are present. So you have binding of the panel form layout and getChildren() method will give list of child component.

      Delete