Creating a Drag Source
With the DragSource
class, you can make any component draggable by the user, and configure the drag operation. By default, the entire component is made draggable, instead of a small part of it. The DragSource
class is a configuration object for the dragged item and contains static methods for configuring the given component instance.
Div box1 = new Div();
Div box2 = new Div();
// Make box 1 draggable and store reference to the configuration object
DragSource<Div> box1DragSource = DragSource.create(box1);
// Access box 2 drag related configuration object without making it draggable
DragSource<Div> box2DragSource = DragSource.configure(box2);
// Make box 2 draggable
box2DragSource.setDraggable(true);
add(box1, box2);
The DragSource
configuration object doesn’t itself store any data, as it’s a convenience proxy that facilitates making a component draggable. Creating a new DragSource
instance of a component doesn’t reset any previous configuration, but any changes override the previous configuration.
When a component is set draggable on the server side, the draggable
attribute is assigned to its topmost element on the browser, making it draggable. When the user starts to drag the component, the root element of the component gets the v-dragged
class name in the browser. This can be used to highlight the dragged component to the user.
.v-dragged {
outline: 1px dotted hotpink;
opacity: 0.5;
}
Exposing the Drag Source API on your Component
As the DragSource
is an interface, it can also be used as a "mix-in interface", which gives an easy way to add its API to your custom component.
This useful when you want to reuse the component in many places for drag operations.
public class CardComponent extends Div implements DragSource<CardComponent>, HasStyle {
public CardComponent() {
// all cards are draggable by default
setDraggable(true);
}
// all DragSource methods have default implementations
}
Assigning Server-Side Data to the Drag Source
It’s possible to set any Java object as the server-side drag data for the drag
source.
This data is provided on the DropEvent
when the drop occurs on a valid drop target, if it’s inside the same UI (browser window / tab) as the drag source component.
// continuing from the previous example, CardComponent implements DragSource
CardComponent card1 = new CardComponent();
CardComponent card2 = new CardComponent();
card1.setDragData("Queen of Hearts");
card2.setDragData(new Card(11, Land.Spade)); // the data can be any object
The data isn’t sent to the browser, as it’s stored in the component instance as server-side-only data.
It isn’t possible to configure the client-side drag data, the dataTransfer
object, from the server side.
Drag Start and End Events
The DragSource
provides a way to react when the user starts and stops dragging a component.
The DragStartEvent
is fired when the drag starts in the browser, which means that you can’t cancel the drag.
You should avoid doing any heavy synchronous processing there, since this blocks the user from dragging the component further in the browser.
This has a negative effect on the UX.
// Continuing from the previous example with CardComponent
card1.addDragStartListener(event -> {
// Highlight suitable drop targets in the UI
getVisibleCards().forEach(target -> {
Card targetCard = target.getCard();
if (targetCard.getLand() == ((Card) card1.getDragData()).getLand()
&& target != card1) {
target.addClassName("possible-drop-zone");
}
})
});
When the user stops dragging the component by either dropping it or by canceling the drag with, for example, the escape key, the DragEndEvent
is fired.
The isSuccessful()
method returns true
if the drop occurred on a drop target that accepted the drop, but only for Chrome and Firefox (read the note after the sample).
card1.addDragEndListener(event -> {
getVisibleCards().forEach(target -> target.removeClassName("possible-drop-zone"));
// NOTE: The following is always FALSE for Edge and Safari !!!
if (event.isSuccessful()) {
// Better to put logic for successful drop into DropEvent for the
// DropTarget because of the above
}
});
Note
|
Handling drag end in Edge and Safari
Edge and Safari don’t report whether the drop occurred successfully or not in the DragEnd event.
You need to take this into account if your users are using any of these browsers, and do any logic in the DropEvent handler of the DropTarget instead.
For Chrome and Firefox, it works correctly.
|
The Effect Allowed and Drop Effect
It’s possible to customize the effectAllowed
for the drag source.
This has an effect on what the browser visually presents to the user and should match what’s set in the drop target as the dropEffect
.
The DragEndEvent
reports the dropEffect
for the drop event.
The value is determined in priority order by:
-
the desired action
dropEffect
set by the drop target; -
the
effectAllowed
set for the drag source; -
the modifier keys the user presses and holds when dropping.
When the drop effect is MOVE
, you should move / remove the drag source component from its original location.
When the drop effect is NONE
, the drop didn’t occur and dropEvent.isSuccessful()
returns false
.
Customizing the Draggable Element
You can customize the element that is made draggable by overriding the getDraggableElement()
method in the DragSource
interface.
This is useful if it isn’t the whole component that’s to be draggable, but only a part of it.
/* NOTE: RouteItem is a made up custom component, not a core Vaadin component. */
public class DraggableRouteItem extends RouteItem implements DragSource<RouteItem> {
private Icon dragHandle = VaadinIcon.MENU.create();
public DraggableRouteItem(String destination) {
super(destination);
add(dragHandle);
}
// Instead of allowing the whole item to be draggable, only allow dragging
// from the icon.
@Override
public Element getDraggableElement() {
return dragHandle.getElement();
}
}
Changing the draggable element also changes the drag image that the browser shows under the cursor. HTML 5 has an API for setting a custom drag image element, but it isn’t yet available from the server-side API, because it works unreliably in some browsers (Edge / Safari).
4FFD51BA-4736-44BD-8FCF-0E534A19FB8D