Skip to content

Commit

Permalink
Enable seamless drag-and-drop between graphical editors
Browse files Browse the repository at this point in the history
  • Loading branch information
mx990 committed Sep 19, 2024
1 parent f4ffb21 commit 8b628bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public CustomDragSourceListener(final EditPartViewer viewer) {
public void dragStart(final DragSourceEvent event) {
final Request req = createRequest(new Point(event.x, event.y));

if (!isAltKeyPressed() || req == null) {
if (req == null) {
event.doit = false;
lastReq = null;
return;
Expand All @@ -71,13 +71,6 @@ public void dragFinished(final DragSourceEvent event) {
lastReq = null;
}

private boolean isAltKeyPressed() {
final AdvancedGraphicalViewerKeyHandler keyHandler = (AdvancedGraphicalViewerKeyHandler) getViewer()
.getKeyHandler();

return (keyHandler.getCurrentStateMask() & SWT.ALT) != 0;
}

private boolean isCtrlKeyPressed() {
final AdvancedGraphicalViewerKeyHandler keyHandler = (AdvancedGraphicalViewerKeyHandler) getViewer()
.getKeyHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,40 @@
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateConnectionRequest;
import org.eclipse.gef.requests.ReconnectRequest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;

public class CustomDragTargetListener extends AbstractTransferDropTargetListener {

public CustomDragTargetListener(final EditPartViewer viewer) {
super(viewer, CustomSourceTransfer.getInstance());
setEnablementDeterminedByCommand(true);
}

@Override
public void handleDragOver() {
super.handleDragOver();
// forward dragOver events as mouse events to enable tool movement
getViewer().getEditDomain().mouseDrag(toMouseEvent(getCurrentEvent()), getViewer());
}

@Override
protected void handleDrop() {
super.handleDrop();
if (getCurrentEvent().detail == DND.DROP_NONE) {
// if no drop is possible
// forward drop events as mouse events to enable tool execution
getViewer().getEditDomain().mouseUp(toMouseEvent(getCurrentEvent()), getViewer());
}
}

@Override
public boolean isEnabled(final DropTargetEvent event) {
return true; // always enabled to get dragOver events
}

@Override
Expand Down Expand Up @@ -74,4 +102,15 @@ protected void updateTargetRequest() {
}
}

protected static MouseEvent toMouseEvent(final DropTargetEvent event) {
final Event swtEvent = new Event();
swtEvent.display = event.display;
swtEvent.widget = event.widget;
swtEvent.type = SWT.MouseMove;
swtEvent.item = event.item;
final Point relativePoint = ((DropTarget) event.widget).getControl().toControl(new Point(event.x, event.y));
swtEvent.x = relativePoint.x;
swtEvent.y = relativePoint.y;
return new MouseEvent(swtEvent);
}
}

0 comments on commit 8b628bf

Please sign in to comment.