Tuesday, April 23, 2013

Using ADF Client Side Listeners & Server Side Listeners to save panelSplitter position in a managed bean

In some cases where we find the framework supported events and handlers to be inadequate and need a custom event to be handled by the components. ADF Client side and server side listeners work in conjunction with each other to suffice such a requirement.

One such example is with panelSplitter component. Let us say a user wants to save the state of panelSplitter (splitter position) in session, then below solution would be handy.

The af:clientListener and af:serverListener tags are defined for a panelSplitter component as follows:

<af:panelSplitter id="ps1" label="MySplitter">
  <af:clientListener method="propertyChangeClientListener" type="propertyChange"/>
  <af:serverListener type="mySplitterCustomEvent"
                     method="#{mybean.handleSplitterPositionChange}"/>
</af:inputText>


On changing the splitter position through user action, the client listener invokes the below javascript method - propertyChangeClientListener(). That method indirectly invokes the server side listener by raising a particular event that the server side listener is waiting for. That event here is "mySplitterCustomEvent". This event is queued in AdfCustomEvent with the parameter as splitterPosition value and a configuration value "true" to make this immediate on server.

function propertyChangeClientListener(e) {
    var inputComp = AdfPage.PAGE.findComponent(e.getSource().getClientId());
    if(e.getPropertyName().toString() == "splitterPosition"){
      var newVal = e.getNewValue();
      AdfCustomEvent.queue(inputComp, "mySplitterCustomEvent",
                                 // Send one parameter
                                 {splitterPosition:newVal},
                                 // Make it "immediate" on the server
                                 true);
    }
}

Given below is the server side listener method. This method reads the value of the splitterPosition from the custom event received and stores the value in a managed bean "resizeBean".

public void handleSplitterPositionChange(ClientEvent event){
     ResizePersistenceBean resizeBean = null;
    try{
       //Use ADFUtil class posted in my earlier blogs
        resizeBean = (ResizePersistenceBean)ADFUtil.evaluateEL("#{resizePersistenceBean}");
       if(!event.getParameters().get("splitterPosition").toString().equals("")){
        resizeBean.setSplitterPosition((int)(Float.parseFloat(event.getParameters().get("splitterPosition").toString())));
       }
    }catch(Exception e){
       resizeBean.setSplitterPosition(200);
    }
}

No comments:

Post a Comment