简体   繁体   English

为什么我不能从Windows中的CD上删除文件到Eclipse / SWT Drop Target

[英]Why can't I drop files on to Eclipse / SWT Drop Target from a CD in windows

In my Eclipse RCP application I have a TreeViewer that is a drop target for files, it works fine in most circumstances, however when I try and drag files stored on a CD ROM from windows explorer to the node the icon that indicates drops are allowed doesn't change and dropping does nothing. 在我的Eclipse RCP应用程序中,我有一个TreeViewer,它是文件的放置目标,它在大多数情况下工作正常,但是当我尝试将存储在CD ROM上的文件从Windows资源管理器拖到节点时,指示丢弃的图标是允许的改变和放弃什么都不做。

Since people seemed confused about my question here's a more detailed explanation: 由于人们似乎对我的问题感到困惑,这里有一个更详细的解释:

When executing the code below (supplied by Baz), I am able to drag files and drop them on to the text box, when I drag a file from most locations on my machine, the window appears like this -> 当执行下面的代码(由Baz提供)时,我能够拖动文件并将其拖放到文本框中,当我从我的机器上的大多数位置拖动文件时,窗口显示如下 - >

工作拖动

The cursor indicates that I can drop, and when I release the mouse the drop(DropTargetEvent event) method is invoked. 光标表示我可以删除,当我释放鼠标时,调用drop(DropTargetEvent event)方法。

Now when I do the same thing, but take a file from explorer that is on a DVD in my optical drive then it looks like this: 现在,当我做同样的事情,但从我的光驱中的DVD上的资源管理器中获取文件时,它看起来像这样:

在此输入图像描述

Indicating that the drop is not allowed, and when I release the mouse the drop(DropTargetEvent event) method is NOT invoked. 指示不允许丢弃,并且当我释放鼠标时,不调用drop(DropTargetEvent event)方法。

It's also worth noting that I am able to drop the same DVD files in to a folder in the eclipse navigator, indicating that this isn't a machine specific issue, there must be something different in the eclipse trees that allows it but I can't see it. 还值得注意的是,我能够将相同的DVD文件放入eclipse导航器中的文件夹中,表明这不是机器特定的问题,在日食树中必须有不同的东西允许它,但我可以'看到了。

package widgets;

import java.util.Arrays;

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.PluginTransfer;

public class SourceTest {
    public static void main(String[] args) {
        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1,false));

        final Text text = new Text(shell, SWT.BORDER);
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));

        DropTarget dt = new DropTarget(text, DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK);
        dt.setTransfer(new Transfer[] { FileTransfer.getInstance(), PluginTransfer.getInstance() });
        dt.addDropListener(new DropTargetAdapter() {
            public void drop(DropTargetEvent event) {
                String fileList[] = null;
                FileTransfer ft = FileTransfer.getInstance();
                if (ft.isSupportedType(event.currentDataType)) {
                    fileList = (String[]) event.data;
                }
                System.out.println(Arrays.toString(fileList));
            }
        });

        shell.setSize(300, 100);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}

It appears that not all of the DND operations possible are supported when coming from a CDROM device. 看来,当来自CDROM设备时,不支持所有可能的DND操作。 Therefore you need to implement a couple of more methods on the DropTargetAdapter in order to modify the Drop operation while in progress to narrow down the operations that will actually be performed so that the OS doesn't prevent the drop. 因此,您需要在DropTargetAdapter上实现更多方法,以便在进行中修改Drop操作以缩小实际执行的操作范围,以便操作系统不会阻止丢弃。

I took your example and just made the small change to the DropTargetAdapter 我举了你的例子,只是对DropTargetAdapter做了一些小改动

    @Override
    public void dropAccept( DropTargetEvent event )
    {
        Object object = FileTransfer.getInstance().nativeToJava( event.currentDataType );
        if( object instanceof String[] )
        {
            String[] strs = (String[]) object;
            System.out.println("dropAccept " + strs[0]);
            event.detail = DND.DROP_COPY;
        }
        event.detail = DND.DROP_COPY;
    }
    @Override
    public void dragEnter( DropTargetEvent event )
    {
        event.detail = DND.DROP_COPY;
    }

I think updated your question clarifies what problem is. 我认为更新了你的问题澄清了什么问题。

Your Drop Target used DND_MOVE as Flag. 你的掉落目标使用DND_MOVE作为旗帜。 Since we can't remove files in Read Only Medias such as DVD, 由于我们无法删除只读媒体(如DVD)中的文件,

Sender(in this case, OS's shell) will refuse DND communication no matter whether receiver's decision is. 无论接收者是否决定,发送者(在这种情况下,OS的shell)都将拒绝DND通信。

Screen shot of yours shows Moving DND Feedback not Copy . 您的屏幕截图显示移动 DND反馈而不是复制

dropAccept() gives you last chance to modify communication request, Use this carefully, because multiple drop target listeners per each Transfer Type can be exist. dropAccept()为您提供修改通信请求的最后机会,请谨慎使用,因为每个Transfer Type可以存在多个drop target侦听器。 To support this case, TreeViewer's DND APIs are slightly different with SWT's one. 为了支持这种情况,TreeViewer的DND API与SWT的略有不同。

DND does not work the way you think it does. DND不像您认为的那样工作。

Consider downloading some file from web . 考虑从网上下载一些文件 When you validate downloading , there is no actual file content but you have the only URL of a web resource. 验证下载时没有实际的文件内容,但您拥有Web资源的唯一URL

Likewise, when you validate drop operation, you can't know the actual data, because there is no established connection until you accept the drop operation. 同样,当您验证放置操作时,您无法知道实际数据,因为在您接受放置操作之前没有建立连接 The only thing you can know in the validating phase is the kind of transfer type . 您在验证阶段唯一可以知道的是传输类型

So you can only validate the drop based on the content kind, not the "Actual Content" 因此,您只能根据内容种类而不是“实际内容”来验证丢弃

Typical Validation codes are: 典型的验证码是:

public boolean validateDrop(...){
    return FileTransfer.getInstance().isSupportedType(transferData);
}

After you accept "TransferData", the transfer will retrieve the actual data from the connection. 接受“TransferData”后,传输将从连接中检索实际数据。

So remember, "TransferData" is different from the actual data. 所以请记住,“TransferData”与实际数据不同。

The FileTransfer retrieves the files in the forma of a String array which contains the absolute path to each file, not the Java File object. FileTransfer检索String数组格式的文件,该数组包含每个文件的绝对路径,而不是Java File对象。

PS: Clipboard works in the same way. PS:剪贴板以相同的方式工作。

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

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