Pages

Tuesday, November 12, 2013

Drag and Drop Capabilities in Hierarchy Viewer

In this example we are adding Drag and Drop functionality to Hierarchy viewer. 

To create a Hierarchy viewer fallow this post (Hierarchy Viewer and Tree Table Sample).

Modified existing EmployeeAM and added new instance to update employee's manager "UpdateEmployeeVO"..


Created new client interface method to update manager "updateManager".



Added Drag and Drop capabilities to hierarchy viewer.
 

Note: Drag and Drop is based on the "discriminant" attribute.

The discriminant for the default DataFlavors generated by this drag source. The discriminant is used to segregate drags from this drag source. Please note that drag and drop can only be performed between compatible drag sources and drop targets. The discriminant is used for the compatibility purpose. The discriminants of the DataFlavors generated by the default drag source must match the allowed discriminants on the allowed DataFlavors of the drop target.

 To handle the drag and drop created a bean  method.

        Transferable transferable = dropEvent.getTransferable();
       
        Map dropSite = (Map) dropEvent.getDropSite();
        String dropTargetClientRowKey = (String) dropSite.get("clientRowKey");
        if (dropTargetClientRowKey == null) {
            return DnDAction.NONE;
        }
        UIHierarchyViewer hv = (UIHierarchyViewer) dropEvent.getDropComponent();
        CollectionModel hvCollectionModel = (CollectionModel) hv.getValue();
        JUCtrlHierBinding hvTreeBinding = (JUCtrlHierBinding) hvCollectionModel.getWrappedData();
       
        Object dropTargetRowKey =
            hv.getClientRowKeyManager().getRowKey(FacesContext.getCurrentInstance(), hv, dropTargetClientRowKey);
       
        JUCtrlHierNodeBinding targetNode = hvTreeBinding.findNodeByKeyPath((List) dropTargetRowKey);
        Row targetRow = targetNode.getRow();

        DataFlavor<RowKeySet> modelPlanRowKeySetFlavor = DataFlavor.getDataFlavor(RowKeySet.class, "EmployeeHierarchy");
        RowKeySet modelPlanRowKeys = transferable.getData(modelPlanRowKeySetFlavor);
        Row sourceRow = null;
        if (modelPlanRowKeys != null && !modelPlanRowKeys.isEmpty()) {
            Iterator rksIterator = modelPlanRowKeys.iterator();
            while (rksIterator.hasNext()) {
                List key = (List) rksIterator.next();
                if (key.isEmpty()) {
                    continue;
                }
                JUCtrlHierNodeBinding sourceNode = hvTreeBinding.findNodeByKeyPath(key);
                sourceRow = sourceNode.getRow();
            }
        }

Once we identify the source and target we are calling the am method to persist the changes.


Sample UI.

To enable the drag and drop select a node and hold for a second then drag the node and drop on  target.



You can download the sample code.

2 comments:

  1. Hi
    sample code is not available. Please check it.
    I need to sample code. It's useful post
    thanks
    juddi

    ReplyDelete