Skip to content

Commit 7e28ace

Browse files
committed
impl completed
Issue #320
1 parent 33d3c1a commit 7e28ace

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-select-listeners.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { inject, injectable } from 'inversify';
4747
*/
4848
@injectable()
4949
export class BPMNElementSnapper implements ISnapper {
50-
constructor(public grid: { x: number; y: number } = { x: 10, y: 10 }) { }
50+
constructor(public grid: { x: number; y: number } = { x: 1, y: 1 }) { }
5151

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

open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/model/BPMNGModelState.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
import org.apache.logging.log4j.LogManager;
2121
import org.apache.logging.log4j.Logger;
22+
import org.eclipse.glsp.graph.GGraph;
2223
import org.eclipse.glsp.server.model.DefaultGModelState;
2324
import org.openbpmn.bpmn.BPMNModel;
2425
import org.openbpmn.bpmn.exceptions.BPMNModelException;
2526
import org.w3c.dom.Document;
2627

28+
import com.google.inject.Inject;
29+
2730
/**
2831
* The BPMNGModelState extends the DefaultGModelState and provides the property
2932
* 'bpmnModel' which holds an instance of the BPMN MetaModel.
@@ -46,6 +49,9 @@ public class BPMNGModelState extends DefaultGModelState {
4649
private boolean initialized = false;
4750
private String rootID = "undefined_root_id";
4851

52+
@Inject
53+
protected BPMNGModelFactory bpmnGModelFactory;
54+
4955
public BPMNGModelState() {
5056
resetRevisions();
5157
}
@@ -174,4 +180,18 @@ public void reset() {
174180
this.initialized = false;
175181
}
176182

183+
/**
184+
* This method can be used to rebuild the GModel which results in a new Model
185+
* Root ID.
186+
* This method is for example called by the BPMNAutoAlignActionHandler to
187+
* refresh the Client side
188+
*
189+
*/
190+
public void refreshGModelState() {
191+
this.setBpmnModel(bpmnModel);
192+
GGraph newGModel = bpmnGModelFactory.buildGGraph(bpmnModel);
193+
this.updateRoot(newGModel);
194+
this.execute(new SetDirtyCommand());
195+
}
196+
177197
}

open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNAutoAlignActionHandler.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@
1515
********************************************************************************/
1616
package org.openbpmn.glsp.operations;
1717

18+
import java.util.ArrayList;
1819
import java.util.List;
20+
import java.util.Set;
1921
import java.util.logging.Logger;
2022

2123
import org.eclipse.glsp.server.actions.AbstractActionHandler;
2224
import org.eclipse.glsp.server.actions.Action;
25+
import org.eclipse.glsp.server.actions.ActionDispatcher;
26+
import org.eclipse.glsp.server.actions.SetDirtyStateAction;
27+
import org.eclipse.glsp.server.features.core.model.UpdateModelAction;
28+
import org.openbpmn.bpmn.elements.BPMNProcess;
29+
import org.openbpmn.bpmn.elements.Participant;
30+
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
31+
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
32+
import org.openbpmn.glsp.model.BPMNGModelFactory;
2333
import org.openbpmn.glsp.model.BPMNGModelState;
34+
import org.openbpmn.glsp.utils.BPMNGridSnapper;
2435

2536
import com.google.inject.Inject;
2637

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

51+
@Inject
52+
protected ActionDispatcher actionDispatcher;
53+
54+
@Inject
55+
protected BPMNGModelFactory bpmnGModelFactory;
56+
4057
@Override
4158
protected List<Action> executeAction(final BPMNAutoAlignAction actualAction) {
59+
logger.finest("Auto align all elements....");
60+
Set<Participant> participants = modelState.getBpmnModel().getParticipants();
61+
for (Participant participant : participants) {
62+
try {
63+
BPMNGridSnapper.snap(participant);
64+
} catch (BPMNMissingElementException e) {
65+
logger.severe("Unable to update bounds for " + participant.getId() + " : " + e.getMessage());
66+
}
67+
}
4268

43-
logger.info("----------> Auto Align Action received!");
69+
Set<BPMNProcess> processList = modelState.getBpmnModel().getProcesses();
70+
for (BPMNProcess process : processList) {
71+
try {
72+
// snap all elements
73+
Set<BPMNElementNode> allNodes = process.getAllFlowElementNodes();
74+
for (BPMNElementNode _node : allNodes) {
75+
BPMNGridSnapper.snap(_node);
76+
}
77+
} catch (BPMNMissingElementException e) {
78+
logger.severe("Unable to update bounds for " + process.getId() + " : " + e.getMessage());
79+
}
80+
}
4481

45-
// no more action - the GModel is now up to date
46-
return none();
82+
// Force a complete refresh of the GModel state....
83+
modelState.refreshGModelState();
84+
List<Action> resultAction = new ArrayList<>();
85+
resultAction.add(new SetDirtyStateAction(true,
86+
SetDirtyStateAction.Reason.OPERATION));
87+
resultAction.add(new UpdateModelAction(modelState.getRoot()));
88+
return resultAction;
4789
}
4890

4991
}

open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/operations/BPMNChangeBoundsOperationHandler.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ private void executeOperation(final ChangeBoundsOperation operation) {
149149
// long _x = Math.round(newPoint.getX() / 10.0) * 10;
150150
newPoint.setX(Math.round(newPoint.getX() / 10.0) * 10);
151151
newPoint.setY(Math.round(newPoint.getY() / 10.0) * 10);
152+
newSize.setHeight(Math.round(newSize.getHeight() / 10.0) * 10);
153+
newSize.setWidth(Math.round(newSize.getWidth() / 10.0) * 10);
152154
} else {
153155
// Defautl: We round x and y
154156
// The reason why we roudn here is that the HelperLine Feature somtimes

open-bpmn.glsp-server/src/main/java/org/openbpmn/glsp/utils/BPMNGridSnapper.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package org.openbpmn.glsp.utils;
22

33
import java.util.Optional;
4+
import java.util.logging.Logger;
45

56
import org.eclipse.glsp.graph.GPoint;
67
import org.eclipse.glsp.graph.util.GraphUtil;
8+
import org.openbpmn.bpmn.elements.Activity;
79
import org.openbpmn.bpmn.elements.Event;
10+
import org.openbpmn.bpmn.elements.Participant;
811
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
912
import org.openbpmn.bpmn.elements.core.BPMNPoint;
13+
import org.openbpmn.bpmn.exceptions.BPMNMissingElementException;
1014

1115
/**
1216
* Helper Class to be used when a new Element is added to the diagram panel. It
1317
* snaps the initial coordinates to 10x10 px, depending on the element type
1418
*/
1519
public class BPMNGridSnapper {
20+
private static Logger logger = Logger.getLogger(BPMNGridSnapper.class.getName());
21+
1622
public static final double GRID_X = 10.0;
1723
public static final double GRID_Y = 10.0;
1824

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

68+
/**
69+
* Snaps a BPMN element based on a given point to the center of the element.
70+
*
71+
* @param pos
72+
* @return
73+
* @throws BPMNMissingElementException
74+
*/
75+
public static void snap(final BPMNElementNode elementNode) throws BPMNMissingElementException {
76+
77+
logger.finest("...snap " + elementNode.getId() + " Pos: " + elementNode.getBounds().getPosition());
78+
double x = elementNode.getBounds().getPosition().getX();
79+
double y = elementNode.getBounds().getPosition().getY();
80+
// snap
81+
x = Math.round(x / GRID_X) * GRID_X;
82+
y = Math.round(y / GRID_Y) * GRID_Y;
83+
84+
// compute offset
85+
if (elementNode instanceof Event) {
86+
// In casse of an event we need to adjust the offset!
87+
x = x - 3;
88+
y = y - 3;
89+
}
90+
// update pos
91+
elementNode.setPosition(x, y);
92+
logger.finest("...after snap " + elementNode.getId() + " Pos: " + elementNode.getBounds().getPosition());
93+
94+
// snap bounds for tasks and pools
95+
if (elementNode instanceof Participant || elementNode instanceof Activity) {
96+
double w = elementNode.getBounds().getDimension().getWidth();
97+
double h = elementNode.getBounds().getDimension().getHeight();
98+
elementNode.setDimension(w, h);
99+
}
100+
}
101+
62102
}

0 commit comments

Comments
 (0)