Solution: ADF Faces, how to pragmatically open an af:popup by id without JavaScript

There are many ways to use javascript to locate a popup by id and make it visible. However, things quickly become complicated if your popup is nested within many containers or adf components. In my case, the popup is contained within a page template which is consumed by a master page. The id becomes increasingly complex as the master page layout changes during development (code started to look like ::lol:lol:goofy:staph:tryingtomakeitworkcomponent). At the end the solutions using javascript fails to work and more importantly isn’t easy to distribute amongst a team of developers. Instead, use the simple approach described below via Java:


/**
* called when a row is double clicked within my table,
* popupid is the absolute id of the component
* (no container ::: funny business here!)
**/

public void openPopup(String popupId) {
RichPopup popup = (RichPopup)ADFFacesUtil.findComponentInRoot(popupId);
popup.show(new RichPopup.PopupHints());//modify these hint parameters if you do not want the default behavior.
}

/**
* Locate an UIComponent in view root with its component id. Use a recursive way to achieve this.
* Taken from http://www.jroller.com/page/mert?entry=how_to_find_a_uicomponent
* @param id UIComponent id
* @return UIComponent object
*/
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;
}


/**
* Locate an UIComponent from its root component.
* Taken from http://www.jroller.com/page/mert?entry=how_to_find_a_uicomponent
* @param base root Component (parent)
* @param id UIComponent id
* @return UIComponent object
*/
public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;

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

Result:
template

Advertisements

About wesfang

www.linkedin.com/in/wesfang/ https://twitter.com/wesleyfang
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s