11 Replies Latest reply on Mar 31, 2010 6:24 AM by jsfgeeks

    Drag & Drop between dataTable and Tree

    jsfgeeks

      Hi,

       

       

           I am developing a page in which I have a dataTable with only one column. (e.g. car names). And at the same time I have an empty tree on the same page. What I want to do is, one can drag the car name from the table and can drop to the specific make (of car) in the tree.

       

          I tried it, but might be missing something, and that's why I am getting error as NullPointerException in the dropListener.

       

          I am populating my table with the help of ArrayList<TreeNode<String>>.

       

      e.g.

      while(rs.next()) {

      TreeNode<String> currentNode = null;
      currentNode.setData(rs1.getString("car_name"));
      topics.add(currentNode);

      }

       

      My dropListener is something like below,

       

      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;


      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;


      But it gets nullPointerException for srcNode. I mean, it can't find as a sourceNode.

       

      What can I do to resolve this ?

       

      Thanks in advance,

      JSF GEEKS

        • 1. Re: Drag & Drop between dataTable and Tree
          nbelaevski

          Hi,

           

          You are using rich:dataTable as source, so draggableSource cannot be UITreeNode.

          • 2. Re: Drag & Drop between dataTable and Tree
            jsfgeeks

            Thanks for your support.

             

                 Now I am creating a tree dynamically. (<rich:tree> is defined in my jsp page and I am adding nodes from backbean on create button and removing on Remove button). Now, what I want is, can I have a full tree structure (with ID and values) on one button click, say Save Tree, so that on the save button, I can store it in DB. (different node level in different tables).

             

                I hope my question is clear.

             

            Thanks again,

            JSF GEEKS

            • 3. Re: Drag & Drop between dataTable and Tree
              nbelaevski

              You already have tree structure, so you can walk through it and store nodes in DB. Or am I not getting the problem?

              • 4. Re: Drag & Drop between dataTable and Tree
                jsfgeeks

                Hi nick,

                 

                      Can you give me an example of how to walk through the tree and get the values. I am new to tree.

                 

                Thank you,

                JSF GEEKS

                • 5. Re: Drag & Drop between dataTable and Tree
                  nbelaevski

                  There are two ways of providing data for tree component: using model (TreeNode-based classes) or declarative tags (rich:treeNodeAdaptor & its recursive version). In the first case you have to traverse the model manually (i.e. by calling getChildren()); for the second case you've built the tree from the pre-existing lists, so you can just store them then.

                  • 6. Re: Drag & Drop between dataTable and Tree
                    jsfgeeks

                    Thanks nick,

                     

                          But as I mentioned earlier, I am new to tree. So, can you provide any simple example or a link to the example of it, so that I can understand easily. The function, which I am using to add nodes is as below,

                     

                    public String addNode()
                    {
                    try
                    {
                    //System.out.println("\n\t Adding Node...");
                    outtxt_msg.setValue(null);
                    outtxt_msg.setRendered(false);
                    TreeNodeImpl nodeImpl = new TreeNodeImpl();
                    if (txtTopic == null || txtTopic.length() <= 0)  // txtTopic is a variable, which has a value of inputText
                    {
                    //System.out.println("\n\t Enter node name to create...");
                    outtxt_msg.setValue("Enter node name to create.");
                    outtxt_msg.setRendered(true);
                    return null;
                    }
                    nodeImpl.setData(txtTopic);
                    if (selectedNode == null)
                    {
                    selectedNode = rootNode;
                    }
                    selectedNode.addChild(new Integer(index), nodeImpl);
                    index++;
                    txtTopic = "";



                    }
                    catch (Exception e)
                    {
                    System.out.println("\n\t Error rin adding node...\n\t\t " + e);
                    }
                    return null;
                    }

                     

                         And I am using drag N drop functionality, by using two trees. So can you help some more ?

                     

                    Thank you,

                    JSF GEEKS

                     

                     

                    • 7. Re: Drag & Drop between dataTable and Tree
                      nbelaevski

                      Hmm, I'm not sure what exactly example is necessary, but I'll try:

                       

                       

                      public class TreeBean<T> {
                       
                          TreeNode<T> rootNode;
                          public void saveRoot() {
                              Iterator<Map.Entry<Object, TreeNode<T>>> children = rootNode.getChildren();
                              while (children.hasNext()) {
                                  Entry<Object, TreeNode<T>> entry = children.next();
                       
                                  TreeNode<T> childNode = entry.getValue();
                                  //add data from "childNode" to be saved in DB
                                  //...
                       
                                  saveChildNode(childNode);
                              }
                          }
                       
                          private void saveChildNode(TreeNode<T> childNode) {
                              // TODO Auto-generated method stub
                          }
                      }
                      
                      1 of 1 people found this helpful
                      • 8. Re: Drag & Drop between dataTable and Tree
                        jsfgeeks

                        Thanks Nick for the great help. It works fine.

                         

                                  But, as I told you, I have a dynamic tree creation. so, user can go up to any level. The two functions, which you have suggested, works fine but it's for static (two level) trees only. So if I want to generate a function which can get data from tree of any level, then it will be great for me.

                         

                        Thanks again,

                        JSF GEEKS

                        • 9. Re: Drag & Drop between dataTable and Tree
                          nbelaevski

                          Then you just call this method recursively passing the name of the table according to the level.

                          • 10. Re: Drag & Drop between dataTable and Tree
                            jsfgeeks

                            Thanks Nick,

                             

                                I have tried the recursive function. Now, what I want is, if the tree node is leaf, then after inserting all the leaf of particular  node(parent), then I want to remove that (parent)node from the tree. And for that, I need a row key. So, by using that TreeNode object, how can I remove that (parent)node.

                             

                               How can I get the rowkey of particular TreeNode ?

                             

                            Thanks for great support,

                            JSF GEEKS

                            • 11. Re: Drag & Drop between dataTable and Tree
                              jsfgeeks

                              Hi Nick,

                               

                                    Finally I got the solution by using 2 recursion functions.

                               

                                    Anyway, thanks for your support.

                               

                              JSF GEEKS