Beispiel #1
0
        private void tsbDelete_Click(object sender, EventArgs e)
        {
            string message = "Are you sure you want to remove the ";

            message += tvCustomProperties.SelectedNode.Level == _groupLevel ? "group " : "definition ";
            XmlNode      node = tvCustomProperties.SelectedNode.Tag as XmlNode;
            ModelElement elem = tvCustomProperties.SelectedNode.Tag as ModelElement;
            string       name = null;

            if (node != null)
            {
                name = node.Attributes["name"].Value;
            }
            if (elem != null)
            {
                if (elem is CustomPropertyGroup)
                {
                    name = ((CustomPropertyGroup)elem).Name;
                }
                else
                {
                    name = ((CustomPropertyDefinition)elem).Name;
                }
            }
            message += "named \"" + name + "\"?";

            DialogResult rslt = MessageBox.Show(message, "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

            if (rslt == DialogResult.Yes)
            {
                TreeNode rootNode = null;
                switch (tvCustomProperties.SelectedNode.Level)
                {
                case _groupLevel:
                    rootNode = tvCustomProperties.SelectedNode.Parent;
                    break;

                case _definitionLevel:
                    rootNode = tvCustomProperties.SelectedNode.Parent.Parent;
                    break;
                }
                if (rootNode == _machineNode)
                {
                    node.ParentNode.RemoveChild(node);
                }
                else
                {
                    elem.Delete();
                }
                tvCustomProperties.SelectedNode.Remove();
            }
        }
        /// <summary>
        /// Post process gathered information.
        /// </summary>
        /// <param name="serializationResult">Serialization result.</param>
        /// <param name="store">Store.</param>
        protected virtual void DoPostProcess(Microsoft.VisualStudio.Modeling.SerializationResult serializationResult, Store store)
        {
            foreach (System.Guid domainClassId in this.dictionary.Keys)
            {
                DomainRelationshipInfo relationshipInfo = store.DomainDataDirectory.FindDomainRelationship(domainClassId);
                if (relationshipInfo == null)
                {
                    SerializationUtilities.AddMessage(serializationResult, "", SerializationMessageKind.Warning,
                                                      "Couldn't find domain relationship data. DomainClassId: " + domainClassId, 0, 0);
                    continue;
                }
                foreach (PostProcessRelationshipData data in this.dictionary[domainClassId])
                {
                    // get source and target elements
                    ModelElement source = store.ElementDirectory.FindElement(data.ModelElementSourceId);
                    ModelElement target = store.ElementDirectory.FindElement(data.ModelElementTargetId);
                    if (target == null) // target is link ?
                    {
                        target = store.ElementDirectory.FindElementLink(data.ModelElementTargetId);
                    }

                    if (source == null)
                    {
                        SerializationUtilities.AddMessage(serializationResult, "", SerializationMessageKind.Warning,
                                                          "Couldn't find the source element of the relationship " + relationshipInfo.Name + ". Id of missing element: " + data.ModelElementSourceId, 0, 0);
                        continue;
                    }
                    if (target == null)
                    {
                        SerializationUtilities.AddMessage(serializationResult, "", SerializationMessageKind.Warning,
                                                          "Couldn't find the target element of the relationship " + relationshipInfo.Name + ". Id of missing element: " + data.ModelElementTargetId, 0, 0);
                        continue;
                    }

                    if (data.RelationshipId == System.Guid.Empty)
                    {
                        try
                        {
                            // create new relationship
                            RoleAssignment[] roleAssignments = new RoleAssignment[2];
                            roleAssignments[0] = new RoleAssignment(GetSourceDomainRole(relationshipInfo).Id, source);
                            roleAssignments[1] = new RoleAssignment(GetTargetDomainRole(relationshipInfo).Id, target);

                            store.ElementFactory.CreateElementLink(relationshipInfo, roleAssignments);
                        }
                        catch (System.Exception ex)
                        {
                            SerializationUtilities.AddMessage(serializationResult, "", SerializationMessageKind.Warning,
                                                              "Error while creating new instance of relationship " + relationshipInfo.Name + " between " + source.Id.ToString() + " and " + target.Id.ToString() + ".  Exception: " + ex.Message, 0, 0);
                        }
                    }
                    else
                    {
                        try
                        {
                            // assign role players
                            ElementLink instance = store.ElementDirectory.FindElementLink(data.RelationshipId);
                            if (instance == null)
                            {
                                throw new System.ArgumentNullException("Post processing failed because relationship (id=" + data.RelationshipId + ") is not in the store");
                            }

                            DomainRoleInfo.SetRolePlayer(instance, GetSourceDomainRole(relationshipInfo).Id, source);
                            DomainRoleInfo.SetRolePlayer(instance, GetTargetDomainRole(relationshipInfo).Id, target);

                            this.trackDictionary.Remove(instance.Id);
                        }
                        catch (System.Exception ex)
                        {
                            SerializationUtilities.AddMessage(serializationResult, "", SerializationMessageKind.Warning,
                                                              "Error while creating the instance of the relationship " + relationshipInfo.Name + " (id: " + data.RelationshipId + ") between " + source.Id.ToString() + " and " + target.Id.ToString() + ".  Exception: " + ex.Message, 0, 0);
                        }
                    }
                }
            }

            foreach (Guid id in this.trackDictionary)
            {
                ModelElement m = store.ElementDirectory.FindElement(id);
                if (m != null)
                {
                    m.Delete();
                }
            }
        }