Adding Support for Drag and Drop

You can add support for drag-and-drop to your extensions. Drag-and-drop, also known as DnD, is a feature that allows the user to "drag" a portion of an application's user interface somewhere else (usually to another part of the user interface) and "drop" it there. The application's DnD support specifies what can be dragged, where it can be dropped, and what happens when dragging and dropping.

The WebLogic Workshop extensibility API provides IDE-specific wrappings for DnD functionality. These classes and interfaces make it easier to support DnD in the IDE.

Note: A full description of the mechanics of DnD is beyond the scope of this topic. For a general introduction to DnD in Java, see How to Use Drag and Drop and Data Transfer at the Sun web site.

For a sample that illustrates DnD support, see the DragDropSimple Sample.

DnD in WebLogic Workshop

The interesting DnD classes and interfaces are located in the com.bea.ide.core.datatransfer package. To add support for DnD to your extension, you'll typically do the following:

Implementing IDragDropDriver

In some cases, the easiest way to implement IDragDropDriver is to simply extend DefaultDragDropDriver, a default implementation provided in the API. To provide the component-specific data that will be transferred, override the getDragInfo method to return your implementation of IDragDropDriver.IDragSourceInfo. When calling getDragInfo, the IDE will pass in a Component instance representing the component dragged from — the drag source. You can use this instance to retrieve the data (or other component containing the data) that should be transferred by the DnD operation. Using what you know about the drag source to create an instance of your IDragSourceInfo implementation.

For example, the DragDropSimple sample project receives a JTree through getDragInfo, then extracts the currently selected tree node to get the data that should be transferred.

Implementing IDragDropDriver.IDragSourceInfo

Like IDragDropDriver, IDragSourceInfo has a default implementation in the DefaultDragDropDriver.DragSourceInfo class. The interesting method in this interface is getTransferable, which the IDE calls to retrieve a Transferable implementation that contains the data to be transferred. If the data you're transferring is a string, you might want to skip implementing Transferable in your own class and create an instance of StringSelection, which implements the interface to support string transfers. In any case, your code should populate the Transferable implementation with the data to be transferred, then pass it back with getTransferable so that the IDE can handle the rest.

Registering the Component as DnD-able

With DnD driver in hand, you register your component with the IDE for DnD support, passing along an instance of the driver. The following example tells the IDE that a JTree created elsewhere in the code should be registered for DnD support.

DataTransferSvc.get().registerDnDSupport(tree,
    new SimpleTreeDragDropDriver(),
    javax.swing.TransferHandler.COPY,
    true);

Related Topics

DragDropSimple Sample