Andrea Vicari
Andrea Vicari

Reputation: 3

Drag and Drop on SWT Grid

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?

Upvotes: 0

Views: 587

Answers (1)

fluminis
fluminis

Reputation: 4039

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]);
            }
        }
    }
});

Upvotes: 1

Related Questions