Skip to main content

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.

Comments

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

    ReplyDelete

Post a Comment

Popular posts from this blog

OJDeploy: Documentation for the tool

Real DOCS:  http://docs.oracle.com/cd/E26098_01/user.1112/e17455/deploying_apps.htm#OJDUG645 OJDeploy Documentation if you run it from the command line - I keep looking for this so I though I would post it here so I remeber. Oracle JDeveloper Deploy 11.1.2.1.0.6081 Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. Usage:   ojdeploy -profile <name> -workspace <jws> [ -project <name> ] [ <options> ]   ojdeploy -buildfile <ojbuild.xml> [ <options> ]   ojdeploy -buildfileschema Arguments:   -profile               the name of the Profile to deploy   -workspace      full path to the JDeveloper Workspace file(.jws)   -project              name of the JDeveloper Project within the .jws where the Profile can be...

Util code

public static MethodExpression getMethodExpression( String expr, Class returnType, Class[] argTypes){ FacesContext fc = FacesContext.getCurrentInstance(); ELContext elctx = fc.getELContext(); ExpressionFactory elFactory = fc.getApplication().getExpressionFactory(); return elFactory.createMethodExpression( elctx, expr, returnType, argTypes); } public static javax.faces.el.MethodBinding getMethodBinding( String expr, Class[] argTypes){ FacesContext fc = FacesContext.getCurrentInstance(); ELContext elctx = fc.getELContext(); return fc.getApplication().createMethodBinding(expr, argTypes); } SetPropertyListener listener = new SetPropertyListener( ActionEvent.class.getName()); listener.setFrom(link.getRoute()); listener.setValueExpression("to", JSFUtils.getValueExpression("#{pageFlowScope.route}", String.class)); action.addActionListener(listener); AdfFacesContext.getCurrentInstance().getPageFlowScope() .put("route", lin...

ADF: LOV Description instead of code

I keep on forgetting how to do this so this is a note to self in terms I understand (ie pictures): Add the related entiy object to yours (in the example we are adding RegionEO(list of values) to CountryEO) Now add the RegionName field from the EO and a transient attribute (I named mine RegionNameLOV) Make the transient attribute updateable and base it on the expression RegionName (the description you wish to display) Add a list of values to RegionNameLOV and map BOTH key to parent fk (region id here) AND RegionName to your transient coulmn (RegionNameLOV).  Map the transient as an Input text with List of Values. Then just drag the lov item onto the page (RegionNameLOV)