Skip to content

Instantly share code, notes, and snippets.

@biemond
Created September 28, 2011 19:08
Show Gist options
  • Save biemond/1248904 to your computer and use it in GitHub Desktop.
Save biemond/1248904 to your computer and use it in GitHub Desktop.
Set the Focus in ADF
package nl.whitehorses.adf.demo.beans;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import oracle.adf.controller.TaskFlowId;
import oracle.adf.share.ADFContext;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
public class Focus {
// to make this work the UIComponent must have clientComponent="true"
public String getFocus() {
FacesContext facesContext = FacesContext.getCurrentInstance();
String clientId = null;
// only for the secondPage, this page contains no dynamic regions
if ("/secondView".equals(facesContext.getViewRoot().getViewId())){
clientId = "r1:0:it3";
}
// only for the startPage
if ("/startView".equals(facesContext.getViewRoot().getViewId())){
// what is the current Task Flow
TaskFlowId taskFlow =
(TaskFlowId)this.getExpression("DynamicRegion.dynamicTaskFlowId");
// in the region we need to find the component
UIComponent region = facesContext.getViewRoot().findComponent("r1");
UIComponent focus = null;
// department TF
if ( "/WEB-INF/department.xml#department".equals(taskFlow.getFullyQualifiedName()) ){
// find the Focus UIComponent in the department TF
// and get its clientId
focus = region.findComponent("it1");
// employee TF
} else if ( "/WEB-INF/employee.xml#employee".equals(taskFlow.getFullyQualifiedName())) {
// find the Focus UIComponent in the employee TF
// and get its clientId
focus = region.findComponent("it3");
}
if ( focus != null ) {
// found so use javascript to set the focus
clientId = focus.getClientId(facesContext);
ExtendedRenderKitService service =
Service.getRenderKitService(facesContext,
ExtendedRenderKitService.class);
service.addScript(facesContext,
" var b = AdfPage.PAGE.findComponent('"+clientId+"'); b.focus();"
);
} else {
// nothing found then set the startPage focus
clientId = null;
}
// nothing found then set the startPage focus, this
// can be handled without javascript
if ( clientId == null )
clientId = "it2";
System.out.println(new java.util.Date()+" focus on "+clientId);
}
return clientId;
}
private Object getExpression(String param) {
ADFContext adfCtx = ADFContext.getCurrent();
ELContext elContext2 = adfCtx.getELContext();
ExpressionFactory elFactory2 = adfCtx.getExpressionFactory();
ValueExpression valueExp2 =
elFactory2.createValueExpression(elContext2,
"#{"+param+"}",Object.class);
return valueExp2.getValue(elContext2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment