How to find an UIComponent in ADF page root and region?

By W.Zh  Nov 2014

If you want to locate an UIComponent in view root with its component id, here is a recursive way to achieve this.
    public static UIComponent findComponentInRoot(String id) {
UIComponent component = null;

FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}

return component;
}

public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;

UIComponent kid = null;
UIComponent result = null;
Iterator kids = base.getFacetsAndChildren();
while (kids.hasNext() && (result == null)) {
kid = (UIComponent)kids.next();
if (id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponent(kid, id);
if (result != null) {
break;
}
}
return result;
}

 

And if a tag is in region, you need to use this method to look up in the region tree nodes.

public static UIComponent getUIComponentInRegion(String regionId,
String componentId) {
RichRegion region = (RichRegion)findComponentInRoot(regionId);
UIComponent component = null;
if (region != null) {
component = findComponent(region, componentId);
}
return component;
}

Advertisements