3 Replies Latest reply on Dec 18, 2008 4:50 AM by Ilya Shaikovsky

    Drag and Drop feature of Rich Tree

    Narin Kalra Newbie

      Hi Friends,

      I am using Seam for developing my application. In one scenario i need to use rich:tree component with drag and drop behavior.

      But i am getting problem with droplistener method. As it is giving the null pointer exception. Could you please send me some demo of how to implement this feature into my application.

      Following is the code used by me for this

      public void dropListener(DropEvent dropEvent) {
      
      
      
       // resolve drag destination attributes
      
       UITreeNode destNode = (dropEvent.getSource() instanceof UITreeNode) ? (UITreeNode) dropEvent.getSource() : null;
      
      
      
      
       UITree destTree = destNode != null ? destNode.getUITree() : null;
      
       TreeRowKey dropNodeKey = (dropEvent.getDropValue() instanceof TreeRowKey) ? (TreeRowKey) dropEvent.getDropValue() : null;
      
       TreeNode droppedInNode = dropNodeKey != null ? destTree.getTreeNode(dropNodeKey) : null;
      
      
      
      
      
      
       // resolve drag source attributes
      
       UITreeNode srcNode = (dropEvent.getDraggableSource() instanceof UITreeNode) ? (UITreeNode) dropEvent.getDraggableSource() : null;
      
       UITree srcTree = srcNode != null ? srcNode.getUITree() : null;
      
      
      
      
       TreeRowKey dragNodeKey = (dropEvent.getDragValue() instanceof TreeRowKey) ? (TreeRowKey) dropEvent.getDragValue() : null;
      
       TreeNode draggedNode = dragNodeKey != null ? srcTree.getTreeNode(dragNodeKey) : null;
      
       if (dropEvent.getDraggableSource() instanceof UIDragSupport && srcTree == null && draggedNode == null && dropEvent.getDragValue() instanceof TreeNode) {
      
      
      
      
       srcTree = destTree;
      
       draggedNode = (TreeNode) dropEvent.getDragValue();
      
       dragNodeKey = srcTree.getTreeNodeRowKey(draggedNode) instanceof TreeRowKey ? (TreeRowKey) srcTree.getTreeNodeRowKey(draggedNode) : null;
      
      
      
      
       }
      
      
      
       // Note: check if we dropped node on to itself or to item instead of
      
       // folder here
      
       if (droppedInNode != null && (droppedInNode.equals(draggedNode) || droppedInNode.getParent().getParent() != null || draggedNode.getParent().getParent() == null)) {
      
      
      
      
       System.out.println("Warning: Can't drop on itself or to pic itself! Also can't move folders");
      
       return;
      
       }
      
      
      
       FacesContext context = FacesContext.getCurrentInstance();
      
      
      
       if (dropNodeKey != null) {
      
      
      
      
       // add destination node for rerender
      
       destTree.addRequestKey(dropNodeKey);
      
      
      
       Object state = null;
      
       if (dragNodeKey != null) { // Drag from this or other tree
      
       TreeNode parentNode = draggedNode.getParent();
      
      
      
      
       // 1. remove node from tree
      
       state = srcTree.removeNode(dragNodeKey);
      
       // 2. add parent for rerender
      
       Object rowKey = srcTree.getTreeNodeRowKey(parentNode);
      
       srcTree.addRequestKey(rowKey);
      
       if (dropEvent.getDraggableSource() instanceof UIDragSupport) {
      
      
      
      
       selectedNodeChildren.remove(draggedNode);
      
       // if node was gragged in it's parent place dragged node to
      
       // the end of selected nodes in grid
      
       if (droppedInNode.equals(parentNode)) {
      
       selectedNodeChildren.add(draggedNode);
      
      
      
      
       }
      
       }
      
       } else if (dropEvent.getDragValue() != null) { // Drag from some
      
       // drag source
      
       draggedNode = new DemoTreeNodeImpl<String>();
      
       draggedNode.setData(dropEvent.getDragValue().toString());
      
      
      
      
       }
      
      
      
       // generate new node id
      
       Object id = getNewId(destTree.getTreeNode(dropNodeKey));
      
       destTree.addNode(dropNodeKey, draggedNode, id, state);
      
       }
      
      
      
       AjaxContext ac = AjaxContext.getCurrentInstance();
      
      
      
      
       // Add destination tree to reRender
      
       try {
      
       ac.addComponentToAjaxRender(destTree);
      
       } catch (Exception e) {
      
       System.err.print(e.getMessage());
      
       }
      
      
      
       }
      


      Thanks,
      Narin