public void drop(DragAndDropEvent dropEvent) {
Transferable transferable = dropEvent.getTransferable();
Component sourceComponent = transferable.getSourceComponent();
TargetDetails dropTargetData = dropEvent.getTargetDetails();
DropTarget target = dropTargetData.getTarget();
if (sourceComponent.getParent() != layout) {
AbstractComponent c = getWrappedComponent(
createComponentFromPaletteItem(
sourceComponent.getCaption(), null), this);
int index = 0;
Iterator<Component> componentIterator = layout
.getComponentIterator();
Component next = null;
while (next != target && componentIterator.hasNext()) {
next = componentIterator.next();
if (next != sourceComponent) {
index++;
}
}
if (dropTargetData.getData("verticalLocation").equals(
VerticalDropLocation.TOP.toString())) {
index--;
if (index <= 0) {
index = 1;
}
}
layout.addComponent(c, index);
}
if (sourceComponent instanceof WrappedComponent) {
// find the location where to move the dragged component
boolean sourceWasAfterTarget = true;
int index = 0;
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("verticalLocation").equals(
VerticalDropLocation.MIDDLE.toString())) {
if (sourceWasAfterTarget) {
index--;
}
}
// drop before the target?
else if (dropTargetData.getData("verticalLocation").equals(
VerticalDropLocation.TOP.toString())) {
index--;
if (index <= 0) {
index = 1;
}