Please disable your adblock and script blockers to view this page

Search this blog

Saturday 5 July 2014

Populate af:treeTable programatically using POJO in Oracle ADF (11g & 12c)

Hello All
This post is about creating & populating af:treeTable programmatically in ADF 11g and 12c
treeTable is basically a hierarchical representation of data, using ADF model layer we can create treeTable very easily by defining master-detail relation between viewObjects
see my previous posts on treeTable-
Tree Table Component in Oracle ADF(Hierarchical Representation)
Tree Table Component with declarative presentation in ADF, Access childs without custom selection listener
Expand and Collapse an af:treeTable programmatically in ADF Faces (Oracle ADF)
Refreshing Child Nodes of an af:treeTable / af:tree in Oracle ADF



to populate treeTable programmatically we need to configure data set that fits to tree hierarchy
thanks to Chris Muir's post about programmatic creation of treeTable
http://one-size-doesnt-fit-all.blogspot.in/2007/05/back-to-programming-programmatic-adf.html

there is 4-5 simple steps to do-
  • Create a fusion web application and drop a af:treeTable on page , in treeTable creation wizard select "bind later" 


  • now create managed bean to populate data in treeTable, create a simple List in managed bean of type POJO class (a java class that contains attributes and their accessor  )
  • so in this app i am going to Tv Serial and respective character names in treeTable, for that i have created a class that contains POJO for treeTable

  • import java.util.ArrayList;
    import java.util.List;
    
    
    public class Seasons {
        public Seasons(String name) {
            this.name = name; // To Store Header Values (Storing Seasons Name :))
            characters = new ArrayList<Seasons>(); // To Store Detail Values (Storing Season's Charatcers)
        }
    
        public void setCharacters(List<Seasons> empDet) {
            this.characters = empDet;
        }
    
        public List<Seasons> getCharacters() {
            return characters;
        }
        private String name;
        private List<Seasons> characters;
    
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    // This methods directly add characters value in list
        public void addEmp(Seasons employee) {
            characters.add(employee);
        }
    
    }
    

  • In this class the characters list behaves as child of seasons, so in managed bean define a top-level list (that contains both master and detail) and add values

  • See managed Bean Code-
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
    
    public class ProgrammaticTreeTabBean {
        List<Seasons> seasonList = new ArrayList<Seasons>();
    
        ChildPropertyTreeModel charatcerVal;
    
        public ProgrammaticTreeTabBean() {
            super();
            // Creating tree for Seasons and Characters details
            // Adding Master Values (seasons)
            Seasons season1 = new Seasons("Game of Thrones");
           // Adding Detail Values (Child Node-Characters)
            Seasons character = new Seasons("Tywin Lannister");
            season1.addCharaters(character);
            character = new Seasons("Tyrion Lannister");
            season1.addCharaters(character);
            character = new Seasons("Jaime Lannister");
            season1.addCharaters(character);
            character = new Seasons("Cersie Lannister");
            season1.addCharaters(character);
            character = new Seasons("Ned Stark");
            season1.addCharaters(character);
    
            // Adding Master Values (seasons)
            Seasons season2 = new Seasons("Arrow");
            // Adding Detail Values (Child Node-Characters)
            character = new Seasons("Oliver Queen");
            season2.addCharaters(character);
            character = new Seasons("Moira Queen");
            season2.addCharaters(character);
            character = new Seasons("Laurel Lance");
            season2.addCharaters(character);
            character = new Seasons("Sara Lance");
            season2.addCharaters(character);
    
            // Adding Master Values (seasons)
            Seasons season3 = new Seasons("Vikings");
          // Adding Detail Values (Child Node-Characters)
            character = new Seasons("Ragnar Lothrak");
            season3.addCharaters(character);
    
            // Adding all list to topList
            seasonList.add(season1);
            seasonList.add(season2);
            seasonList.add(season3);
            // Filtering Child Data, ChildPropertyTreeModel creates a TreeModel from a List of beans, see
            // https://myfaces.apache.org/trinidad/trinidad-api/apidocs/org/apache/myfaces/trinidad/model/ChildPropertyTreeModel.html
            charatcerVal = new ChildPropertyTreeModel(seasonList, "characters");
        }
    
        public ChildPropertyTreeModel getCharatcerVal() {
            return charatcerVal;
        }
    }
    

  • Now code part is complete, now configure your treeTable to show data, see XML source of af:treeTable

  • <af:treeTable rowBandingInterval="0" id="tt1"
                                  value="#{pageFlowScope.ProgrammaticTreeTabBean.charatcerVal}" var="node"
                                  rowSelection="single" initiallyExpanded="true">
                        <f:facet name="nodeStamp">
                            <af:column headerText="Node Stamp" id="c1" width="250">
                                <af:outputText value="#{node.name}" id="ot1" inlineStyle="font-weight:bold;"/>
                            </af:column>
                        </f:facet>
                    </af:treeTable>
    

  • Now run your application and check , what you have done ;)
Happy Learning :) Sample ADF Application (12.1.3)

18 comments :

  1. Hi Ashish,

    Thanks for Your grate Posts from ur blog its not blog its a place i learn alot

    based on this blog I populated TreeTable

    in my case am populated using Files Class in Java with Folders
    how to add child to chaild Items

    and can U please tell me when am adding chailds the same chailds adding to every node

    Thanks and regards

    ReplyDelete
    Replies
    1. Raja

      Check Add and delete records (Parent/Child) in POJO Based ADF TreeTable
      This post is about adding child and parent nodes in POJO based tree
      I hope it'll help you

      Ashish

      Delete
    2. Hi Ashish Thanks for your reply,

      your suggestion is Good but i don't want that type. as My view now we have some chailds(cherecters)we need to add sub chaild

      Root
      |-chaild
      |-sub chaild

      I did same like Ur code and am adding values from Lists

      in MyCode am using folders and subfolders and so on..... according to directory. I loaded all values to map key as root and Chaild as value now we need to add sub chailds

      Please Help me to do that

      for more Clarity see here
      https://community.oracle.com/thread/3943210?sr=inbox&ru=961050

      Thanks in Advance Your help is really precious to me

      Delete
    3. As per above example if you want to add sub child then
      write like this -

      // Adding Master Values (seasons)

      Seasons season4 = new Seasons("Test Data");

      // Adding Detail Values (Child Node-Characters)

      character = new Seasons("Test Child");

      season4.addCharaters(character);

      //Add this as child of first Record

      season1.addCharaters(season4);



      Ashish

      Delete
  2. Ashish, I need pragmatical Logic Help me here its grate help if U did something and as per your example we cant Imzine root and chaild but your example giving one path to every one without your example there is no other ideas. Please help me

    Thanks AShish for your valuable example

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Sorry ashish my code example am posting here but it was not visible here what can i do

    ReplyDelete
    Replies
    1. You have to call addCharacter method for every child level , Now you have to check if a directory has child directory then you have to call this method to add sub child
      I can't make a sample this time , you have to try it yourself

      Ashish

      Delete
  5. Hi Ashish Thanks for this

    Now i got Tree according to my directory but now i have a problem on selection of those i Completely used your code for selection event

    Now the problem is

    Root
    \- chaild (Selection working)
    \-\-sub chaild(Selection not working) :-(

    ReplyDelete
    Replies
    1. Raja Sekhar

      You can traverse treeTable to get all nodes and to get specific node value in multi level tree you can use f:attribute tag in treeTable and on button click get that attribute value

      < f:attribute name="nodeName" value="#{node.name}" / >

      and to get it's value in bean actionEvent.getComponent().getAttributes().get("nodeName")

      Ashish

      Delete
  6. sorry Ashish to disturbing U again and again i tried it, it was throwing null pointer Exception

    Object obj = selectionEvent.getComponent().getAttributes().get("nodeName");
    System.out.println("Selected Obj " + obj.toString());

    ReplyDelete
    Replies
    1. Don't use f:attribute on selectionListener , drop a button or link in a column of treeTable and then drop f:attribute under it , Like this

      < af:column id="c2" width="30" align="center" >
      < af:link id="l1" text="Get Selected Record"
      actionListener="#{viewScope.ProgrammaticTreeTabBean.SelectedRecordAction}" >
      < f:attribute name="nodeName" value="#{node.name}"/ >
      < / af:link >
      < / af:column >


      Ashish

      Delete
    2. Perfect Solution Ashish. Thanks for awesome support

      now i have doubt why selection listener not supporting in this event Now if we choose one node(Link) its Refreshing means just flipping whole tree i dont want this again and again.

      any have Ur support is awesome
      Regards
      Raj

      Delete
    3. Try setting contentDelivery property to immediate

      Ashish

      Delete
  7. Hi Ashish

    As of our previous discussion we Implemented (n) nodes & child tree with POJO

    Now i have a one question i searched and tried so many ways but not success any way

    can we Implement Bread Crumbs Navigation with that tree. is that's possible with our implementation.

    Thanks
    Raj

    ReplyDelete
  8. Hi Ashish,Could you please help me,
    i am beginner to Oracle ADF, Could you tell me how to bind View Objects to ADF Swing Tree Component Programmatically with any simple Single table based View Object as example.

    Thank you in Advance
    Giridhar

    ReplyDelete
    Replies
    1. Girdhar

      I don't have any examples on ADF Swing , You can learn same using ADF Faces

      Ashish

      Delete