From 85cdb278372dca186d4d26c899f519bb17462bca Mon Sep 17 00:00:00 2001 From: Ralph Soika Date: Wed, 7 Feb 2024 16:53:31 +0100 Subject: [PATCH] impl Issue #319 --- .../src/bpmn-select-listeners.tsx | 19 ++---------- .../event/BPMNCreateEventHandler.java | 26 +++++++--------- .../gateway/BPMNCreateGatewayHandler.java | 19 +++++------- .../glsp/elements/pool/CreatePoolHandler.java | 10 +++---- .../elements/task/BPMNCreateTaskHandler.java | 13 ++++---- .../BPMNChangeBoundsOperationHandler.java | 13 ++++++++ ...{GridSnapper.java => BPMNGridSnapper.java} | 30 ++++++++++++++----- 7 files changed, 66 insertions(+), 64 deletions(-) rename open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/{GridSnapper.java => BPMNGridSnapper.java} (52%) diff --git a/open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx b/open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx index fc73fea9..a873163a 100644 --- a/open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx +++ b/open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx @@ -30,7 +30,6 @@ import { isBPMNLabelNode, isBoundaryEvent, isEventNode, - isGatewayNode, isLaneNode, isPoolNode, isTaskNode @@ -54,21 +53,7 @@ export class BPMNElementSnapper implements ISnapper { constructor(public grid: { x: number; y: number } = { x: 10, y: 10 }) { } snap(position: Point, _element: GModelElement): Point { - - if (isTaskNode(_element)) { - return { - x: Math.round(position.x / 10) * 10, - y: Math.round(position.y / 10) * 10 - }; - } - - if (isGatewayNode(_element)) { - return { - x: Math.round(position.x / 5) * 5, - y: Math.round(position.y / 5) * 5 - }; - } - + // events are moved by 1x1 if (isEventNode(_element)) { return { x: Math.round(position.x / 1) * 1, @@ -76,7 +61,7 @@ export class BPMNElementSnapper implements ISnapper { }; } - // default... + // default move 10x10... return { x: Math.round(position.x / this.grid.x) * this.grid.x, y: Math.round(position.y / this.grid.y) * this.grid.y diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/event/BPMNCreateEventHandler.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/event/BPMNCreateEventHandler.java index 61ad615c..dbfa67ee 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/event/BPMNCreateEventHandler.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/event/BPMNCreateEventHandler.java @@ -39,7 +39,7 @@ import org.openbpmn.glsp.bpmn.BpmnPackage; import org.openbpmn.glsp.elements.CreateBPMNNodeOperationHandler; import org.openbpmn.glsp.model.BPMNGModelState; -import org.openbpmn.glsp.utils.GridSnapper; +import org.openbpmn.glsp.utils.BPMNGridSnapper; import com.google.inject.Inject; @@ -125,33 +125,29 @@ public void executeOperation(final CreateNodeOperation operation) { Optional point = operation.getLocation(); if (point.isPresent()) { - double elementX = point.get().getX(); - double elementY = point.get().getY(); // compute relative center position... - elementX = GridSnapper.snap(elementX - (Event.DEFAULT_WIDTH / 2)); - elementY = GridSnapper.snap(elementY - (Event.DEFAULT_HEIGHT / 2)); + BPMNPoint targetPosition = BPMNGridSnapper.snap(event, point.get()); // compute default label position - double labelX = elementX + (Event.DEFAULT_WIDTH / 2) - (BPMNLabel.DEFAULT_WIDTH / 2); - double labelY = elementY + Event.DEFAULT_HEIGHT + Event.LABEL_OFFSET; - + double labelX = targetPosition.getX() + (Event.DEFAULT_WIDTH / 2) - (BPMNLabel.DEFAULT_WIDTH / 2); + double labelY = targetPosition.getY() + Event.DEFAULT_HEIGHT + Event.LABEL_OFFSET; // in case of a BoundaryEvent we adjust the position to the TaskEdge... if (BPMNTypes.BOUNDARY_EVENT.equals(elementTypeId) && (containerElement != null && containerElement instanceof Activity)) { double taskY = containerElement.getBounds().getPosition().getY(); BPMNPoint taskCenterPoint = containerElement.getBounds().getCenter(); // Upper bounds? - if (elementY + (Event.DEFAULT_HEIGHT / 2) < taskCenterPoint.getY()) { - elementY = taskY - (Event.DEFAULT_HEIGHT / 2); - labelY = elementY - BPMNLabel.DEFAULT_HEIGHT + Event.LABEL_OFFSET; + if (targetPosition.getY() + (Event.DEFAULT_HEIGHT / 2) < taskCenterPoint.getY()) { + targetPosition.setY(taskY - (Event.DEFAULT_HEIGHT / 2)); + labelY = targetPosition.getY() - BPMNLabel.DEFAULT_HEIGHT + Event.LABEL_OFFSET; } else { // lower bounds - elementY = taskY + (containerElement.getBounds().getDimension().getHeight() - - (Event.DEFAULT_HEIGHT / 2)); - labelY = elementY + Event.DEFAULT_HEIGHT + Event.LABEL_OFFSET; + targetPosition.setY(taskY + (containerElement.getBounds().getDimension().getHeight() + - (Event.DEFAULT_HEIGHT / 2))); + labelY = targetPosition.getY() + Event.DEFAULT_HEIGHT + Event.LABEL_OFFSET; } } // set event bounds - event.setPosition(elementX, elementY); + event.setPosition(targetPosition); event.setDimension(Event.DEFAULT_WIDTH, Event.DEFAULT_HEIGHT); // set label bounds event.getLabel().updateLocation(labelX, labelY); diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/gateway/BPMNCreateGatewayHandler.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/gateway/BPMNCreateGatewayHandler.java index e1deeefd..dcfbc423 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/gateway/BPMNCreateGatewayHandler.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/gateway/BPMNCreateGatewayHandler.java @@ -31,11 +31,12 @@ import org.openbpmn.bpmn.elements.BPMNProcess; import org.openbpmn.bpmn.elements.Gateway; import org.openbpmn.bpmn.elements.core.BPMNLabel; +import org.openbpmn.bpmn.elements.core.BPMNPoint; import org.openbpmn.bpmn.exceptions.BPMNModelException; import org.openbpmn.glsp.bpmn.BpmnPackage; import org.openbpmn.glsp.elements.CreateBPMNNodeOperationHandler; import org.openbpmn.glsp.model.BPMNGModelState; -import org.openbpmn.glsp.utils.GridSnapper; +import org.openbpmn.glsp.utils.BPMNGridSnapper; import com.google.inject.Inject; @@ -65,6 +66,7 @@ public class BPMNCreateGatewayHandler extends CreateBPMNNodeOperationHandler { public BPMNCreateGatewayHandler() { super(BPMNTypes.BPMN_GATEWAYS); } + @Override public Optional createCommand(final CreateNodeOperation operation) { return commandOf(() -> executeOperation(operation)); @@ -82,19 +84,12 @@ public void executeOperation(final CreateNodeOperation operation) { Gateway gateway = bpmnProcess.addGateway(gatewayID, getLabel(), operation.getElementTypeId()); Optional point = operation.getLocation(); if (point.isPresent()) { - - double elementX = point.get().getX(); - double elementY = point.get().getY(); - - // compute relative center position... - elementX = GridSnapper.snap(elementX - (Gateway.DEFAULT_WIDTH / 2)); - elementY = GridSnapper.snap(elementY - (Gateway.DEFAULT_HEIGHT / 2)); - - gateway.setPosition(elementX, elementY); + BPMNPoint targetPosition = BPMNGridSnapper.snap(gateway, point.get()); + gateway.setPosition(targetPosition); gateway.setDimension(Gateway.DEFAULT_WIDTH, Gateway.DEFAULT_HEIGHT); // set label bounds - double labelX = elementX + (Gateway.DEFAULT_WIDTH / 2) - (BPMNLabel.DEFAULT_WIDTH / 2); - double labelY = elementY + Gateway.DEFAULT_HEIGHT + Gateway.LABEL_OFFSET; + double labelX = targetPosition.getX() + (Gateway.DEFAULT_WIDTH / 2) - (BPMNLabel.DEFAULT_WIDTH / 2); + double labelY = targetPosition.getY() + Gateway.DEFAULT_HEIGHT + Gateway.LABEL_OFFSET; logger.debug("new BPMNLabel Position = " + labelX + "," + labelY); gateway.getLabel().updateLocation(labelX, labelY); gateway.getLabel().updateDimension(BPMNLabel.DEFAULT_WIDTH, BPMNLabel.DEFAULT_HEIGHT); diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/pool/CreatePoolHandler.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/pool/CreatePoolHandler.java index aae68b5d..0814c2e5 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/pool/CreatePoolHandler.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/pool/CreatePoolHandler.java @@ -32,7 +32,7 @@ import org.openbpmn.glsp.bpmn.BpmnPackage; import org.openbpmn.glsp.elements.CreateBPMNNodeOperationHandler; import org.openbpmn.glsp.model.BPMNGModelState; -import org.openbpmn.glsp.utils.GridSnapper; +import org.openbpmn.glsp.utils.BPMNGridSnapper; import com.google.inject.Inject; @@ -57,9 +57,9 @@ public CreatePoolHandler() { } @Override - public Optional createCommand(final CreateNodeOperation operation) { - return commandOf(() -> executeOperation(operation)); - } + public Optional createCommand(final CreateNodeOperation operation) { + return commandOf(() -> executeOperation(operation)); + } public void executeOperation(final CreateNodeOperation operation) { elementTypeId = operation.getElementTypeId(); @@ -73,7 +73,7 @@ public void executeOperation(final CreateNodeOperation operation) { if (point.isPresent()) { // set the bounds // participant.setPosition(point.get().getX(), point.get().getY()); - participant.setPosition(GridSnapper.snap(point.get().getX(), point.get().getY())); + participant.setPosition(BPMNGridSnapper.snap(point.get().getX(), point.get().getY())); participant.setDimension(Participant.DEFAULT_WIDTH, Participant.DEFAULT_HEIGHT); } } catch (BPMNModelException e) { diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/task/BPMNCreateTaskHandler.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/task/BPMNCreateTaskHandler.java index 1b441bea..0b1e56c8 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/task/BPMNCreateTaskHandler.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/elements/task/BPMNCreateTaskHandler.java @@ -30,11 +30,12 @@ import org.openbpmn.bpmn.BPMNTypes; import org.openbpmn.bpmn.elements.Activity; import org.openbpmn.bpmn.elements.BPMNProcess; +import org.openbpmn.bpmn.elements.core.BPMNPoint; import org.openbpmn.bpmn.exceptions.BPMNModelException; import org.openbpmn.glsp.bpmn.BpmnPackage; import org.openbpmn.glsp.elements.CreateBPMNNodeOperationHandler; import org.openbpmn.glsp.model.BPMNGModelState; -import org.openbpmn.glsp.utils.GridSnapper; +import org.openbpmn.glsp.utils.BPMNGridSnapper; import com.google.inject.Inject; @@ -86,14 +87,10 @@ protected void executeOperation(final CreateNodeOperation operation) { Activity task = bpmnProcess.addTask(taskID, getLabel(), operation.getElementTypeId()); Optional point = operation.getLocation(); if (point.isPresent()) { - double elementX = point.get().getX(); - double elementY = point.get().getY(); - // compute relative center position... - elementX = GridSnapper.snap(elementX - (Activity.DEFAULT_WIDTH / 2)); - elementY = GridSnapper.snap(elementY - (Activity.DEFAULT_HEIGHT / 2)); - task.setPosition(elementX, elementY); + BPMNPoint targetPosition = BPMNGridSnapper.snap(task, point.get()); + task.setPosition(targetPosition); task.setDimension(Activity.DEFAULT_WIDTH, Activity.DEFAULT_HEIGHT); - logger.debug("new BPMNActivity Position = " + elementX + "," + elementY); + logger.debug("new BPMNActivity Position = " + targetPosition.getX() + "," + targetPosition.getY()); } } else { // should not happen diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNChangeBoundsOperationHandler.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNChangeBoundsOperationHandler.java index 4e70ca6d..95f60e67 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNChangeBoundsOperationHandler.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNChangeBoundsOperationHandler.java @@ -144,6 +144,19 @@ private void executeOperation(final ChangeBoundsOperation operation) { BPMNElementNode bpmnElementNode = (BPMNElementNode) modelState.getBpmnModel().findElementById(id); if (bpmnElementNode != null) { + // Special snap mechanism to snap Tasks and Pools to a 10x10 gird + if (bpmnElementNode instanceof Participant || bpmnElementNode instanceof Activity) { + // long _x = Math.round(newPoint.getX() / 10.0) * 10; + newPoint.setX(Math.round(newPoint.getX() / 10.0) * 10); + newPoint.setY(Math.round(newPoint.getY() / 10.0) * 10); + } else { + // Defautl: We round x and y + // The reason why we roudn here is that the HelperLine Feature somtimes + // returns float values with decimal places (e.g. 170.3534577345) + newPoint.setX(Math.round(newPoint.getX())); + newPoint.setY(Math.round(newPoint.getY())); + } + if (bpmnElementNode instanceof Participant) { updatePool(gNode, bpmnElementNode, id, newPoint, newSize); } else { diff --git a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/GridSnapper.java b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/BPMNGridSnapper.java similarity index 52% rename from open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/GridSnapper.java rename to open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/BPMNGridSnapper.java index 7d3517ba..1ceb1106 100644 --- a/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/GridSnapper.java +++ b/open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/BPMNGridSnapper.java @@ -4,17 +4,19 @@ import org.eclipse.glsp.graph.GPoint; import org.eclipse.glsp.graph.util.GraphUtil; +import org.openbpmn.bpmn.elements.Event; +import org.openbpmn.bpmn.elements.core.BPMNElementNode; import org.openbpmn.bpmn.elements.core.BPMNPoint; /** * Helper Class to be used when a new Element is added to the diagram panel. It - * snaps the initial coordinates to 10x10 px. + * snaps the initial coordinates to 10x10 px, depending on the element type */ -public class GridSnapper { +public class BPMNGridSnapper { public static final double GRID_X = 10.0; public static final double GRID_Y = 10.0; - private GridSnapper() { + private BPMNGridSnapper() { } public static GPoint snap(final GPoint originalpoint) { @@ -24,7 +26,7 @@ public static GPoint snap(final GPoint originalpoint) { } public static Optional snap(final Optional originalPoint) { - return originalPoint.map(GridSnapper::snap); + return originalPoint.map(BPMNGridSnapper::snap); } public static BPMNPoint snap(final double elementX, double elementY) { @@ -34,13 +36,27 @@ public static BPMNPoint snap(final double elementX, double elementY) { } /** - * Snaps a x or y coordinate... + * Snaps a BPMN element based on a given point to the center of the element. * * @param pos * @return */ - public static double snap(final double pos) { - return Math.round(pos / GRID_X) * GRID_X; + public static BPMNPoint snap(final BPMNElementNode elementNode, final GPoint point) { + // center + double x = point.getX() - (elementNode.getDefaultWidth() / 2); + double y = point.getY() - (elementNode.getDefaultHeight() / 2); + // snap + x = Math.round(x / GRID_X) * GRID_X; + y = Math.round(y / GRID_Y) * GRID_Y; + + // compute offset + if (elementNode instanceof Event) { + // In casse of an event we need to adjust the offset! + x = x - 3; + y = y - 3; + } + + return new BPMNPoint(x, y); } } \ No newline at end of file