/// <summary>See <see cref="DiagramDocView.Dispose"/>.</summary> protected override void Dispose(bool disposing) { try { if (disposing) { Store store; ModelingEventManager eventMgr; if (null != (store = Store) && DocData.IsLoaded && null != (eventMgr = ModelingEventManager.GetModelingEventManager(store))) { ManageModelingEventHandlers(eventMgr, EventSubscriberReasons.DocumentLoaded | EventSubscriberReasons.ModelStateEvents | EventSubscriberReasons.UserInterfaceEvents, EventHandlerAction.Remove); } if (myDocViewControl != null) { myDocViewControl.Parent.Dispose(); } } } finally { base.Dispose(disposing); } }
/// <summary> /// Manages <see cref="EventHandler{TEventArgs}"/>s in the <see cref="Store"/> during activation and /// deactivation. /// </summary> /// <param name="store">The <see cref="Store"/> for which the <see cref="EventHandler{TEventArgs}"/>s should be managed.</param> /// <param name="action">The <see cref="EventHandlerAction"/> that should be taken for the <see cref="EventHandler{TEventArgs}"/>s.</param> private void ManageStoreEvents(Store store, EventHandlerAction action) { if (store == null || store.Disposed) { return; // bail out } ModelingEventManager.GetModelingEventManager(store).AddOrRemoveHandler(store.DomainDataDirectory.FindDomainClass(ReferenceModeKind.DomainClassId), new EventHandler <ElementPropertyChangedEventArgs>(ReferenceModeKindChangeEvent), action); }
/// <summary> /// Manages <see cref="EventHandler{TEventArgs}"/>s in the <see cref="Store"/> during connect action activation and deactivation. /// The default implementation watches for new <see cref="UniquenessConstraint"/>s added to the <see cref="ORMModel"/>. /// </summary> /// <param name="store">The <see cref="Store"/> for which the <see cref="EventHandler{TEventArgs}"/>s should be managed.</param> /// <param name="action">The <see cref="EventHandlerAction"/> that should be taken for the <see cref="EventHandler{TEventArgs}"/>s.</param> protected virtual void ManageStoreEvents(Store store, EventHandlerAction action) { if (store == null || store.Disposed) { return; // bail out } ModelingEventManager.GetModelingEventManager(store).AddOrRemoveHandler(store.DomainDataDirectory.FindDomainClass(UniquenessConstraint.DomainClassId), new EventHandler <ElementAddedEventArgs>(InternalConstraintAddedEvent), action); }
/// <summary> /// Detach <see cref="EventHandler{TEventArgs}"/>s from the <see cref="Store"/> associated with the <see cref="ORMDesignerDocData"/>. Defers to <see cref="ManageEventHandlers"/>. /// </summary> protected void DetachEventHandlers(ORMDesignerDocData docData) { Store store = docData.Store; if (store != null) { ManageEventHandlers(store, ModelingEventManager.GetModelingEventManager(store), EventHandlerAction.Remove); } }
/// <summary> /// Attach <see cref="EventHandler{TEventArgs}"/>s to the <see cref="Store"/> associated with the <see cref="ORMDesignerDocData"/>. Defers to <see cref="ManageEventHandlers"/>. /// </summary> protected void AttachEventHandlers(ORMDesignerDocData docData) { Store store = docData.Store; if (null != store) { ManageEventHandlers(store, ModelingEventManager.GetModelingEventManager(store), EventHandlerAction.Add); } }
/// <summary> /// Manages <see cref="EventHandler{TEventArgs}"/>s in the <see cref="Store"/> during activation and /// deactivation. /// </summary> /// <param name="store">The <see cref="Store"/> for which the <see cref="EventHandler{TEventArgs}"/>s should be managed.</param> /// <param name="action">The <see cref="EventHandlerAction"/> that should be taken for the <see cref="EventHandler{TEventArgs}"/>s.</param> private void ManageStoreEvents(Store store, EventHandlerAction action) { if (store == null || store.Disposed) { return; // bail out } DomainDataDirectory dataDirectory = store.DomainDataDirectory; ModelingEventManager eventManager = ModelingEventManager.GetModelingEventManager(store); DomainClassInfo classInfo = dataDirectory.FindDomainClass(ReferenceModeKind.DomainClassId); eventManager.AddOrRemoveHandler(classInfo, new EventHandler <ElementPropertyChangedEventArgs>(ReferenceModeKindChangeEvent), action); classInfo = dataDirectory.FindDomainClass(CustomReferenceMode.DomainClassId); eventManager.AddOrRemoveHandler(classInfo, new EventHandler <ElementPropertyChangedEventArgs>(CustomReferenceModeChangeEvent), action); classInfo = dataDirectory.FindDomainRelationship(ModelHasReferenceMode.DomainClassId); eventManager.AddOrRemoveHandler(classInfo, new EventHandler <ElementAddedEventArgs>(CustomReferenceModeAddEvent), action); eventManager.AddOrRemoveHandler(classInfo, new EventHandler <ElementDeletedEventArgs>(CustomReferenceModeRemoveEvent), action); classInfo = dataDirectory.FindDomainRelationship(ReferenceModeHasReferenceModeKind.DomainClassId); eventManager.AddOrRemoveHandler(classInfo, new EventHandler <RolePlayerChangedEventArgs>(ReferenceModeHasKindChangeEvent), action); }
/// <summary> /// Load a model from an input stream /// </summary> /// <param name="inputStream">The stream to input from.</param> /// <returns>A <see cref="Store"/> with the model loaded.</returns> public Store Load(Stream inputStream) { List <string> unrecognizedNamespaces = null; Stream namespaceStrippedStream = null; Store store = null; try { XmlReaderSettings readerSettings = new XmlReaderSettings(); ExtensionLoader extensionLoader = myExtensionLoader; readerSettings.CloseInput = false; Dictionary <string, ExtensionModelBinding> documentExtensions = null; using (XmlReader reader = XmlReader.Create(inputStream, readerSettings)) { reader.MoveToContent(); if (reader.NodeType == XmlNodeType.Element) { if (reader.MoveToFirstAttribute()) { do { if (reader.Prefix == "xmlns") { string URI = reader.Value; if (!string.Equals(URI, ORMCoreDomainModel.XmlNamespace, StringComparison.Ordinal) && !string.Equals(URI, ORMShapeDomainModel.XmlNamespace, StringComparison.Ordinal) && !string.Equals(URI, ORMSerializationEngine.RootXmlNamespace, StringComparison.Ordinal)) { ExtensionModelBinding?extensionType = extensionLoader.GetExtensionDomainModel(URI); if (extensionType.HasValue) { if (documentExtensions == null) { documentExtensions = new Dictionary <string, ExtensionModelBinding>(); } documentExtensions[URI] = extensionType.Value; } else { (unrecognizedNamespaces ?? (unrecognizedNamespaces = new List <string>())).Add(URI); } } } } while (reader.MoveToNextAttribute()); } } } extensionLoader.VerifyRequiredExtensions(ref documentExtensions); if (unrecognizedNamespaces != null) { inputStream.Position = 0; namespaceStrippedStream = ExtensionLoader.CleanupStream(inputStream, extensionLoader.StandardDomainModels, documentExtensions.Values, unrecognizedNamespaces); if (namespaceStrippedStream != null) { inputStream = namespaceStrippedStream; } else { unrecognizedNamespaces = null; } } inputStream.Position = 0; store = CreateStore(); store.UndoManager.UndoState = UndoState.Disabled; store.LoadDomainModels(extensionLoader.GetRequiredDomainModels(documentExtensions)); try { ModelingEventManager eventManager = ModelingEventManager.GetModelingEventManager(store); using (Transaction t = store.TransactionManager.BeginTransaction("File load and fixup")) { foreach (IModelingEventSubscriber subscriber in Utility.EnumerateDomainModels <IModelingEventSubscriber>(store.DomainModels)) { subscriber.ManageModelingEventHandlers(eventManager, EventSubscriberReasons.DocumentLoading | EventSubscriberReasons.ModelStateEvents, EventHandlerAction.Add); } if (inputStream.Length > 1) { (new ORMSerializationEngine(store)).Load(inputStream); } t.Commit(); } foreach (IModelingEventSubscriber subscriber in Utility.EnumerateDomainModels <IModelingEventSubscriber>(store.DomainModels)) { subscriber.ManageModelingEventHandlers(eventManager, EventSubscriberReasons.DocumentLoaded | EventSubscriberReasons.ModelStateEvents, EventHandlerAction.Add); } store.UndoManager.UndoState = UndoState.Enabled; } catch (TypeInitializationException ex) { // If the type that failed to load is an extensions, then remove it from // the list of available extensions and try again. if (documentExtensions != null) { string typeName = ex.TypeName; foreach (KeyValuePair <string, ExtensionModelBinding> pair in documentExtensions) { Type testType = pair.Value.Type; if (testType.FullName == typeName) { if (extensionLoader.CustomExtensionUnavailable(testType)) { return(Load(inputStream)); } break; } } } throw; } } finally { if (namespaceStrippedStream != null) { namespaceStrippedStream.Dispose(); } } return(store); }