Using NB 6.7 RCP, implementing a drag/drop operation on a TopComponent in editor mode is relatively easy, e.g:
- Create a custom transfer handler, which will inherit from java.awt.datatransfer.TransferHandler.
- assign an instance of the new transfer handler to the TopComponent (best place to do so is probably in the componentShowing method inherited from TopComponent).
- at runtime dragging an item over the topComponent will now fire the methods canImport, ImportData of the Custom transfer handler.
Dnd on an *empty* editor area proved to be much trickier to solve.. an empty area by definition is not covered by a topComponent therefore there’s no “handle” to attach a TransferHandler too… After many unsuccessful trials (and growing frustration) I finally stumbled upon the ExternalDropHandler class…
From the javadoc:
When an implementation of this class is available in the global Lookup and an object is being dragged over some parts of the main window of the IDE then the window system may call methods of this class to decide whether it can accept or reject the drag operation. And when the object is actually dropped into the IDE then this class will be asked to handle the drop.
- ExternalTransferHandler being abstract the first thing to do is to create a new class, CustomExternalDropHandler, where the canDrop/handleDrop methods of ExternalDropHandler (which mirror canImport/importData of TransferHandler) will be implemented.
- Then register CustomExternalDropHandler in the global lookup: add the following lines to your layer.xml
<folder name=”Services”> <folder name=”Hidden”> <file name=”ExternalDropHandler.instance”> <attr name=”instanceClass” stringvalue=”org.packagename.CustomExternalDropHandler”/> <attr name=”instanceOf” stringvalue=”org.openide.windows.ExternalDropHandler”/> </file> </folder> </folder>And that’s it – at runtime canDrop, handleDrop will fire whenever an item is dragged over an empty area of the editor space.