Saturday, July 7, 2012

Use Of AF:Switcher with ViewScope Variable In JDeveloper 11g

ADF Faces RC.

The af:switcher component is able to show facet component dynamically. It will render the facet matching "FacetName" property. Its a server side component with no client side representation. Refer here for more info .
The af:setPropertyListener is declartive operation component in ADF for assigning value when certain event fires. Refer more about propertyListener here .

  1. Open your adfc-config.xml file for unbounded task flow.
  2. Select view activity & in property inspector, under Page Parameters click + icon.
  3. Write in From Value=First & To Value=#{viewScope.paramVal}. When the view activity/jspx page will run framework will create a viewScope variable "paramVal" if not found & assign the "First" value to it. Your view activity would show warning message stating "Reference Not Found..." in design time. Just ignore it.

  4.    Drag the af:switcher component from component palette. Right click to select 'insert inside
         swithcer' & then "facet". Repeat twice for 2 facet named "First" & "Second" respectively. 
         Drag af:message component inside both facets.
  5.    You can set "DefaultFacet" property to any facet you want, in case, if "FacetName" is not
         defined or returns Null , DefaultFacet property will be used.
  6.    Select af:switcher in Structure window & write FacetName= #{viewScope.paramVal} .Now
         when first time the page will run, First facet would be selected.
  7.    Now, drag 2 command buttons. Under each button drag af:setPropertyListener.
  8.    In the first button's propertyListener write From=First, To= #{viewScope.paramVal}
         Type=action .
  9.    In the 2nd button's propertyListener write From=Second, To= #{viewScope.paramVal},
         Type=action .
10.    Just the optional thing to do is disabling your button after clicking it.Select first button & in
         disabled property write #{viewScope.paramVal == "First"} .
11.    Do same for second button & write #{viewScope.paramVal == "Second"} .

Run the page to test. 

The above functionality can also achieved by viewScope Managed Bean instead of viewScope variable.
Sample Workspace for viewScope variable - Download
Sample Workspace for viewScope bean - Download

Note the viewScope project has java class which is not using as managed bean. This class is 
wired in managed bean project workspace.