How can we get the item on which i am doing the drop on a treeviewer

217 Views Asked by At

I have created a jface treeviewer and i am adding drag and drop of elements into the treeviewer.So the items should be added on the the subchild of a tree.How can i get the subchildname on which i am dropping a element. for eg:

tree->
      A->
        1
        2
      B
      C

so when I drag and drop on 1 it should get the selecteditem as 1 how can we do it.

the code for drop is as follows

int operationsn = DND.DROP_COPY | DND.DROP_MOVE;
        Transfer[] transferType = new Transfer[]{TestTransfer.getInstance()};

        DropTarget targetts = new DropTarget(treeComposite, operationsn);
        targetts.setTransfer(new Transfer[] { TestTransfer.getInstance() });

        targetts.addDropListener(new DropTargetListener() {
            public void dragEnter(DropTargetEvent event) {
                System.out.println("dragEnter in target ");
                if (event.detail == DND.DROP_DEFAULT) {
                    if ((event.operations & DND.DROP_COPY) != 0) {
                        event.detail = DND.DROP_COPY;
                    } else {
                        event.detail = DND.DROP_NONE;
                    }
                }

            }
            public void dragOver(DropTargetEvent event) {
                System.out.println("dragOver in target ");
                event.feedback = DND.FEEDBACK_SELECT | DND.FEEDBACK_SCROLL;

            }
            public void dragOperationChanged(DropTargetEvent event) {
                System.out.println("dragOperationChanged in target ");
                if (event.detail == DND.DROP_DEFAULT) {
                    if ((event.operations & DND.DROP_COPY) != 0) {
                        event.detail = DND.DROP_COPY;
                    } else {
                        event.detail = DND.DROP_NONE;
                    }
                }

            }
            public void dragLeave(DropTargetEvent event) {
                System.out.println("dragLeave in target ");
            }
            public void dropAccept(DropTargetEvent event) {
                System.out.println("dropAccept in target ");
            }


            public void drop(DropTargetEvent event) {
                //if (textTransfer.isSupportedType(event.currentDataType)) 
                    if (event.data != null) {
                        Test tsType = (Test) event.data;
                           addItem(tsType);
                System.out.println("test step name is" +tsType);
                        }




            }

        });

Here in the addItem function I have written the code to add item to the treeviewer on the selecteditem.but while dropping the item I am not able to select the item so how can we selected the item while dropping the elements into the tree.

1

There are 1 best solutions below

0
On BEST ANSWER

When using JFace Viewers you can use the JFace ViewDropAdapter class rather than DropTargetListener.

This class does more work for you and has a getCurrentTarget() method to return the current target element.

More details on this here