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

ADF sort of generic screen for tables with the same structure

We have a couple (about a hundred) of tables with the same structure (Code, Description, Create Date, Update Date). So I wanted to do something simple so that I did not have to create all these screens 1) EO   I created the EO based on one of the tables I had that had the above columns. I then Added a transient attribute called table name to my EO based on a groovy expression. (the expression needs to change as I am reading web tier stuff from the model layer but I will fix this later) I then generated a java class for my EO. And added the following overriden method to my newly created java class. protected StringBuffer buildDMLStatement(int i, AttributeDefImpl[] attributeDefImpl,   AttributeDefImpl[] attributeDefImpl2, AttributeDefImpl[] attributeDefImpl3, boolean b) {   StringBuffer statement = super.buildDMLStatement(   i, attributeDefImpl, attributeDefImpl2, attributeDefImpl3, b); return new StringBuffer(StringUtils.replace(statement.to...

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...

MANIFEST.MF merge JDeveloper for an executable jar

Goto your project > properties. Then click on deployment in the menu. Edit or add a jar deployment profile. Fill in the details under jar options (select Include manifest and give it a main class name) Also remember that the merge functionality only works with a BLANK line at the end of the merge file. REALLY this caught me. My merge file contents: Class-Path: commons-codec-1.3.jar [...empty line here CRLF...]