Showing posts with label RowKeySet. Show all posts
Showing posts with label RowKeySet. Show all posts

Friday, 21 October 2011

ADF Tree : Row Disclosure Listener which expands below the disclosed node

I recently had the scenario where more than one level of a tree node had to be expanded on the disclosure of a particular tree node.

For example, say you have the below tree:
-A
-B
  - B1
  - B2
    - B2a
    - B2b
  - B3
-C
then, on the disclosure of tree node B you would want all the children (B1, B2, B3, B2a, B2b) of tree node B to be expanded.

I've done it as follow. I've added the following property to my ADF Tree:
rowDisclosureListener="#{treeBean.discloseTreeNodeChildren}" 

Then I implemented the referenced method in my "treeBean" backing bean. It assumes a tree binding called: "tree." See the listing below:
public void discloseTreeNodeChildren(RowDisclosureEvent event) {
    RowKeySet addedKeys = event.getAddedSet();
    Iterator addedIt = addedKeys.iterator();
    if(addedIt.hasNext()) {
        Object addedKey = addedIt.next();
        TreeModel treeModel = (TreeModel) this.tree.getValue();
        JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding)
               treeModel.getRowData(addedKey);
        String isLeaf = (String) nodeBinding.getAttribute("isLeaf");
       
          // you have to have a condition to break the series of disclosure events!!
        if(!(new Boolean(isLeaf))) {
            TreeUtility.expandAllNodes(nodeBinding, this.tree.getDisclosedRowKeys(), 0, 1);
            AdfFacesContext.getCurrentInstance().addPartialTarget(this.tree);
        }
    }
}

Now the utility method called: "expandAllNodes()" I've implemented as such:
protected static void expandAllNodes(JUCtrlHierNodeBinding nodeBinding, RowKeySet
          disclosedKeys) {   
    List<JUCtrlHierNodeBinding> childNodes = (List<JUCtrlHierNodeBinding>)
        nodeBinding.getChildren();     
    ArrayList newKeys = new ArrayList();
    if(childNodes != null) {
        for(JUCtrlHierNodeBinding node : childNodes) {           
            newKeys.add(node.getKeyPath());
            // recursive call to the method expandAllNodes()         
            expandAllNodes(node, disclosedKeys);
        }
    }
    disclosedKeys.addAll(newKeys);     

}
A handy addition to the utility method above is to add currentLevel and targetLevel integers in order to apply a boundary to the expansion of nodes. This gives the ability to only expand n levels below the disclosed node...comes in handy in those "big oak trees!!" :-)

Thursday, 29 September 2011

ADF TreeModel utility methods

In all my latest dealings with the RichTree component in ADF I've found the following three (3) utility methods to be very handy (let's say they were implemented in a utility class called: Utility):

  // Returns the last RowKey object from the given set
  private static Object lastRowKeyFromSet(RowKeySet set) {
    Iterator it = set.iterator();
    Object rowKeyPointer = null;
    while (it.hasNext()) {
      rowKeyPointer = it.next();
    }
    return rowKeyPointer;
  }




  // Returns a String value of the given node attribute as found in the specified TreeModel at the specified row index.
  public static String retrieveNodeAttributeFromRowIndexForTreeModel(String attributeName, int rowIndex,
    TreeModel treeModel) {
    String attr = null;

    if(rowIndex > -1) {
      Object rowData = treeModel.getRowData(rowIndex);
      JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding) rowData;

      if(nodeBinding != null) {
        attr = (String) nodeBinding.getAttribute(attributeName);
      }
    }
    return attr;
  }


  // Returns a String value of the given node attribute as found in the specified TreeModel at the last row key
  // as per the specified row key set.
  public static String retrieveNodeAttributeFromRowKeySetForTreeModel(String attributeName, RowKeySet set,
    TreeModel treeModel) {
    String attr = null;

    if ((set != null) && (set.size() > 0)) {     
      Object currentRowKey = Utility.lastRowKeyFromSet(set);     
      JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding) treeModel.getRowData(currentRowKey);     
      if (nodeBinding != null) {       
        attr = (String) nodeBinding.getAttribute(attributeName);       
      }
    }   
    return attr;
  }


Hope you find it handy!