Eclipse Plugin – Dragging text from Java editor onto custom viewer always gets null data

I have created a custom view on Eclipse and defined a drag and drop listener for it. When I drag items from my view onto the native Java editor (using TextTransfer as the mechanism), the text successfully gets pasted on the editor.

However, when I try to do the opposite i.e. when I select a piece of text from the Java editor and drag it to my view, then the cursor shows an invalid sign and the drop doesn’t work as expected. The Drop Target is also set to accept TextTransfer instances.

When I have two Java editors open and I drag text from one to the other, it works perfectly. Why does it not work when I drag the text onto my view?

I overrode the dragEnter function of the DropTargetAdapter in my view to check if the drag was being detected and it was. After printing the event.datatypes, I can also see the CF_TEXT type being supported. When I print the event.data, however it is null. Why?

Code is below:

viewDropTargetAdapter = new DropTargetAdapter()
    {
        @Override
        public void drop(DropTargetEvent event)
        {
            addCodeSnippetAction.run();
        }
        @Override
        public void dragEnter(DropTargetEvent event)
        {
            System.out.println("DATATYPE: " + event.currentDataType);
            System.out.println("DATA: " + event.data);
            System.out.println("DETAIL: " + event.detail);
            TransferData[] td = event.dataTypes;
            for(int i=0; i<td.length; i++)
            {
                System.out.println("Datatype of " + i + " is: " + td[i].type + " and " +getNameFromId(td[i].type));
            }
            super.dragEnter(event);
        }
    };
    viewDropTarget = new DropTarget(viewer.getControl(), DND.DROP_COPY);
    viewDropTarget.setTransfer(new Transfer[] {TextTransfer.getInstance()});
    viewDropTarget.addDropListener(viewDropTargetAdapter);

Output is below:

DATATYPE: [email protected]
DATA: null
DETAIL: 0
Datatype of 0 is: 13 and CF_UNICODETEXT
Datatype of 1 is: 1 and CF_TEXT

Answer

After some researching, I realized that it is necessary to set the event.detail variable manually in the DragEnter function. After adding the line:

event.detail = DND.DROP_COPY;

it works now.

Leave a Reply

Your email address will not be published. Required fields are marked *