@Override
public void drop(final DragAndDropEvent dropEvent) {
final Transferable transferable = dropEvent.getTransferable();
final Component sourceComponent = transferable.getSourceComponent();
if (sourceComponent instanceof WrappedComponent) {
final TargetDetails dropTargetData = dropEvent
.getTargetDetails();
final DropTarget target = dropTargetData.getTarget();
// find the location where to move the dragged component
boolean sourceWasAfterTarget = true;
int index = 0;
final Iterator<Component> componentIterator = layout
.getComponentIterator();
Component next = null;
while (next != target && componentIterator.hasNext()) {
next = componentIterator.next();
if (next != sourceComponent) {
index++;
} else {
sourceWasAfterTarget = false;
}
}
if (next == null || next != target) {
// component not found - if dragging from another layout
return;
}
// drop on top of target?
if (dropTargetData.getData("horizontalLocation").equals(
HorizontalDropLocation.CENTER.toString())) {
if (sourceWasAfterTarget) {
index--;
}
}
// drop before the target?
else if (dropTargetData.getData("horizontalLocation").equals(
HorizontalDropLocation.LEFT.toString())) {
index--;
if (index < 0) {
index = 0;
}