/// <summary> /// Called by the base ConnectAction class to create the underlying relationship. /// </summary> /// <remarks> /// This implementation delegates calls to the ConnectionBuilder AssociationConnectorBuilder. /// </remarks> public override void CreateConnection(DslDiagrams::ShapeElement sourceShapeElement, DslDiagrams::ShapeElement targetShapeElement, DslDiagrams::PaintFeedbackArgs paintFeedbackArgs) { if (sourceShapeElement == null) { throw new global::System.ArgumentNullException("sourceShapeElement"); } if (targetShapeElement == null) { throw new global::System.ArgumentNullException("targetShapeElement"); } sourceShapeElement = RemovePassThroughShapes(sourceShapeElement); targetShapeElement = RemovePassThroughShapes(targetShapeElement); DslModeling::ModelElement sourceElement = sourceShapeElement.ModelElement; if (sourceElement == null) { sourceElement = sourceShapeElement; } DslModeling::ModelElement targetElement = targetShapeElement.ModelElement; if (targetElement == null) { targetElement = targetShapeElement; } // You will need to implement a method with the following signature: // public static void Connect(DslModeling::ModelElement sourceElement, DslModeling::ModelElement targetElement) // { // } // in a partial class of AssociationConnectorBuilder. AssociationConnectorBuilder.Connect(sourceElement, targetElement); }
/// <summary> /// Called by the base ConnectAction class to determine if the given shapes can be connected. /// </summary> /// <remarks> /// This implementation delegates calls to the ConnectionBuilder AssociationConnectorBuilder. /// </remarks> public override bool CanCreateConnection(DslDiagrams::ShapeElement sourceShapeElement, DslDiagrams::ShapeElement targetShapeElement, ref string connectionWarning) { bool canConnect = true; if (sourceShapeElement == null) { throw new global::System.ArgumentNullException("sourceShapeElement"); } sourceShapeElement = RemovePassThroughShapes(sourceShapeElement); DslModeling::ModelElement sourceElement = sourceShapeElement.ModelElement; if (sourceElement == null) { sourceElement = sourceShapeElement; } DslModeling::ModelElement targetElement = null; if (targetShapeElement != null) { targetShapeElement = RemovePassThroughShapes(targetShapeElement); targetElement = targetShapeElement.ModelElement; if (targetElement == null) { targetElement = targetShapeElement; } } // base.CanCreateConnection must be called to check whether existing Locks prevent this link from getting created. canConnect = base.CanCreateConnection(sourceShapeElement, targetShapeElement, ref connectionWarning); if (canConnect) { if (targetShapeElement == null) { // You will need to implement a method with the following signature: // public static bool CanAcceptSource(DslModeling::ModelElement sourceElement) // { // } // in a partial class of AssociationConnectorBuilder. return(AssociationConnectorBuilder.CanAcceptSource(sourceElement)); } else { // You will need to implement a method with the following signature: // public static bool CanAcceptSourceAndTarget(DslModeling::ModelElement sourceElement, DslModeling::ModelElement targetElement) // { // } // in a partial class of AssociationConnectorBuilder. return(AssociationConnectorBuilder.CanAcceptSourceAndTarget(sourceElement, targetElement)); } } else { //return false return(canConnect); } }
protected override void OnCopy(ModelElement sourceElement) { base.OnCopy(sourceElement); NavigationProperty sourceProperty = (NavigationProperty)sourceElement; this.TypedEntitySet = sourceProperty.TypedEntitySet; this.KeyAttribute = Common.ExtensionMethods.Clone(sourceProperty.KeyAttribute); using (ValidationContextRegion.DisableAll()) { // this will add delete which dispose validation region when transaction completes/rolled-back /*this.Store.RegisterActionOnTransactionEvent(TransactionEvent.Committed | TransactionEvent.RolledBack, * disabledValidationRegion, (args, disposable) => disposable.Dispose());*/ if (sourceProperty.PersistentTypeHasAssociations != null) { this.PersistentTypeHasAssociations = (PersistentTypeHasAssociations)sourceProperty.PersistentTypeHasAssociations.Copy( new[] { PersistentTypeHasAssociations.TargetPersistentTypeDomainRoleId, PersistentTypeHasAssociations.SourcePersistentTypeDomainRoleId }); } } if (this.PersistentTypeHasAssociations != null) { this.PersistentTypeHasAssociations.TargetPersistentType = sourceProperty.PersistentTypeHasAssociations.TargetPersistentType; this.PersistentTypeHasAssociations.Name = AssociationConnectorBuilder.BuildAssociationName( this.PersistentTypeHasAssociations.SourcePersistentType, this.PersistentTypeHasAssociations.TargetPersistentType); } }
public override void ExecCommand(MenuCommand menuCommand) { CurrentModelSelection modelSelection = GetCurrentSelectedPersistentType(); EntityDiagram entityDiagram = modelSelection.GetFromSelection <EntityDiagram>(false).SingleOrDefault(); PersistentType persistentType = modelSelection.IsPersistentTypeSelected ? modelSelection.CurrentPersistentType : null; Store store = persistentType != null ? persistentType.Store : entityDiagram.Store; IEnumerable <PersistentTypeItem> existingTypeNames = store.ElementDirectory.FindElements <PersistentType>() .Where(item => item.TypeKind.In(PersistentTypeKind.Entity, PersistentTypeKind.Interface, PersistentTypeKind.Structure)) .Select(item => new PersistentTypeItem(item.Name, ModelUtil.ConvertTo(item.TypeKind), item.AllProperties.Select(prop => prop.Name).ToArray())); PersistentTypeItem persistentTypeEnd1 = persistentType == null ? null : new PersistentTypeItem( persistentType.Name, ModelUtil.ConvertTo(persistentType.TypeKind), persistentType.AllProperties.Select(prop => prop.Name). ToArray()); Func <PersistentTypeItem, PersistentType> findTypeFunc = (typeItemd) => store.ElementDirectory .FindElements <PersistentType>() .Where(item => item.Name == typeItemd.Name && ModelUtil.ConvertTo(item.TypeKind) == typeItemd.Kind) .SingleOrDefault(); IEnumerable <string> existingAssociations = store.ElementDirectory.FindElements <PersistentTypeHasAssociations>().Select(assoc => assoc.Name).ToArray(); if (existingTypeNames.Count() < 2) { MessageBox.Show("There must be at least 2 persistent types to make association.", "Add Association...", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //FormAddAssociation.ResultData resultData; //if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData)) FormAddAssociation.ResultData resultData; if (FormAddAssociation.DialogShow(existingTypeNames, existingAssociations, persistentTypeEnd1, out resultData)) { modelSelection.MakeActionWithinTransaction(string.Format("Add new association '{0}'", resultData.AssociationName), delegate { PersistentType sourcePersistentType = findTypeFunc(resultData.PersistentItem1.TypeItem); PersistentType targetPersistentType = findTypeFunc(resultData.PersistentItem2.TypeItem); IAssociationInfo sourceInfo = new AssociationInfo { Multiplicity = resultData.PersistentItem2.Multiplicity, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default, UseAssociationAttribute = resultData.PersistentItem2.UseAssociationAttribute }; sourceInfo.PairTo.SetAsCustom(resultData.PersistentItem1.PropertyName); IAssociationInfo targetInfo = new AssociationInfo { Multiplicity = resultData.PersistentItem1.Multiplicity, OnOwnerRemove = AssociationOnRemoveAction.Default, OnTargetRemove = AssociationOnRemoveAction.Default, UseAssociationAttribute = resultData.PersistentItem1.UseAssociationAttribute }; targetInfo.PairTo.SetAsCustom(resultData.PersistentItem2.PropertyName); if (resultData.SimpleMode && sourcePersistentType == targetPersistentType) { targetInfo = sourceInfo; targetPersistentType = sourcePersistentType; } var persistentTypesAssociation = AssociationConnectorBuilder.CreatePersistentTypesAssociation(sourcePersistentType, targetPersistentType, sourceInfo, targetInfo, resultData.AssociationName, true, !resultData.SimpleMode); Owner.SelectModelElement((ModelElement)persistentTypesAssociation); }); } }