Skip to content

Commit

Permalink
impl
Browse files Browse the repository at this point in the history
Issue #319
  • Loading branch information
rsoika committed Feb 7, 2024
1 parent c290bbd commit 85cdb27
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 64 deletions.
19 changes: 2 additions & 17 deletions open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
isBPMNLabelNode,
isBoundaryEvent,
isEventNode,
isGatewayNode,
isLaneNode,
isPoolNode,
isTaskNode
Expand All @@ -54,29 +53,15 @@ 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,
y: Math.round(position.y / 1) * 1
};
}

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -125,33 +125,29 @@ public void executeOperation(final CreateNodeOperation operation) {
Optional<GPoint> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -65,6 +66,7 @@ public class BPMNCreateGatewayHandler extends CreateBPMNNodeOperationHandler {
public BPMNCreateGatewayHandler() {
super(BPMNTypes.BPMN_GATEWAYS);
}

@Override
public Optional<Command> createCommand(final CreateNodeOperation operation) {
return commandOf(() -> executeOperation(operation));
Expand All @@ -82,19 +84,12 @@ public void executeOperation(final CreateNodeOperation operation) {
Gateway gateway = bpmnProcess.addGateway(gatewayID, getLabel(), operation.getElementTypeId());
Optional<GPoint> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -57,9 +57,9 @@ public CreatePoolHandler() {
}

@Override
public Optional<Command> createCommand(final CreateNodeOperation operation) {
return commandOf(() -> executeOperation(operation));
}
public Optional<Command> createCommand(final CreateNodeOperation operation) {
return commandOf(() -> executeOperation(operation));
}

public void executeOperation(final CreateNodeOperation operation) {
elementTypeId = operation.getElementTypeId();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -86,14 +87,10 @@ protected void executeOperation(final CreateNodeOperation operation) {
Activity task = bpmnProcess.addTask(taskID, getLabel(), operation.getElementTypeId());
Optional<GPoint> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -24,7 +26,7 @@ public static GPoint snap(final GPoint originalpoint) {
}

public static Optional<GPoint> snap(final Optional<GPoint> originalPoint) {
return originalPoint.map(GridSnapper::snap);
return originalPoint.map(BPMNGridSnapper::snap);
}

public static BPMNPoint snap(final double elementX, double elementY) {
Expand All @@ -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);
}

}

0 comments on commit 85cdb27

Please sign in to comment.