Implement Drag and Drop in Your Web Apps - Part 2/Page 2
Implement Drag and Drop in Your Web Apps - Part 2 [con't]
You may wonder why
MousePanel. Here's why: Not all GWT widgets support mouse listeners; in fact, most do not, and we want to be able to drag any GWT component. So we wrap widgets in a mouse panel, which does support mouse listeners. Unbeknownst to users of the dnd module, they are really dragging mouse panels, which contain a single widget. We used this same technique in The Viewport's Use of a Focus Panel: Revisited" (page 115). See that section for more information about mouse panels and mouse listeners.
DragSource class adds two mouse listeners to the widget that it wraps. The first listener, an instance of
DragSourceListener, which is listed in Listing 6.15 on page 192, monitors the drag and invokes the abstract methods defined by the
DropTarget classes at the appropriate times.
The second listener, by default, is an instance of
FollowsMouseDragger, which is listed in Listing 6.14 on page 191. That implementation of the
MouseListener interface drags the drag source wherever the mouse goes. Notice that the mouse listener—an instance of
DragSource subclasses can override
getMouseListener() to provide a different dragger.
Oh, one more thing: The order in which we add listeners is significant because that is the order in which GWT will invoke them. (That's a big improvement over the Abstract Window Toolkit (AWT), which does not guarantee the order in which listeners are invoked.) For the drag-and-drop module to function properly, the drag source listener must be added first because the
onMouseUp method turns into a no-op if the drag source is not being dragged (we don't want the drag source listener to react to mouse up events if the drag source is not being dragged). Because
AbstractMouseDragger.onMouseUp() sets the drag source's dragged property to false, that method must be called after the
DragSourceListener.onMouseUp(). If you reverse the order of the addition of the mouse listeners, you will see that the drag-and-drop module never reacts to mouse up events.
The DropTarget class is listed in Listing 6.12.
Â void onLoad()
The GWT calls this method when a widget's DOM element is attached to the browser's DOM tree. The onLoad method is a protected method in the Widget class, so it is available for overriding by subclasses, but you cannot call it directly outside a widget subclass. The
onLoad method is overridden in the drag-and-drop module discussed in this section by the
DragSource class to make sure that the drag source's parent widget is an instance of
This is another extension of MousePanel because we want any GWT widget to be able to function as a drop target. This class provides no-op defaults for two of the three methods that subclasses are likely to override: dragSourceEntered() and dragSourceExited().
For dragSourceDropped(), if the drag source is acceptable to the drop target—indicated by the return value of acceptsDragSource(), which is an abstract method subclasses must implement—we tell the drag source that it was accepted by the drop target; otherwise, we notify the drag source that, sadly enough, it was rejected by the drop target.