0
votes

I would like to implement drag and drop on a Nebula Grid. I found the listener DragDetectListener but I do not know how to find the target (which is where I want to release the row). I can not use a SWT Table since I must use a Nebula Grid.

To be more clear:

I have a Nebula Grid with N rows and I would like to drag one of the rows between two other rows. I know the row that I have moved. How can I get the row dropped on?

1
Here is an article that explains how DnD works in SWT: eclipse.org/articles/Article-SWT-DND/DND-in-SWT.htmlRüdiger Herrmann
@AndreaVicari If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older posts which still don't have answers.Rüdiger Herrmann

1 Answers

1
votes

Drag and Drop should involve two listeners. One on the component that start the drag, and an other one on the component where the drop is done.

source.addDragListener(new DragSourceListener() {
   public void dragStart(DragSourceEvent event) {
      // Only start the drag if needed
      if (iDoNotNeedToStartTheDrag) {
          event.doit = false;
      }
   }
   public void dragSetData(DragSourceEvent event) {
     // Provide the data of the requested type.
     if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
          event.data = "the data to transfert";
     }
   }
   public void dragFinished(DragSourceEvent event) {
     // At the end of the drag, if we need to do something on the source
   }
});

Then on the target :

target.addDropListener(new DropTargetListener() {
    public void dragEnter(DropTargetEvent event) {
    }
    public void dragOver(DropTargetEvent event) {
    }
    public void dragOperationChanged(DropTargetEvent event) {
    }
    public void dragLeave(DropTargetEvent event) {
    }
    public void dropAccept(DropTargetEvent event) {
    }
    public void drop(DropTargetEvent event) {
        // do what ever you want...
        if (textTransfer.isSupportedType(event.currentDataType)) {
            String text = (String)event.data;
            TableItem item = new TableItem(dropTable, SWT.NONE);
            item.setText(text);
        }
        if (fileTransfer.isSupportedType(event.currentDataType)){
            String[] files = (String[])event.data;
            for (int i = 0; i < files.length; i++) {
                TableItem item = new TableItem(dropTable, SWT.NONE);
                item.setText(files[i]);
            }
        }
    }
});