Skip to content

Commit

Permalink
Enable seamless connections between graphical editors
Browse files Browse the repository at this point in the history
  • Loading branch information
mx990 committed Sep 20, 2024
1 parent 5aef5b0 commit 202b33a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.fordiac.ide.application.editparts.ConnectionEditPart;
import org.eclipse.fordiac.ide.gef.editparts.InterfaceEditPart;
Expand All @@ -37,11 +39,16 @@
import org.eclipse.fordiac.ide.model.libraryElement.IInterfaceElement;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PlatformUI;

public class FBNetworkPanningSelectionTool extends AdvancedPanningSelectionTool {

Expand All @@ -67,13 +74,15 @@ public void mouseDown(final MouseEvent me, final EditPartViewer viewer) {

@Override
public void mouseUp(final MouseEvent me, final EditPartViewer viewer) {
final EditPartViewer actualViewer = findViewer(new Point(me.x, me.y), viewer);
final MouseEvent convertedEvent = convertMouseEventCoordinates(me, viewer, actualViewer);
if (checkConnCreationState(me.stateMask)) {
connectionCreationTool.mouseUp(me, viewer);
connectionCreationTool.mouseUp(convertedEvent, actualViewer);
} else {
if (LEFT_MOUSE == me.button) {
lastLeftClick = getLocation();
}
super.mouseUp(me, viewer);
super.mouseUp(convertedEvent, actualViewer);
}
}

Expand All @@ -100,9 +109,23 @@ public void keyDown(final KeyEvent evt, final EditPartViewer viewer) {
public void mouseMove(final MouseEvent me, final EditPartViewer viewer) {
// the super call has to be first so that the target editpart is updated
// accordingly
super.mouseMove(me, viewer);
final EditPartViewer actualViewer = findViewer(new Point(me.x, me.y), viewer);
final MouseEvent convertedEvent = convertMouseEventCoordinates(me, viewer, actualViewer);
super.mouseMove(convertedEvent, actualViewer);
if (checkConnCreationState(me.stateMask)) {
connectionCreationTool.mouseDrag(me, viewer);
connectionCreationTool.mouseMove(convertedEvent, actualViewer);
}
}

@Override
public void mouseDrag(final MouseEvent me, final EditPartViewer viewer) {
// the super call has to be first so that the target editpart is updated
// accordingly
final EditPartViewer actualViewer = findViewer(new Point(me.x, me.y), viewer);
final MouseEvent convertedEvent = convertMouseEventCoordinates(me, viewer, actualViewer);
super.mouseDrag(convertedEvent, actualViewer);
if (checkConnCreationState(me.stateMask)) {
connectionCreationTool.mouseDrag(convertedEvent, actualViewer);
}
}

Expand Down Expand Up @@ -174,4 +197,42 @@ private static boolean isConnectionCreationTarget(final EditPart targetEditPart)
return (targetEditPart != null) && (targetEditPart.getModel() instanceof IInterfaceElement);
}

private static EditPartViewer findViewer(final Point point, final EditPartViewer viewer) {
if (viewer.getControl().getBounds().contains(point)) {
return viewer;
}
final Point absolute = viewer.getControl().toDisplay(point);
return Stream.of(PlatformUI.getWorkbench().getWorkbenchWindows())
.flatMap(window -> Stream.of(window.getPages())).flatMap(page -> Stream.of(page.getEditorReferences()))
.map(ref -> ref.getEditor(false)).filter(Objects::nonNull)
.<EditPartViewer>map(editor -> editor.getAdapter(GraphicalViewer.class)).filter(Objects::nonNull)
.filter(candidate -> containsAbsolutePoint(candidate, absolute)).findAny().orElse(viewer);
}

private static boolean containsAbsolutePoint(final EditPartViewer viewer, final Point point) {
final Control control = viewer.getControl();
if (control.getParent() != null) {
return control.getBounds().contains(control.getParent().toControl(point));
}
return control.getBounds().contains(point);
}

private static MouseEvent convertMouseEventCoordinates(final MouseEvent event, final EditPartViewer from,
final EditPartViewer to) {
if (from == to) {
return event;
}
final Point converted = to.getControl().toControl(from.getControl().toDisplay(event.x, event.y));
final Event swtEvent = new Event();
swtEvent.display = event.display;
swtEvent.widget = event.widget;
swtEvent.time = event.time;
swtEvent.data = event.data;
swtEvent.x = converted.x;
swtEvent.y = converted.y;
swtEvent.button = event.button;
swtEvent.stateMask = event.stateMask;
swtEvent.count = event.count;
return new MouseEvent(swtEvent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ protected boolean handleButtonUp(final int which) {
protected boolean handleDrag() {
if (isInState(PAN_IN_PROGRESS) && (getCurrentViewer().getControl() instanceof FigureCanvas)) {
final FigureCanvas canvas = (FigureCanvas) getCurrentViewer().getControl();
canvas.scrollTo(viewLocation.x - getDragMoveDelta().width, viewLocation.y - getDragMoveDelta().height);
// canvas.scrollTo(viewLocation.x - getDragMoveDelta().width, viewLocation.y -
// getDragMoveDelta().height);
return true;
}
return super.handleDrag();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
package org.eclipse.fordiac.ide.gef.tools;

import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.fordiac.ide.gef.figures.HideableConnection;
import org.eclipse.fordiac.ide.gef.router.MoveableRouter;
import org.eclipse.fordiac.ide.model.commands.create.AbstractConnectionCreateCommand;
import org.eclipse.fordiac.ide.model.ui.editors.AdvancedScrollingGraphicalViewer;
import org.eclipse.fordiac.ide.ui.UIPlugin;
import org.eclipse.fordiac.ide.ui.preferences.ConnectionPreferenceValues;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.tools.ConnectionDragCreationTool;
Expand Down Expand Up @@ -51,15 +49,28 @@ public void deactivate() {
@Override
public void mouseDrag(final MouseEvent me, final EditPartViewer viewer) {
if (isActive() && viewer instanceof final AdvancedScrollingGraphicalViewer advViewer) {
advViewer.checkScrollPositionDuringDragBounded(me,
new Point(MoveableRouter.MIN_CONNECTION_FB_DISTANCE_SCREEN
+ HideableConnection.BEND_POINT_BEVEL_SIZE + ConnectionPreferenceValues.HANDLE_SIZE,
ConnectionPreferenceValues.HANDLE_SIZE));
CanvasHelper.bindToContentPane(me, advViewer, NEW_CONNECTION_CANVAS_BORDER);
// advViewer.checkScrollPositionDuringDragBounded(me,
// new Point(MoveableRouter.MIN_CONNECTION_FB_DISTANCE_SCREEN
// + HideableConnection.BEND_POINT_BEVEL_SIZE + ConnectionPreferenceValues.HANDLE_SIZE,
// ConnectionPreferenceValues.HANDLE_SIZE));
// CanvasHelper.bindToContentPane(me, advViewer, NEW_CONNECTION_CANVAS_BORDER);
}
super.mouseDrag(me, viewer);
}

@Override
protected boolean handleMove() {
// overwritten to disable viewer check
if (isInState(STATE_CONNECTION_STARTED | STATE_INITIAL | STATE_ACCESSIBLE_DRAG_IN_PROGRESS)) {
updateTargetRequest();
updateTargetUnderMouse();
showSourceFeedback();
showTargetFeedback();
setCurrentCommand(getCommand());
}
return true;
}

@Override
public void mouseUp(final MouseEvent me, final EditPartViewer viewer) {
if (((me.stateMask & SWT.MOD2) != 0)) {
Expand Down

0 comments on commit 202b33a

Please sign in to comment.