public override void ElementAdded(ElementAddedEventArgs e)
        {
            if (e.ModelElement != null)
                if (e.ModelElement.Store.TransactionManager.CurrentTransaction != null)
                    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing)
                        return;

            if (e.ModelElement == null)
                return;

            if (ImmutabilityExtensionMethods.GetLocks(e.ModelElement) != Locks.None)
                return;

            DomainClass domainClass = e.ModelElement as DomainClass;
            if (domainClass != null)
            {
                if (domainClass.DomainModelTreeNodes.Count == 0)
                {
                    RootNode node = new RootNode(domainClass.Store);
                    node.DomainElement = domainClass;
                    node.IsElementHolder = true;

                    // add to the domain model diagram tree
                    domainClass.ModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(node);
                    domainClass.ModelContext.ViewContext.DomainModelTreeView.RootNodes.Add(node);
                }
            }
        }
        public override void ElementDeleting(ElementDeletingEventArgs e)
        {
            if (e.ModelElement != null)
                if (e.ModelElement.Store.TransactionManager.CurrentTransaction != null)
                    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing)
                        return;

            if (e.ModelElement == null)
                return;

            if (ImmutabilityExtensionMethods.GetLocks(e.ModelElement) != Locks.None)
                return;

            TreeNodeReferencesReferenceRSNodes con = e.ModelElement as TreeNodeReferencesReferenceRSNodes;
            if (con != null)
            {
                ReferenceRSNode rsNode = con.ReferenceRSNode;
                if (rsNode != null)
                {
                    ReferenceNode node = rsNode.ReferenceNode;
                    if (node != null)
                    {
                        // since this node still exists, it isnt included in the deletion process
                        // --> move to root node if this node is the element holder, as its parent has been deleted
                        if (node.IsElementHolder)
                        {
                            RootNode rootNode = new RootNode(node.Store);
                            rootNode.DomainElement = node.DomainElement;

                            node.EmbeddingModelTreeViewModel.RootNodes.Add(rootNode);
                            node.EmbeddingModelTreeViewModel.ModelTreeNodes.Add(rootNode);

                            // copy sub tree
                            for (int i = node.EmbeddingRSNodes.Count - 1; i >= 0; i--)
                                node.EmbeddingRSNodes[i].TreeNode = rootNode;

                            for (int i = node.ReferenceRSNodes.Count - 1; i >= 0; i--)
                                node.ReferenceRSNodes[i].TreeNode = rootNode;

                            for (int i = node.InheritanceNodes.Count - 1; i >= 0; i--)
                                node.InheritanceNodes[i].TreeNode = rootNode;

                            for (int i = node.ShapeClassNodes.Count - 1; i >= 0; i--)
                                node.ShapeClassNodes[i].TreeNode = rootNode;

                            rootNode.IsElementHolder = true;
                            rootNode.IsEmbeddingTreeExpanded = node.IsEmbeddingTreeExpanded;
                            rootNode.IsExpanded = true;
                            rootNode.IsInheritanceTreeExpanded = node.IsInheritanceTreeExpanded;
                            rootNode.IsReferenceTreeExpanded = node.IsReferenceTreeExpanded;
                            rootNode.IsShapeMappingTreeExpanded = node.IsShapeMappingTreeExpanded;
                        }

                        node.Delete();
                    }

                    rsNode.Delete();
                }
            }
        }
        public override void ModelFinalize(CopyPaste.ModelProtoElement protoElement, CopyPaste.ModelProtoGroupMerger groupMerger)
        {
            base.ModelFinalize(protoElement, groupMerger);

            DomainClass domainClass = this.Store.ElementDirectory.FindElement(groupMerger.GetIdMapping(protoElement.ElementId)) as DomainClass;
            if (domainClass == null)
                return;

            if (domainClass.IsDomainModel)
                domainClass.IsDomainModel = false;

            if (domainClass.BaseClass != null)
            {
                ReadOnlyCollection<DomainClassReferencesBaseClass> col = DomainRoleInfo.GetElementLinks<DomainClassReferencesBaseClass>(domainClass, DomainClassReferencesBaseClass.DerivedClassDomainRoleId);
                if (col.Count != 1)
                {
                    throw new ArgumentNullException("Domain class can only reference one base class");
                }

                DomainClassReferencesBaseClass r = col[0];
                r.InhNodeId = Guid.Empty;     // otherwise node id of the source element would be used
            }

            if (domainClass.DomainModelTreeNodes.Count == 0)
            {
                RootNode node = new RootNode(domainClass.Store);
                node.DomainElement = domainClass;
                node.IsElementHolder = true;

                // add to the domain model diagram tree
                domainClass.ModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(node);
                domainClass.ModelContext.ViewContext.DomainModelTreeView.RootNodes.Add(node);
            }

            SerializationDomainClassAddRule.OnDomainClassAdded(domainClass);
            SerializationHelper.UpdateDerivedElementsSerializationProperties(domainClass);
        }
        private static void FixUpDomainModelTreeView(LibraryModelContext model)
        {
            // create root nodes and embedding rs
            foreach (DomainClass domainClass in model.Classes)
            {
                if (!domainClass.HasParent())
                {
                    RootNode node = new RootNode(model.Store);
                    node.DomainElement = domainClass;
                    node.IsElementHolder = true;
                    domainClass.ModelContext.ViewContext.DomainModelTreeView.RootNodes.Add(node);
                    domainClass.ModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(node);

                    node.IsEmbeddingTreeExpanded = true;
                    node.IsReferenceTreeExpanded = true;
                    node.IsInheritanceTreeExpanded = true;
                    node.IsShapeMappingTreeExpanded = true;

                    FixUpDomainClassInTreeView(domainClass);
                }
            }

            // reference relationships + shapes
            foreach (DomainRelationship relationship in model.Relationships)
            {
                if (relationship is ReferenceRelationship)
                {
                    ReferenceRelationship rel = relationship as ReferenceRelationship;
                    ModelTreeHelper.AddNewReferenceRelationship(rel, rel.Source.RolePlayer as DomainClass, rel.Target.RolePlayer);

                    // shape
                    foreach (RelationshipShapeClass s in rel.RelationshipShapeClasses)
                    {
                        ReferenceRSNode node = rel.ReferenceRSNode;

                        // create new shape relationship node
                        ShapeRelationshipNode shapeNode = new ShapeRelationshipNode(rel.Store);
                        shapeNode.RelationshipShapeClass = s;

                        node.ShapeRelationshipNodes.Add(shapeNode);
                        rel.ModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(shapeNode);
                    }
                }
            }

            // inheritance
            foreach (DomainClass domainClass in model.Classes)
            {
                if (domainClass.BaseClass != null)
                {
                    DomainClassReferencesBaseClass refBase = DomainClassReferencesBaseClass.GetLink(domainClass, domainClass.BaseClass);
                    if (refBase != null)
                    {
                        if (domainClass.DomainModelTreeNodes.Count > 0)
                            ModelTreeHelper.AddNewInheritanceRelationship(refBase, domainClass, domainClass.BaseClass, false);
                        else
                            ModelTreeHelper.AddNewInheritanceRelationship(refBase, domainClass, domainClass.BaseClass, true);
                    }
                }
            }

            // shapes
            foreach (DomainClass domainClass in model.Classes)
                foreach (PresentationDomainClassElement p in domainClass.ShapeClasses)
                {
                    foreach (TreeNode node in domainClass.DomainModelTreeNodes)
                    {
                        if (node.IsElementHolder)
                        {
                            ShapeClassNode shapeNode = new ShapeClassNode(domainClass.Store);
                            shapeNode.ShapeClass = p;

                            node.ShapeClassNodes.Add(shapeNode);
                            domainClass.ModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(shapeNode);
                            break;
                        }
                    }
                }

        }
        public override void ElementDeleting(ElementDeletingEventArgs e)
        {
            if (e.ModelElement != null)
                if (e.ModelElement.Store.TransactionManager.CurrentTransaction != null)
                    if (e.ModelElement.Store.TransactionManager.CurrentTransaction.IsSerializing)
                        return;

            if (e.ModelElement == null)
                return;

            if (ImmutabilityExtensionMethods.GetLocks(e.ModelElement) != Locks.None)
                return;

            ReferenceRelationship referenceRelationship = e.ModelElement as ReferenceRelationship;
            if (referenceRelationship != null)
            {
                if (referenceRelationship.ReferenceRSNode != null)
                    referenceRelationship.ReferenceRSNode.Delete();
            }

            EmbeddingRelationship embeddingRelationship = e.ModelElement as EmbeddingRelationship;
            if (embeddingRelationship != null)
            {
                if(embeddingRelationship.EmbeddingRSNode != null )
                    embeddingRelationship.EmbeddingRSNode.Delete();
            }

            DomainClassReferencesBaseClass inhRelationship = e.ModelElement as DomainClassReferencesBaseClass;
            if (inhRelationship != null)
            {
                InheritanceNode node = inhRelationship.Store.ElementDirectory.FindElement(inhRelationship.InhNodeId) as InheritanceNode;
                if (node != null)
                {
                    if (node.IsElementHolder)
                    {
                        RootNode rootNode = new RootNode(node.Store);
                        rootNode.DomainElement = node.DomainElement;

                        rootNode.IsElementHolder = true;
                        rootNode.IsEmbeddingTreeExpanded = node.IsEmbeddingTreeExpanded;
                        rootNode.IsExpanded = true;
                        rootNode.IsInheritanceTreeExpanded = node.IsInheritanceTreeExpanded;
                        rootNode.IsReferenceTreeExpanded = node.IsReferenceTreeExpanded;
                        rootNode.IsShapeMappingTreeExpanded = node.IsShapeMappingTreeExpanded;

                        // copy sub tree
                        for (int i = node.EmbeddingRSNodes.Count - 1; i >= 0; i--)
                            node.EmbeddingRSNodes[i].TreeNode = rootNode;

                        for (int i = node.ReferenceRSNodes.Count - 1; i >= 0; i--)
                            node.ReferenceRSNodes[i].TreeNode = rootNode;

                        for (int i = node.InheritanceNodes.Count - 1; i >= 0; i--)
                            node.InheritanceNodes[i].TreeNode = rootNode;

                        for (int i = node.ShapeClassNodes.Count - 1; i >= 0; i--)
                            node.ShapeClassNodes[i].TreeNode = rootNode;

                        node.DomainElement.ParentModelContext.ViewContext.DomainModelTreeView.RootNodes.Add(rootNode);
                        node.DomainElement.ParentModelContext.ViewContext.DomainModelTreeView.ModelTreeNodes.Add(rootNode);
                    }

                    // connection was deleted using the property window and not the menu item, so delete the rs and the
                    // node here
                    //if (inhNode.IsElementHolder)
                    //    TreeOperations.SplitTree(inhNode);

                    node.Delete();
                }

            }
        }