Edgblog

July 15, 2009

Netbeans RCP 6.7 – Drag and drop on the editor area

Filed under: java — edgblog @ 6:55 am

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.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.