简体   繁体   English

Eclipse插件-将文本从Java编辑器拖到自定义查看器中始终会得到空数据

[英]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. 我已经在Eclipse上创建了一个自定义视图,并为其定义了一个拖放侦听器。 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. 当我将视图中的项目拖到本机Java编辑器上(使用TextTransfer作为机制)时,文本成功粘贴到了编辑器上。

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. 但是,当我尝试相反的操作时,即当我从Java编辑器中选择一段文本并将其拖到我的视图中时,光标将显示一个无效的符号,并且按预期的方式无法正常工作。 The Drop Target is also set to accept TextTransfer instances. 放置目标也设置为接受TextTransfer实例。

When I have two Java editors open and I drag text from one to the other, it works perfectly. 当我打开两个Java编辑器并将文本从一个拖到另一个时,它可以完美地工作。 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. 我在视图中覆盖了DropTargetAdapter的dragEnter函数,以检查是否已检测到拖动。 After printing the event.datatypes, I can also see the CF_TEXT type being supported. 打印event.datatypes之后,我还可以看到支持CF_TEXT类型。 When I print the event.data, however it is null. 当我打印event.data时,它为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. 经过研究,我意识到有必要在DragEnter函数中手动设置event.detail变量。 After adding the line: 添加行之后:

event.detail = DND.DROP_COPY;

it works now. 现在有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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