Search code examples
eclipseeclipse-plugindrag-and-dropeclipse-pde

Handling drag and drop of files inside Eclipse Package Explorer


I'm trying to create an Eclipse plugin to support a proprietary project file format. My goal is to be able to drag and drop a file in the Project Explorer (any type of file) onto a file of the type I support, and have the name of the file being dragged appended to the end of the proprietary file.

Right now, I have a custom editor that can parse out some data from an existing file in a manageable way. This means that I have an editor associated with the file type, such that my special icon shows up next to it. I don't know if that's relevant.

I'm attempting to use the extension point "org.eclipse.ui.dropActions" but I'm not sure how to register my DropActionDelegate (implements org.eclipse.ui.part.IDropActionDelegate) such that it will be called when a file is dropped onto one of my type within the Project Explorer.

Anybody have any ideas? Am I even on the right track with the DropActionDelegate?


Solution

  • You are on the right track implementing an IDropActionDelegate:

    class DropActionDelegate implements IDropActionDelegate {
    
        @Override
        public boolean run(Object source, Object target) {
            String transferredData (String) target; // whatever type is needed  
            return true; // if drop successful
        }
    }
    

    The purpose of the extension point org.eclipse.ui.dropActions is to provide drop behaviour to views which you don't have defined yourself (like the Project Explorer).

    You register the drop action extension like this:

    <extension point="org.eclipse.ui.dropActions"> 
            <action 
                id="my_drop_action" 
                class="com.xyz.DropActionDelegate"> 
            </action> 
    </extension>
    

    Don't forget to attach an adequate listener to your editor in your plugin code:

    class DragListener implements DragSourceListener {
    
    @Override
    public void dragStart(DragSourceEvent event) {
    }
    
    @Override
    public void dragSetData(DragSourceEvent event) {
        PluginTransferData p;
        p = new PluginTransferData(
            "my_drop_action", // must be id of registered drop action
             "some_data" // may be of arbitrary type
            );
        event.data = p;
    }
    
    @Override
    public void dragFinished(DragSourceEvent event) {
    }
    
    }