}
@SuppressWarnings("unchecked")
public PictogramElement add(IAddContext context) {
IAddConnectionContext addConContext = (IAddConnectionContext) context;
SequenceFlow addedSequenceFlow = (SequenceFlow) context.getNewObject();
Anchor sourceAnchor = null;
Anchor targetAnchor = null;
if(addConContext.getSourceAnchor() == null) {
EList<Shape> shapeList = getDiagram().getChildren();
for (Shape shape : shapeList) {
FlowNode flowNode = (FlowNode) getBusinessObjectForPictogramElement(shape.getGraphicsAlgorithm().getPictogramElement());
if(flowNode == null || flowNode.getId() == null || addedSequenceFlow.getSourceRef() == null ||
addedSequenceFlow.getTargetRef() == null) continue;
if(flowNode.getId().equals(addedSequenceFlow.getSourceRef())) {
EList<Anchor> anchorList = ((ContainerShape) shape).getAnchors();
for (Anchor anchor : anchorList) {
if(anchor instanceof ChopboxAnchor) {
sourceAnchor = anchor;
break;
}
}
}
if(flowNode.getId().equals(addedSequenceFlow.getTargetRef())) {
EList<Anchor> anchorList = ((ContainerShape) shape).getAnchors();
for (Anchor anchor : anchorList) {
if(anchor instanceof ChopboxAnchor) {
targetAnchor = anchor;
break;
}
}
}
}
} else {
sourceAnchor = addConContext.getSourceAnchor();
targetAnchor = addConContext.getTargetAnchor();
}
if(sourceAnchor == null || targetAnchor == null) {
return null;
}
IPeCreateService peCreateService = Graphiti.getPeCreateService();
// CONNECTION WITH POLYLINE
FreeFormConnection connection = peCreateService.createFreeFormConnection(getDiagram());
connection.setStart(sourceAnchor);
connection.setEnd(targetAnchor);
sourceAnchor.getOutgoingConnections().add(connection);
targetAnchor.getIncomingConnections().add(connection);
Bpmn2MemoryModel model = ModelHandler.getModel(EcoreUtil.getURI(getDiagram()));
FlowElement sourceElement = model.getFlowElement(addedSequenceFlow.getSourceRef());
FlowElement targetElement = model.getFlowElement(addedSequenceFlow.getTargetRef());
GraphicsAlgorithm sourceGraphics = getPictogramElement(sourceElement).getGraphicsAlgorithm();
GraphicsAlgorithm targetGraphics = getPictogramElement(targetElement).getGraphicsAlgorithm();
List<GraphicInfo> bendpointList = null;
if(addConContext.getProperty("org.activiti.designer.bendpoints") != null) {
bendpointList = (List<GraphicInfo>) addConContext.getProperty("org.activiti.designer.bendpoints");
}
if(bendpointList != null && bendpointList.size() >= 0) {
for (GraphicInfo graphicInfo : bendpointList) {
Point bendPoint = StylesFactory.eINSTANCE.createPoint();
bendPoint.setX((int)graphicInfo.getX());
bendPoint.setY((int)graphicInfo.getY());
connection.getBendpoints().add(bendPoint);
}
} else {
Shape sourceShape = (Shape) getPictogramElement(sourceElement);
ILocation sourceShapeLocation = Graphiti.getLayoutService().getLocationRelativeToDiagram(sourceShape);
int sourceX = sourceShapeLocation.getX();
int sourceY = sourceShapeLocation.getY();
Shape targetShape = (Shape) getPictogramElement(targetElement);
ILocation targetShapeLocation = Graphiti.getLayoutService().getLocationRelativeToDiagram(targetShape);
int targetX = targetShapeLocation.getX();
int targetY = targetShapeLocation.getY();
if (sourceElement instanceof Gateway && targetElement instanceof Gateway == false) {
if (((sourceGraphics.getY() + 10) < targetGraphics.getY()
|| (sourceGraphics.getY() - 10) > targetGraphics.getY()) &&
(sourceGraphics.getX() + (sourceGraphics.getWidth() / 2)) < targetGraphics.getX()) {
boolean subProcessWithBendPoint = false;
if(targetElement instanceof SubProcess) {
int middleSub = targetGraphics.getY() + (targetGraphics.getHeight() / 2);
if((sourceGraphics.getY() + 20) < middleSub || (sourceGraphics.getY() - 20) > middleSub) {
subProcessWithBendPoint = true;
}
}
if(targetElement instanceof SubProcess == false || subProcessWithBendPoint == true) {
Point bendPoint = StylesFactory.eINSTANCE.createPoint();
bendPoint.setX(sourceX + 20);
bendPoint.setY(targetY + (targetGraphics.getHeight() / 2));
connection.getBendpoints().add(bendPoint);
}
}
} else if (targetElement instanceof Gateway) {
if (((sourceGraphics.getY() + 10) < targetGraphics.getY()
|| (sourceGraphics.getY() - 10) > targetGraphics.getY()) &&
(sourceGraphics.getX() + sourceGraphics.getWidth()) < targetGraphics.getX()) {
boolean subProcessWithBendPoint = false;
if(sourceElement instanceof SubProcess) {
int middleSub = sourceGraphics.getY() + (sourceGraphics.getHeight() / 2);
if((middleSub + 20) < targetGraphics.getY() || (middleSub - 20) > targetGraphics.getY()) {
subProcessWithBendPoint = true;
}
}
if(sourceElement instanceof SubProcess == false || subProcessWithBendPoint == true) {
Point bendPoint = StylesFactory.eINSTANCE.createPoint();
bendPoint.setX(targetX + 20);
bendPoint.setY(sourceY + (sourceGraphics.getHeight() / 2));
connection.getBendpoints().add(bendPoint);
}
}
} else if (targetElement instanceof EndEvent) {
int middleSource = sourceGraphics.getY() + (sourceGraphics.getHeight() / 2);
int middleTarget = targetGraphics.getY() + (targetGraphics.getHeight() / 2);
if (((middleSource + 10) < middleTarget &&
(sourceGraphics.getX() + sourceGraphics.getWidth()) < targetGraphics.getX()) ||
((middleSource - 10) > middleTarget &&
(sourceGraphics.getX() + sourceGraphics.getWidth()) < targetGraphics.getX())) {
Point bendPoint = StylesFactory.eINSTANCE.createPoint();
bendPoint.setX(targetX + (targetGraphics.getWidth() / 2));
bendPoint.setY(sourceY + (sourceGraphics.getHeight() / 2));
connection.getBendpoints().add(bendPoint);
}
}
}
IGaService gaService = Graphiti.getGaService();
Polyline polyline = gaService.createPolyline(connection);
polyline.setLineStyle(LineStyle.SOLID);
polyline.setForeground(Graphiti.getGaService().manageColor(getDiagram(), IColorConstant.BLACK));
// create link and wire it
link(connection, addedSequenceFlow);
// add dynamic text decorator for the reference name
ConnectionDecorator textDecorator = peCreateService.createConnectionDecorator(connection, true, 0.5, true);
MultiText text = gaService.createDefaultMultiText(getDiagram(), textDecorator);
text.setStyle(StyleUtil.getStyleForTask((getDiagram())));
text.setHorizontalAlignment(Orientation.ALIGNMENT_LEFT);
text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
if (OSUtil.getOperatingSystem() == OSEnum.Mac) {
text.setFont(gaService.manageFont(getDiagram(), text.getFont().getName(), 11));
}
if(addConContext.getProperty("org.activiti.designer.connectionlabel") != null) {
GraphicInfo labelLocation = (GraphicInfo) addConContext.getProperty("org.activiti.designer.connectionlabel");
gaService.setLocation(text, (int)labelLocation.getX(), (int)labelLocation.getY());
} else {
gaService.setLocation(text, 10, 0);
}
if (StringUtils.isNotEmpty(addedSequenceFlow.getName())) {
TextUtil.setTextSize(addedSequenceFlow.getName(), text);
}
// set reference name in the text decorator
text.setValue(addedSequenceFlow.getName());
// add static graphical decorators (composition and navigable)
ConnectionDecorator cd = peCreateService.createConnectionDecorator(connection, false, 1.0, true);
createArrow(cd);