private static void activateDragAndDrop() {
if(isDnDActive) {
return;
}
isDnDActive = true;
DragSource dragSource = DragSource.getDefaultDragSource();
DragSourceAdapter dragSourceListener = new DragSourceAdapter() {
@Override
public void dragEnter(DragSourceDragEvent dsde) {
processDragSourceEvent(dsde);
}
@Override
public void dragMouseMoved(DragSourceDragEvent dsde) {
processDragSourceEvent(dsde);
}
private void processDragSourceEvent(DragSourceDragEvent dsde) {
if(dndHandler != null) {
return;
}
dndHandler = new DnDHandler();
List<NativeComponent> nativeComponentList = new ArrayList<NativeComponent>();
for(NativeComponent nativeComponent: getNativeComponents()) {
DropTarget dropTarget = nativeComponent.getDropTarget();
if(dropTarget != null && dropTarget.isActive()) {
nativeComponentList.add(nativeComponent);
}
}
SWTNativeComponent[] nativeComponents = nativeComponentList.toArray(new SWTNativeComponent[0]);
boolean[] wereEnabled = new boolean[nativeComponents.length];
for(int i=0; i<nativeComponents.length; i++) {
SWTNativeComponent nativeComponent = nativeComponents[i];
wereEnabled[i] = nativeComponent.isControlParentEnabled;
nativeComponent.setControlParentEnabled(false, false);
}
dndHandler.nativeComponents = nativeComponents;
dndHandler.wereEnabled = wereEnabled;
}
@Override
public void dragDropEnd(DragSourceDropEvent dsde) {
if(dndHandler == null) {
return;
}
SWTNativeComponent[] nativeComponents = dndHandler.nativeComponents;
boolean[] wereEnabled = dndHandler.wereEnabled;
for(int i=0; i<nativeComponents.length; i++) {
SWTNativeComponent nativeComponent = nativeComponents[i];
if(wereEnabled[i]) {
nativeComponent.setControlParentEnabled(true, false);
}
}
dndHandler = null;
}
};
dragSource.addDragSourceMotionListener(dragSourceListener);
dragSource.addDragSourceListener(dragSourceListener);
}