简体   繁体   中英

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.

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 ->

工作拖动

The cursor indicates that I can drop, and when I release the mouse the drop(DropTargetEvent event) method is invoked.

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:

在此输入图像描述

Indicating that the drop is not allowed, and when I release the mouse the drop(DropTargetEvent event) method is NOT invoked.

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.

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. 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.

I took your example and just made the small change to the 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. Since we can't remove files in Read Only Medias such as DVD,

Sender(in this case, OS's shell) will refuse DND communication no matter whether receiver's decision is.

Screen shot of yours shows Moving DND Feedback not Copy .

dropAccept() gives you last chance to modify communication request, Use this carefully, because multiple drop target listeners per each Transfer Type can be exist. To support this case, TreeViewer's DND APIs are slightly different with SWT's one.

DND does not work the way you think it does.

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.

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.

So remember, "TransferData" is different from the actual data.

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.

PS: Clipboard works in the same way.

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