4

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?

mtsz
  • 2,725
  • 7
  • 28
  • 41
Jon O
  • 6,532
  • 1
  • 46
  • 57

1 Answers1

2

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) {
}

}
mtsz
  • 2,725
  • 7
  • 28
  • 41
  • 1
    I have long since abandoned this particular project, but thank you for your answer. It looks complete and is easy to follow, so I'm going to give you the benefit of the doubt! :) – Jon O Mar 15 '12 at 21:42
  • Ok, thats nice, thanks for reviewing!... I was working at a project with a similar requirement and implemented it this way. Decided to document the method somewhere and this seemed like a good place :) – mtsz Mar 15 '12 at 22:24
  • Updated link to [IDropActionDelegate documentation](http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_ui_dropActions.html&resultof=%22IDropActionDelegate%22%20%22idropactiondeleg%22) – Max Hohenegger Feb 22 '16 at 09:37