« ADF Faces: Remove navigation range bar at the bottom or top of a table | Main | Custom skins deployment strategies for MyFaces Trinidad and ADF Faces Rich Client »
ADF Faces RC: How to programmatically add an event listener to a dynamically created component
By frank.nimphius | February 1, 2008
JavaServer Faces is a very flexible technology and allows developers - at runtime - to create new component instances to their JSF page on an if needed basis. One possible usecase is a tab panel, on which the developer needs to dynamically add a new tab. While adding a new tab is pretty straight forward to achieve from within a managed bean,
-
RichShowDetailItem rsd = new RichShowDetailItem();
-
rsd.setText("Hello");
-
rsd.setId("tabId1");
-
DisclosureEvent de = new DisclosureEvent(rsd,true);
-
tabPanel1.getChildren().add(rsd);
assigning a listener event method to it requires a bit more knowledge. So for this example, lets assume that there exist a managed bean called TabBean.java (exposed as TabBean in JSF) with an event handler method of
-
public void onDiscloseChange(DisclosureEvent disclosureEvent) {
-
System.out.println("I am called by " +((RichShowDetailItem) disclosureEvent.getSource()).getId());
-
}
The ExpressionLanguage string that you would use to wire a tab panel component to this method then looks like
-
#{TabBean.onDiscloseChange}
To dynamically add this Expression Language reference to a dynamically created tab page, you use the following code in the same managed bean that you use to create the tab
-
public String commandButton_action() {
-
RichShowDetailItem rsd = new RichShowDetailItem();
-
...
-
DisclosureEvent de = new DisclosureEvent(rsd,true);
-
rsd.setDisclosureListener(getMethodExpression("#{TabBean.onDiscloseChange}",Object.class,new Class[]{DisclosureEvent.class},new Object[] {de}));
-
...
-
}
-
-
public void onDiscloseChange(DisclosureEvent disclosureEvent) {
-
System.out.println("I am called by " +((RichShowDetailItem) disclosureEvent.getSource()).getId());
-
}
-
-
public MethodExpression getMethodExpression(String expr, Class returnType, Class[] argTypes,Object[] args){
-
FacesContext fc = FacesContext.getCurrentInstance();
-
ELContext elctx = fc.getELContext();
-
ExpressionFactory elFactory = fc.getApplication().getExpressionFactory();
-
MethodExpression methodExpr = elFactory.createMethodExpression(elctx,expr,returnType,argTypes);
-
return methodExpr;
-
}
-
}
So what is the above code doing? When creating a new tab, the call to setDisclosureListener() allows you to specify a disclosure listener for the new component. To obtain the JSF 1.2 method expression for this event handler method, you use code as shown in the getMethodExpression() helper. The key is that to create a new event handler reference, you need to tell it a) the class of the object argument: "new Class[]{DisclosureEvent.class}", and more important b) the actual event source: DisclosureEvent de = new DisclosureEvent(rsd,true); where "rsd" is the newly created tab instance.
Frank
Topics: ADF Faces | No Comments »
Comments are closed.