简体   繁体   中英

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 ie 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: org.eclipse.swt.dnd.TransferData@77f5c2c7
DATA: null
DETAIL: 0
Datatype of 0 is: 13 and CF_UNICODETEXT
Datatype of 1 is: 1 and CF_TEXT

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM