private void LoadDiagram <TModel, TDiagram>(string fileName, string diagramExtension, System.Guid diagramDomainId, CreateDiagramHandler callback) where TModel : Microsoft.VisualStudio.Modeling.ModelElement where TDiagram : ComponentModelDiagram { ReadOnlyCollection <TModel> modelsLayerList = this.Store.ElementDirectory.FindElements <TModel>(); if (modelsLayerList.Count == 0) { return; } DslModeling::SerializationResult serializationResult = new DslModeling::SerializationResult(); DslModeling::ISchemaResolver schemaResolver = new DslShell::ModelingSchemaResolver(this.ServiceProvider); // Enable diagram fixup rules in our store, because we will load diagram data. global::DSLFactory.Candle.SystemModel.CandleDomainModel.EnableDiagramRules(this.Store); string diagramFileName = fileName + diagramExtension; // Ensure that subordinate diagram file is named properly; handles the case where a rename occurs while the designer was closed. if (this.Hierarchy != null) { uint itemId = DslShell::SubordinateFileHelper.GetChildProjectItemId(this.Hierarchy, this.ItemId, DataLayerDiagramExtension); if (itemId != global::Microsoft.VisualStudio.VSConstants.VSITEMID_NIL) { DslShell::SubordinateFileHelper.EnsureChildFileName(this.ServiceProvider, this.Hierarchy, itemId, diagramFileName); // Found the diagram file, lock it in the running documents table. //this.diagramDocument2LockHolder = DslShell::SubordinateFileHelper.LockSubordinateDocument( this.ServiceProvider, this, diagramFileName, itemId ); //if( this.diagramDocument2LockHolder == null ) //{ // throw new global::System.InvalidOperationException( global::DSLFactory.Candle.SystemModel.SystemModelDomainModel.SingletonResourceManager.GetString( "CannotOpenDocument" ) ); //} } } try { global::DSLFactory.Candle.SystemModel.CandleSerializationHelper.Instance.LoadDiagram <TModel, TDiagram>(serializationResult, modelsLayerList[0], diagramFileName, schemaResolver, diagramDomainId, callback); } catch { ServiceLocator.Instance.IDEHelper.ShowMessage(String.Concat("Error when opening diagram file ", diagramFileName)); } finally { //if( this.diagramDocument2LockHolder != null && (serializationResult.Failed ) ) //{ // // Load failed, release the document lock if we added one above. // this.diagramDocument2LockHolder.UnregisterSubordinateDocument(); // this.diagramDocument2LockHolder = null; //} } // Report serialization messages. this.SuspendErrorListRefresh(); try { // Report serialization messages. foreach (DslModeling::SerializationMessage serializationMessage in serializationResult) { this.AddErrorListItem(new DslShell::SerializationErrorListItem(this.ServiceProvider, serializationMessage)); } } finally { this.ResumeErrorListRefresh(); } if (serializationResult.Failed) { // Load failed, can't open the file. throw new global::System.InvalidOperationException(global::DSLFactory.Candle.SystemModel.CandleDomainModel.SingletonResourceManager.GetString("CannotOpenDocument")); } else { if (this.Hierarchy != null && global::System.IO.File.Exists(diagramFileName)) { DslShell::SubordinateFileHelper.EnsureChildProjectItem(this.Hierarchy, this.ItemId, diagramFileName); } } }
/// <summary> /// Loads the diagram. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TDiagram">The type of the diagram.</typeparam> /// <param name="serializationResult">The serialization result.</param> /// <param name="modelRoot">The model root.</param> /// <param name="diagramFileName">Name of the diagram file.</param> /// <param name="schemaResolver">The schema resolver.</param> /// <param name="diagramDomainId">The diagram domain id.</param> /// <param name="callback">The callback.</param> public void LoadDiagram <TModel, TDiagram>(DslModeling::SerializationResult serializationResult, TModel modelRoot, string diagramFileName, DslModeling::ISchemaResolver schemaResolver, System.Guid diagramDomainId, CreateDiagramHandler callback) where TModel : ModelElement where TDiagram : ComponentModelDiagram { Partition diagramPartition = modelRoot.Store.DefaultPartition; // Ensure there is an outer transaction spanning both model and diagram load, so moniker resolution works properly. if (!diagramPartition.Store.TransactionActive) { throw new global::System.InvalidOperationException(global::DSLFactory.Candle.SystemModel.CandleDomainModel.SingletonResourceManager.GetString("MissingTransaction")); } ComponentModelDiagram diagram = null; DslModeling::DomainClassXmlSerializer diagramSerializer = this.Directory.GetSerializer(diagramDomainId); global::System.Diagnostics.Debug.Assert(diagramSerializer != null, "Cannot find serializer for SystemModelDiagram"); if (diagramSerializer != null) { if (!global::System.IO.File.Exists(diagramFileName)) { // missing diagram file indicates we should create a new diagram. diagram = callback(diagramPartition); } else { using (global::System.IO.FileStream fileStream = global::System.IO.File.OpenRead(diagramFileName)) { DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(this.Directory, fileStream.Name, serializationResult); using (DslModeling::Transaction t = diagramPartition.Store.TransactionManager.BeginTransaction("LoadDiagram", true)) { // Ensure there is some content in the file. Blank (or almost blank, to account for encoding header bytes, etc.) // files will cause a new diagram to be created and returned if (fileStream.Length > 5) { global::System.Xml.XmlReaderSettings settings = new global::System.Xml.XmlReaderSettings(); try { using (global::System.Xml.XmlReader reader = global::System.Xml.XmlReader.Create(fileStream, settings)) { reader.MoveToContent(); diagram = diagramSerializer.TryCreateInstance(serializationContext, reader, diagramPartition) as ComponentModelDiagram; if (diagram != null) { // Note: the actual instance we get back from TryCreateInstance() can be of a derived type of SystemModelDiagram, // so we need to find the correct serializer instance to deserialize the element properly. DslModeling::DomainClassXmlSerializer instanceSerializer = this.Directory.GetSerializer(diagram.GetDomainClass().Id); global::System.Diagnostics.Debug.Assert(instanceSerializer != null, "Cannot find serializer for " + diagram.GetDomainClass().Name + "!"); instanceSerializer.ReadRootElement(serializationContext, diagram, reader, schemaResolver); } } } catch (global::System.Xml.XmlException xEx) { DslModeling::SerializationUtilities.AddMessage( serializationContext, DslModeling::SerializationMessageKind.Error, xEx ); } if (serializationResult.Failed) { // Serialization error encountered, rollback the transaction. diagram = null; t.Rollback(); } } if (diagram == null && !serializationResult.Failed) { // Create diagram if it doesn't exist diagram = CreateDiagramHelper(diagramPartition, modelRoot); } if (t.IsActive) { t.Commit(); } } // End inner Tx } } if (diagram != null) { diagram.ModelElement = modelRoot; } } }