Skip to content

Commit

Permalink
impl completed
Browse files Browse the repository at this point in the history
Issue #320
  • Loading branch information
rsoika committed Feb 8, 2024
1 parent 33d3c1a commit 7e28ace
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { inject, injectable } from 'inversify';
*/
@injectable()
export class BPMNElementSnapper implements ISnapper {
constructor(public grid: { x: number; y: number } = { x: 10, y: 10 }) { }
constructor(public grid: { x: number; y: number } = { x: 1, y: 1 }) { }

snap(position: Point, _element: GModelElement): Point {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.glsp.graph.GGraph;
import org.eclipse.glsp.server.model.DefaultGModelState;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.w3c.dom.Document;

import com.google.inject.Inject;

/**
* The BPMNGModelState extends the DefaultGModelState and provides the property
* 'bpmnModel' which holds an instance of the BPMN MetaModel.
Expand All @@ -46,6 +49,9 @@ public class BPMNGModelState extends DefaultGModelState {
private boolean initialized = false;
private String rootID = "undefined_root_id";

@Inject
protected BPMNGModelFactory bpmnGModelFactory;

public BPMNGModelState() {
resetRevisions();
}
Expand Down Expand Up @@ -174,4 +180,18 @@ public void reset() {
this.initialized = false;
}

/**
* This method can be used to rebuild the GModel which results in a new Model
* Root ID.
* This method is for example called by the BPMNAutoAlignActionHandler to
* refresh the Client side
*
*/
public void refreshGModelState() {
this.setBpmnModel(bpmnModel);
GGraph newGModel = bpmnGModelFactory.buildGGraph(bpmnModel);
this.updateRoot(newGModel);
this.execute(new SetDirtyCommand());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
********************************************************************************/
package org.openbpmn.glsp.operations;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import org.eclipse.glsp.server.actions.AbstractActionHandler;
import org.eclipse.glsp.server.actions.Action;
import org.eclipse.glsp.server.actions.ActionDispatcher;
import org.eclipse.glsp.server.actions.SetDirtyStateAction;
import org.eclipse.glsp.server.features.core.model.UpdateModelAction;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.elements.Participant;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
import org.openbpmn.glsp.model.BPMNGModelFactory;
import org.openbpmn.glsp.model.BPMNGModelState;
import org.openbpmn.glsp.utils.BPMNGridSnapper;

import com.google.inject.Inject;

Expand All @@ -37,13 +48,44 @@ public class BPMNAutoAlignActionHandler extends AbstractActionHandler<BPMNAutoAl
@Inject
protected BPMNGModelState modelState;

@Inject
protected ActionDispatcher actionDispatcher;

@Inject
protected BPMNGModelFactory bpmnGModelFactory;

@Override
protected List<Action> executeAction(final BPMNAutoAlignAction actualAction) {
logger.finest("Auto align all elements....");
Set<Participant> participants = modelState.getBpmnModel().getParticipants();
for (Participant participant : participants) {
try {
BPMNGridSnapper.snap(participant);
} catch (BPMNMissingElementException e) {
logger.severe("Unable to update bounds for " + participant.getId() + " : " + e.getMessage());
}
}

logger.info("----------> Auto Align Action received!");
Set<BPMNProcess> processList = modelState.getBpmnModel().getProcesses();
for (BPMNProcess process : processList) {
try {
// snap all elements
Set<BPMNElementNode> allNodes = process.getAllFlowElementNodes();
for (BPMNElementNode _node : allNodes) {
BPMNGridSnapper.snap(_node);
}
} catch (BPMNMissingElementException e) {
logger.severe("Unable to update bounds for " + process.getId() + " : " + e.getMessage());
}
}

// no more action - the GModel is now up to date
return none();
// Force a complete refresh of the GModel state....
modelState.refreshGModelState();
List<Action> resultAction = new ArrayList<>();
resultAction.add(new SetDirtyStateAction(true,
SetDirtyStateAction.Reason.OPERATION));
resultAction.add(new UpdateModelAction(modelState.getRoot()));
return resultAction;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private void executeOperation(final ChangeBoundsOperation operation) {
// 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);
newSize.setHeight(Math.round(newSize.getHeight() / 10.0) * 10);
newSize.setWidth(Math.round(newSize.getWidth() / 10.0) * 10);
} else {
// Defautl: We round x and y
// The reason why we roudn here is that the HelperLine Feature somtimes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package org.openbpmn.glsp.utils;

import java.util.Optional;
import java.util.logging.Logger;

import org.eclipse.glsp.graph.GPoint;
import org.eclipse.glsp.graph.util.GraphUtil;
import org.openbpmn.bpmn.elements.Activity;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.Participant;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.elements.core.BPMNPoint;
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;

/**
* Helper Class to be used when a new Element is added to the diagram panel. It
* snaps the initial coordinates to 10x10 px, depending on the element type
*/
public class BPMNGridSnapper {
private static Logger logger = Logger.getLogger(BPMNGridSnapper.class.getName());

public static final double GRID_X = 10.0;
public static final double GRID_Y = 10.0;

Expand Down Expand Up @@ -59,4 +65,38 @@ public static BPMNPoint snap(final BPMNElementNode elementNode, final GPoint poi
return new BPMNPoint(x, y);
}

/**
* Snaps a BPMN element based on a given point to the center of the element.
*
* @param pos
* @return
* @throws BPMNMissingElementException
*/
public static void snap(final BPMNElementNode elementNode) throws BPMNMissingElementException {

logger.finest("...snap " + elementNode.getId() + " Pos: " + elementNode.getBounds().getPosition());
double x = elementNode.getBounds().getPosition().getX();
double y = elementNode.getBounds().getPosition().getY();
// 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;
}
// update pos
elementNode.setPosition(x, y);
logger.finest("...after snap " + elementNode.getId() + " Pos: " + elementNode.getBounds().getPosition());

// snap bounds for tasks and pools
if (elementNode instanceof Participant || elementNode instanceof Activity) {
double w = elementNode.getBounds().getDimension().getWidth();
double h = elementNode.getBounds().getDimension().getHeight();
elementNode.setDimension(w, h);
}
}

}

0 comments on commit 7e28ace

Please sign in to comment.