@SuppressWarnings({"unchecked", "rawtypes"}) private void setSourceRef( DataAssociation association, ItemAwareElement value, EObject container, EStructuralFeature containerFeature) { if (association.getSourceRef().size() == 0) { if (container != null) { if (containerFeature.isMany()) ((List) container.eGet(containerFeature)).add(value); else container.eSet(containerFeature, value); } if (value == null) association.getSourceRef().clear(); else association.getSourceRef().add(value); updateConnectionIfNeeded(association, value); } else { if (container != null) { if (containerFeature.isMany()) ((List) container.eGet(containerFeature)).add(value); else container.eSet(containerFeature, value); } updateConnectionIfNeeded(association, value); if (value == null) association.getSourceRef().clear(); else association.getSourceRef().set(0, value); updateConnectionIfNeeded(association, value); } if (association.getTargetRef() != null) { ItemAwareElement targetRef = association.getTargetRef(); if (value != null) targetRef.setItemSubjectRef(value.getItemSubjectRef()); else targetRef.setItemSubjectRef(null); updateConnectionIfNeeded(association, value); } }
@SuppressWarnings({"unchecked", "rawtypes"}) private void setTargetRef( DataAssociation association, ItemAwareElement value, EObject container, EStructuralFeature containerFeature) { if (container != null) { if (containerFeature.isMany()) ((List) container.eGet(containerFeature)).add(value); else container.eSet(containerFeature, value); } updateConnectionIfNeeded(association, value); association.setTargetRef(value); if (!association.getSourceRef().isEmpty()) { ItemAwareElement sourceRef = association.getSourceRef().get(0); if (value != null) sourceRef.setItemSubjectRef(value.getItemSubjectRef()); else sourceRef.setItemSubjectRef(null); } updateConnectionIfNeeded(association, value); }
private void updateConnectionIfNeeded(DataAssociation association, ItemAwareElement value) { DiagramEditor diagramEditor = ModelUtil.getDiagramEditor(association); if (diagramEditor == null) return; boolean hasDoneChanges = false; Diagram diagram = diagramEditor.getDiagramTypeProvider().getDiagram(); IFeatureProvider fp = diagramEditor.getDiagramTypeProvider().getFeatureProvider(); Shape taskShape = null; EObject container = association.eContainer(); if (container instanceof Activity || container instanceof Event) { for (PictogramElement pe : Graphiti.getLinkService().getPictogramElements(diagram, container)) { if (pe instanceof Shape && BusinessObjectUtil.getFirstElementOfType(pe, BPMNShape.class) != null) { taskShape = (Shape) pe; break; } } } Shape dataShape = null; if (value instanceof DataObject || value instanceof DataObjectReference || value instanceof DataStore || value instanceof DataStoreReference || value instanceof DataInput || value instanceof DataOutput) { List<PictogramElement> pes = Graphiti.getLinkService().getPictogramElements(diagram, (EObject) value); for (PictogramElement p : pes) { if (BusinessObjectUtil.getFirstElementOfType(p, BPMNShape.class) != null) { dataShape = (Shape) p; break; } } } Connection connection = DataAssociationFeatureContainer.findDataAssociation(diagram, association); if (connection != null) { // There's an existing DataAssociation connection which needs to // either be reconnected or deleted, depending on what the combobox // selection is. if (dataShape != null) { // need to reconnect the DataAssociation ReconnectionContext rc = null; if (association instanceof DataInputAssociation) { Point p = GraphicsUtil.createPoint(connection.getStart()); Anchor a = AnchorUtil.createAnchor((AnchorContainer) dataShape, p); rc = new ReconnectionContext(connection, connection.getStart(), a, null); rc.setTargetPictogramElement(dataShape); rc.setTargetLocation(Graphiti.getPeService().getLocationRelativeToDiagram(a)); rc.setReconnectType(ReconnectionContext.RECONNECT_SOURCE); } else { Point p = GraphicsUtil.createPoint(connection.getEnd()); Anchor a = AnchorUtil.createAnchor(dataShape, p); rc = new ReconnectionContext(connection, a, connection.getEnd(), null); rc.setTargetPictogramElement(dataShape); rc.setTargetLocation(Graphiti.getPeService().getLocationRelativeToDiagram(a)); rc.setReconnectType(ReconnectionContext.RECONNECT_TARGET); } IReconnectionFeature rf = fp.getReconnectionFeature(rc); if (rf.canReconnect(rc)) { rf.reconnect(rc); hasDoneChanges = true; } } else { // need to delete the DataAssociation connection DeleteContext dc = new DeleteContext(connection); connection.getLink().getBusinessObjects().remove(0); IDeleteFeature df = fp.getDeleteFeature(dc); df.delete(dc); } } else if (dataShape != null) { // There is no existing DataAssociation connection, but the newly selected source or target // is some kind of data object shape, so we need to create a connection between the Activity // (or Throw/Catch Event) that owns the DataAssociation, and the new data object shape. Point p = GraphicsUtil.createPoint((AnchorContainer) dataShape); Anchor ownerAnchor = AnchorUtil.createAnchor(taskShape, p); p = GraphicsUtil.createPoint(taskShape); Anchor peAnchor = AnchorUtil.createAnchor((AnchorContainer) dataShape, p); AddConnectionContext ac = null; if (association instanceof DataOutputAssociation) { ac = new AddConnectionContext(ownerAnchor, peAnchor); } else { ac = new AddConnectionContext(peAnchor, ownerAnchor); } ac.putProperty(GraphitiConstants.BUSINESS_OBJECT, association); ac.setNewObject(association); IAddFeature af = fp.getAddFeature(ac); if (af.canAdd(ac)) { PictogramElement pe = af.add(ac); if (pe instanceof Connection) { connection = (Connection) pe; hasDoneChanges = true; } } } if (hasDoneChanges) { FeatureSupport.updateConnection( diagramEditor.getDiagramTypeProvider().getFeatureProvider(), connection); } }