Pages

Thursday, November 19, 2009

Find an UIComponent in an ADF Task Flow Region

When you want to find an UIComponent (like a Tree) inside an ADF Region you can not use findComponent on the ViewRoot because this will only search for the component in the JSF page and not in the JSF page fragments ( Task Flows). And off course you can use a backing bean to make a binding but I want to find the component by first searching for the right Region in the JSF page and then searching the component inside the Region.

First I need to have some common methods.
// find a jsf component inside the JSF page
private UIComponent getUIComponent(String name) {
  FacesContext facesCtx = FacesContext.getCurrentInstance();
  return facesCtx.getViewRoot().findComponent(name) ;
}

// find a UIComponent inside a UIComponent 
private UIComponent getUIComponent(UIComponent component, String name) {
        if (component != null)
            System.out.println(component.getId());
        
        List<UIComponent> items = component.getChildren();
        Iterator<UIComponent> facets = component.getFacetsAndChildren();

        if  ( items.size() > 0 ) {
          System.out.println("got childern");
          for (UIComponent item : items) {
              UIComponent found = getUIComponent(item, name);
              if (found != null) {
                  return found;
              }
              if (item.getId().equalsIgnoreCase(name)) {
                  return item;
              }
          }
        } else if ( facets.hasNext()) {
            System.out.println("got facets");
            while ( facets.hasNext() ){
              UIComponent facet = facets.next();  
              UIComponent found = getUIComponent(facet, name);
              if (found != null) {
                  return found;
              }
              if (facet.getId().equalsIgnoreCase(name)) {
                  return facet;
              }
            }
        }
        return null;
    }
Now we can find the right Region and then search inside the Region for the ADF Tree
// get the dymamic region of the main page
RichRegion region = (RichRegion)getUIComponent("dynam1");
if ( region != null) {
  // find tree 2 
  RichTree rt = (RichTree)getUIComponent(region,"t2");
  if ( rt != null ) {
    // do your thing
  }   
}
Or you can use the findComponent method after you found the region
// get the dymamic region of the main page
RichRegion region = (RichRegion)getUIComponent("dynam1");
if ( region != null) {
  // find tree 2 
  RichTree rt = (RichTree)region.findComponent("t2");
  if ( rt != null ) {
   // do your thing
  }   
}

8 comments:

  1. I have a page with a region, the region has a panelStretchLayout, and within this an inputText,
    when the method to call the children of panelStretchLayout, the method returns null

    ReplyDelete
  2. Hi,

    Can you make a testcase and send it to biemond at gmail dot com

    thanks

    ReplyDelete
  3. Test Case
    http://www.box.net/shared/s8g7gxac01

    Run-> Main.jspx and look the Log of JDeveloper

    ReplyDelete
  4. Hi,

    I found the problem,

    first the quick solution , you can do a findComponent on the region.

    RichRegion region = (RichRegion)getUIComponent("theRegion");
    RichInputText it = (RichInputText)region.findComponent("it1");


    and now your problem. Some components dont have childern but facets. So I changed the getUIComponent component.

    private UIComponent getUIComponent(UIComponent component, String name) {
    if (component != null)
    System.out.println(component.getId());

    List<UIComponent> items = component.getChildren();
    Iterator<UIComponent> facets = component.getFacetsAndChildren();

    if ( items.size() > 0 ) {
    System.out.println("got childern");
    for (UIComponent item : items) {
    UIComponent found = getUIComponent(item, name);
    if (found != null) {
    return found;
    }
    if (item.getId().equalsIgnoreCase(name)) {
    return item;
    }
    }
    } else if ( facets.hasNext()) {
    System.out.println("got facets");
    while ( facets.hasNext() ){
    UIComponent facet = facets.next();
    UIComponent found = getUIComponent(facet, name);
    if (found != null) {
    return found;
    }
    if (facet.getId().equalsIgnoreCase(name)) {
    return facet;
    }
    }
    }
    return null;
    }

    thanks I will update this blogpost

    ReplyDelete
  5. Thanks for your time

    In the next Blog I found a similar solution
    http://www.jroller.com/page/mert?entry=how_to_find_a_uicomponent

    ReplyDelete
  6. Hi,

    The blog was very useful . I am using Jdeveloper 11.1.1.4.0 .. I am getting Missing in and out parameter...
    the scenario is like this ..
    I have a choice list , for which i have inturn created a view and provided a bind variable ,
    Issue is whenever i select a value from the choice list i get the above error.

    ReplyDelete
  7. Hi,
    I am able to get the component(SelectOneChoice) in the region, but when I try to get it's value it returns a null even when I have selected one.

    ReplyDelete
    Replies
    1. Solved. The SelectOneChoice value attribute in the .jsff file should not be set, then after selecting one of the values it returns that value. But have no idea why this happens??

      Delete