Enabling and Disabling ADF Components


The purpose of this is to show different alternatives to enable and disable ADF components. There are 3 separate sample workspaces (11.1.1.2.0), one for each one of the ways to enable and disable ADF components that will be shown.

 

 

1. Using isDisabled() and setDisabled() methods on the managed bean.

 

In this example, the first af:commandButton (cb1) calls a method in the backing bean which enables/disabled the other af:commandButton:

 

<af:commandButton text="Press Me!" binding="#{backing_test1.cb1}"

                  id="cb1" action="#{backing_test1.toggleComponent}"/>

<br/>

<af:goLink text="goLink 1" binding="#{backing_test1.gl1}" id="gl1"

           destination="http://www.oracle.com"/>

 

This is the method used in the backing bean which simply uses the isDisabled() and setDisabled() methods of the second button:

 

public void toggleComponent() {

    if (gl1.isDisabled()) {

        gl1.setDisabled(false);

        gl1.setText("This goLink is now enabled");

    } else {

        gl1.setDisabled(true);

        gl1.setText("This goLink is now disabled");

    }

}

 

Further information about the isDisabled() and setDisabled() methods (that are also applicable to other ADF components) can be seen at the Java API Reference for Oracle ADF Faces.

 

Download sample workspace here.

 

 

2. Using expression language to check if the component is disabled or not.

 

As in the previous example, the first af:commandButton (cb1) calls a method in the backing bean which enables/disabled the other af:commandButton:

 

<af:commandButton text="Press Me!" binding="#{backing_test1.cb1}"

                  id="cb1"

                  actionListener="#{backing_test1.toggleComponent}"/>

<br/>

<af:inputDate label="Date:" binding="#{backing_test1.id1}" id="id1"/>

 

This is the method used in the backing bean which uses Expression Language to check if the second button is disabled or not:

 

public void toggleComponent(ActionEvent evento) {

    FacesContext fctx = FacesContext.getCurrentInstance();

    ELContext elctx = fctx.getELContext();

    Application jsfApp = fctx.getApplication();

    //create a ValueExpression that points to the ADF binding layer

    ExpressionFactory exprFactory = jsfApp.getExpressionFactory();

    ValueExpression valueExpr = exprFactory.createValueExpression(

                                 elctx,

                                 "#{!backing_test1.id1.disabled}",

                                  Object.class

                                 ); 

    id1.setDisabled(Boolean.parseBoolean(valueExpr.getValue(elctx).toString()));

}

 

Further information about Expression Language Techniques can be seen in this Advanced Expression Language Techniques OTN article.

 

Download sample workspace here.

 

 

3. Using expression language on the Disabled property of the ADF component on the JSPX.

 

In this example the managed bean method to disable the second button is also called from the first button, but the second button has its disabled property set on the JSPX to take the EL value from the managed bean attribute:

 

<af:commandButton text="Click me" id="cb1"

                  actionListener="#{ToggleTestBk.toggleComponent}"/>

<br/>

<af:commandButton text="commandButton 2" id="cb2"

                  disabled="#{ToggleTestBk.isButtonEnabled}"

                  partialSubmit="true"/>

 

The managed bean method simply uses a private Boolean attribute which indicates whether the button is enabled or not and changes the value every time is invoked.

 

private boolean isButtonEnabled;

...

public void toggleComponent(ActionEvent event) {

    try{

        this.setIsButtonEnabled(!isButtonEnabled);

    }

    catch(Exception ex){

      System.out.println("Exception in toggleButton: " + ex);

    }

}

 

In the downloadable sample workspace provided for this example, the managed bean scope configured for the managed bean is application, but the application works with session scope as well. Further information about the 4 possible values for the managed bean scope element can be seen in this JSF Managed Bean Facility OTN article.

Download sample workspace here.


 

On the first workspace a goLink is enabled and disabled, on the second one an inputDate is used, and on the third one a commandButton. There are many other ADF components that have the Disabled property.

 

Three approaches, one similar behaviour. Obviously, you can figure out many other ways to get this enable/disable functionality on ADF components.  It's up to you to decide which alternative is best for you.


Comments:

In what adf phase can I acces to adf component for example af:commandbuton to enable or disable when page loads?

I´m trying to use this code but does not work ...

public void beforePhase(PagePhaseEvent event)
{

if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID)
{
if (!AdfFacesContext.getCurrentInstance().isPostback())
{
System.out.println("carga inicial de pagina");

if (cbCrear == null)
{
System.out.println("PM carga inicial de pagina");
}
else
{
System.out.println("PM Ya puedo acceder al boton");
}
}
}

if (event.getPhaseId() == Lifecycle.PREPARE_RENDER_ID)
{
if (!AdfFacesContext.getCurrentInstance().isPostback())
{
System.out.println("carga inicial de pagina");

if (cbCrear == null)
{
System.out.println("PR carga inicial de pagina");
}
else
{
System.out.println("PR Ya puedo acceder al boton");
}
}
}

}

Posted by Oscar Manuel Juarez on November 29, 2010 at 04:09 PM CLST #

Hi Oscar.

Besides the phases (which can be sometimes confusing to use) you can use an easier approach on ADF components like af:commandbuton to enable or disable them when the page loads by implementing a constructor method on the Managed Bean (UI components must be exposed when creating the JSPX page) and call the corresponding enabling/disabling method on that constructor, which will be executed every time the page is loaded.

Posted by Cristóbal Soto on January 13, 2011 at 09:42 AM CLST #

Post a Comment:
  • HTML Syntax: NOT allowed
About


Principal Technical Support Engineer in the Exalogic Enterprise Support Team (EEST).

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today