Get the current node of a tree in Oracle ADF | Techartifact

Requirement- To get the current selected node of tree and get value of that node.

We will be writing selectionListener for that tree in managed bean.first we invoke the default tree selection listener with the expression
#{bindings.Departments.treeModel.makeCurrent}. In order to do this, we use a helper method called invokeMethodExpression(). Then, we obtain the currently selected node from the tree by calling getRowData() on the oracle.adf.view.rich.component.rich.data.RichTree component (obtained earlier from the selection event).

    public void treeRowSelectionlistener(SelectionEvent selectionEvent) {
       
        invokeMethodExpression("#{bindings.checklistStructureVO1.treeModel.makeCurrent}", Object.class, SelectionEvent.class, selectionEvent);
        
        RichTree tree = (RichTree)selectionEvent.getSource(); // get the tree component from the event
           TreeModel model = (TreeModel)tree.getValue();    
           //get selected nodes
           RowKeySet rowKeySet = selectionEvent.getAddedSet();
        Iterator rksIterator = rowKeySet.iterator();
        //Validating for single select only. Need to check for multiselect
        while (rksIterator.hasNext()) {
            List key = (List)rksIterator.next();
            JUCtrlHierBinding treeBinding = null;
            CollectionModel collectionModel = (CollectionModel)tree.getValue();
            treeBinding = (JUCtrlHierBinding)collectionModel.getWrappedData();            
            JUCtrlHierNodeBinding nodeBinding = null;
            nodeBinding = treeBinding.findNodeByKeyPath(key);
            Row rw = nodeBinding.getRow();
           
            int seqId = ((BigDecimal)rw.getAttribute("ChecklistStructureId")).intValue(); // We can get the column name value from that node or row. 
            String elementId=((BigDecimal)rw.getAttribute("ElementId")).toString();
            String ChecklistStructureId=((BigDecimal)rw.getAttribute("ChecklistStructureId")).toString();
            String ElementName=(String)rw.getAttribute("ElementName"); 
           
           
            Object result = ob1.execute();
            //System.out.println("XXXXXXXXXXXX"+result.toString());
            if (!ob1.getErrors().isEmpty()){
                List errorList = ob1.getErrors();
                System.out.println("ERROR IN VC EXECUTION");
                // Capture and handle Error
            }   
        
     
    }
    }


        private Object invokeMethodExpression(String expr, Class returnType, Class[] argTypes, Object[] args){
           FacesContext fc = FacesContext.getCurrentInstance(); 
           ELContext elctx = fc.getELContext();
           ExpressionFactory elFactory = fc.getApplication().getExpressionFactory(); 
           MethodExpression methodExpr = elFactory.createMethodExpression(elctx,expr,returnType,argTypes);    
           return methodExpr.invoke(elctx,args); 
        }

Happy coding with Techartifact

get the current viewId or url in ADF using ControllerContext

ControllerContext – Context class for per request per web application Controller information. It have various method when you need to perform
action programmatically .It Provide per-request information about the controller state for a web application.

Use getInstance() method to get the context which includes:

1. ViewPortContext (current view rendered in a browser window, a modal dialog or a region).
2. Global ViewActivity URL (url of a view activity in an unbounded task flow).
3. Local view activity url.
4. Task flow url view activity view id.
5. Savepoint restore url.

getCurrentRootViewPort() -Returns a ViewPortContext corresponding to the current root view port. The root view port is the view port for the current browser window/tab.

getGlobalViewActivityURL(ViewId) – Generate a URL for a view activity in the unbounded task flow.
Note: the view activity being referenced must be in the unbounded task flow. URL access to view activities within bounded task flows is not supported. It is caller’s responsibility to ensure that the view activity exists.

Returns:
a properly encounded URL that will result in navigation to the specified view activity.

getSavePointRestoreURL(savePointId) -Generates a URL to a save point restore activity within the current web application.

getViewActivityViewID(localViewId) -Generate view ID for a view activity inside a task flow definition. The view ID can be used when creating a UIViewRoot representing this view activity so that it can be launched in a dialog window. It is caller’s responsibility to ensure that the view activity exists.

Get the current View Id

 
ControllerContext.getInstance().getCurrentViewPort().getViewId();

Get request URL of a view activity

 
String url = ControllerContext.getInstance().getGlobalViewActivityURL(viewId)

or 

String viewId = "/department"; 
String url = ControllerContext.getInstance().getGlobalViewActivityURL(viewId);

Navigate programmatically among view activities:

 
ControllerContext.getInstance().getCurrentViewPort().setViewId("Department");  //Ensure "Department" is a valid view id in current task flow