/// <summary> /// Called whenever the classes collection has changed. /// </summary> /// <param name="sender">Object that has raised the event</param> /// <param name="e">Information about the change</param> protected void OnClassesChanged(object sender, NotifyCollectionChangedEventArgs e) { if (e.Action == NotifyCollectionChangedAction.Add) { foreach (PIMClass cls in e.NewItems) { _ImplClass implCls = cls as _ImplClass; if (implCls == null) { classes.Remove(cls); throw new ArgumentException("A class created outside the model library inserted into classes!"); } implCls.Package = this; if (!adaptedElement.OwnedType.Contains(implCls.AdaptedClass)) { adaptedElement.OwnedType.Add(implCls.AdaptedClass); } } } else if (e.Action == NotifyCollectionChangedAction.Remove) { foreach (PIMClass cls in e.OldItems) { adaptedElement.OwnedType.Remove(((_ImplClass)cls).AdaptedClass); ((_ImplClass)cls).Package = null; } } }
/// <summary> /// Changes the application domain of the stereotype. /// It removes extensions to the metaclasses that were removed from the domain /// and adds extensions to those that were added. /// </summary> protected void OnApplicationDomainChanged(object sender, NotifyCollectionChangedEventArgs e) { // Access the parent profile. // The cast is safe as the type is checked when the package property is being set _Profile profile = package as _Profile; if (e.Action == NotifyCollectionChangedAction.Add) { foreach (string metaClassName in e.NewItems) { _ImplClass metaClass = null; foreach (Model metamodel in profile.MetamodelReference) { // Verify that the metamodel contains the metaclass // Otherwise throw exception metaClass = (_ImplClass)metamodel.Classes.Get(metaClassName); if (metaClass != null) { break; } } if (metaClass == null) { throw new ArgumentException("The profile that contains this stereotype does not reference " + "any metamodel that contains metaclass that the extended element is an instance of!"); } NUml.Uml2.Extension extension = NUml.Uml2.Create.Extension(); NUml.Uml2.Property end0 = NUml.Uml2.Create.Property(); NUml.Uml2.ExtensionEnd end1 = NUml.Uml2.Create.ExtensionEnd(); // Initialize the extension and its ends extension.Name = adaptedElement.Name + " extends " + metaClassName; end0.Name = "base_" + metaClassName; end0.Visibility = NUml.Uml2.VisibilityKind.@private; end0.Association = extension; end0.Type = metaClass.AdaptedClass; end1.Name = "extension_" + adaptedElement.Name; end1.Owner = extension; end1.Visibility = NUml.Uml2.VisibilityKind.@private; end1.Type = (NUml.Uml2.Stereotype)adaptedElement; // Assign extension ends correctly to both extension and stereotype extension.OwnedEnd = end1; extension.MemberEnd.Add(end0); extensionEnds.Add(end0); adaptedElement.OwnedAttribute.Add(end0); profile.Adaptee.OwnedMember.Add(extension); } } else if (e.Action == NotifyCollectionChangedAction.Remove) { foreach (string metaClassName in e.OldItems) { // Find the extension end owned by the stereotype (by name) string endName = "base_" + metaClassName; NUml.Uml2.Property end = null; foreach (NUml.Uml2.Property extEnd in extensionEnds) { if (extEnd.Name == endName) { end = extEnd; break; } } // If the extension end was not found, continue // (it is no more in the collection for some reason) if (end == null) { continue; } // Remove the extension from the stereotype and the parent profile extensionEnds.Remove(end); adaptedElement.OwnedAttribute.Remove(end); NUml.Uml2.Association extension = end.Association; profile.Adaptee.OwnedMember.Remove(extension); } } }