Tuesday, October 12, 2010

ADF tree menu code

We have a tree menu that works of a hierarchical structure in the DB. Note you must have seperate iterators for the tree and the view. It will disclose and select an item in your tree based on a selection. This can be modified in various ways to get your trees jumping through hoops.

So here is the code so I dont loose it:
<af:tree value="#{bindings.TreeRootMenuVO2.treeModel}" var="node"
    selectionListener="#{pageFlowScope.TreeBean.selectionListener}"
    rowSelection="single" id="t1"
    binding="#{pageFlowScope.TreeBean.menuTree}"
    contentDelivery="immediate" fetchSize="500">
  <f:facet name="nodeStamp">
    <af:outputText value="#{node}" id="ot1"/>
  </f:facet>
</af:tree>

  <af:commandToolbarButton text="Find Node" id="cb5"
  actionListener="#{pageFlowScope.TreeBean.findInTree}"/>

  <af:commandToolbarButton text="JS Test" id="cb15">
    <af:clientListener method="selectNode" type="click"/>
  </af:commandToolbarButton>
Java code for the selection of an item.
public void selectionListener(SelectionEvent selectionEvent){
  //Invoke standard tree selection listener
  invokeMethod("#{bindings.TreeRootMenuVO2.treeModel.makeCurrent}",
    Object.class, new Class[]{ SelectionEvent.class }, 
    new Object[]{ selectionEvent });
  RichTree rt = this.getMenuTree();
  RowKeySet rks = rt.getSelectedRowKeys();
  BigDecimal menuId = null;

  for (Object facesTreeRowKey : rks){
    rt.setRowKey(facesTreeRowKey);
    JUCtrlHierNodeBinding root = (JUCtrlHierNodeBinding)rt.getRowData();
    menuId = (BigDecimal)root.getRow().getAttribute("MenuId");
  }

  DCIteratorBinding iter = 
    getBindingContainer().findIteratorBinding("MenuVO1Iterator");
  //this method just applies a view criteria containing just the pk to the 
  //iterator then calls  
  //vo.getViewCriteriaManager().removeApplyViewCriteriaName(criteriaName);
  applyCriteriaAndQuery(iter, "MenuVOPKCriteria", createMenuParams(menuId));
  AdfFacesContext.getCurrentInstance().addPartialTarget(formBind);
}

Java code for the find in tree method.
public void findInTree(ActionEvent actionEvent) throws Exception{
  // find current row in the menu iterator
  BigDecimal menuId = retrieveCurrentRowId();
  if (menuId != null){
  JUCtrlHierBinding tree = (JUCtrlHierBinding)
    ((CollectionModel)getMenuTree().getValue()).getWrappedData();
  // Method that gets the hieracical tree for the selected item
  List keyPath = getBranchKeys(menuId);
  if (keyPath != null){
    JUCtrlHierNodeBinding node = tree.findNodeByKeyPath(keyPath);
    List selectedKeyList = node.getKeyPath();
    RowKeySet sRowKeys = menuTree.getSelectedRowKeys();
    sRowKeys.clear();
    sRowKeys.add(selectedKeyList);
    List disclosedKeyList = new ArrayList();
    buildDisclosedRows(node, disclosedKeyList);
    RowKeySet dRowKeys = menuTree.getDisclosedRowKeys();
    dRowKeys.clear();
    if (disclosedKeyList != null && disclosedKeyList.size() > 0){
      for (Object disclosedItemList : disclosedKeyList){
        dRowKeys.add(disclosedItemList);
      }
    }
    AdfFacesContext.getCurrentInstance().addPartialTarget(menuTree);
    }
  }
}

    private void buildDisclosedRows(JUCtrlHierNodeBinding node, List keyList){
        JUCtrlHierNodeBinding parent = node.getParent();
        if (parent != null && parent.getKeyPath() != null){
            buildDisclosedRows(parent, keyList);
        }
        keyList.add(node.getKeyPath());
    }

And just for interest sake some javascript code to work with the menu that does very little but should give you an idea.

<af:resource type="javascript">
  function selectNode(evt) {
    //Get instance of AdfRichTree.js
    var tree = AdfPage.PAGE.findComponent("t1");
    var srks = tree.getSelectedRowKeys();
    var firstRowKey;
    for (rowKey in srks){
      firstRowKey  = rowKey;
      alert("idx:::" + firstRowKey);
      break;
    }

    if (tree.isPathExpanded(firstRowKey)){
      tree.setDisclosedRowKey(firstRowKey,false);
      tree.setDisclosedRowKey(7,true);
      var keys = new Array[1];
      keys[0] = 7;
      tree.getSelectedRowKeys(keys);
      //tree.setValue(7);
    }
  }
</af:resource>
If you need any other code that I have left out just shout.

1 comment:

  1. Please give full sample, I have a lot of problems in it :(

    ReplyDelete