Пример #1
0
 protected void OnShowRelationShips(object sender, EventArgs e)
 {
     if ((base.CurrentCandleDocView != null) && (base.CurrentCandleDocView.CurrentDiagram != null))
     {
         ComponentModelDiagram currentDiagram = base.CurrentCandleDocView.CurrentDiagram as ComponentModelDiagram;
         if (currentDiagram != null)
         {
             currentDiagram.ShowRelationShips = !currentDiagram.ShowRelationShips;
         }
     }
 }
        /// <summary>
        /// Saves the diagram.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="serializationResult">The serialization result.</param>
        /// <param name="modelRoot">The model root.</param>
        /// <param name="modelFileName">Name of the model file.</param>
        /// <param name="diagram">The diagram.</param>
        /// <param name="diagramFileName">Name of the diagram file.</param>
        /// <param name="encoding">The encoding.</param>
        /// <param name="writeOptionalPropertiesWithDefaultValue">if set to <c>true</c> [write optional properties with default value].</param>
        public void SaveDiagram <TModel>(DslModeling::SerializationResult serializationResult, TModel modelRoot, string modelFileName, ComponentModelDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
            where TModel : ModelElement
        {
            if (serializationResult.Failed)
            {
                return;
            }

            // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.

            using (global::System.IO.MemoryStream diagramFileContent = new global::System.IO.MemoryStream())
            {
                DslModeling::DomainClassXmlSerializer diagramSerializer = this.Directory.GetSerializer(diagram.GetDomainClass().Id);
                global::System.Diagnostics.Debug.Assert(diagramSerializer != null, "Cannot find serializer for " + diagram.GetDomainClass().Name + "!");
                if (diagramSerializer != null)
                {
                    DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(this.Directory, diagramFileName, serializationResult);
                    serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue;
                    global::System.Xml.XmlWriterSettings settings = new global::System.Xml.XmlWriterSettings();
                    settings.Indent   = true;
                    settings.Encoding = encoding;
                    using (global::System.IO.StreamWriter streamWriter = new global::System.IO.StreamWriter(diagramFileContent, encoding))
                    {
                        using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(streamWriter, settings))
                        {
                            diagramSerializer.WriteRootElement(serializationContext, diagram, writer);
                        }
                    }
                }
                if (!serializationResult.Failed)
                {       // Only write the contents if there's no error encountered during serialization.
                    if (diagramFileContent != null)
                    {
                        IVsQueryEditQuerySave2 scc = ServiceLocator.Instance.GetService <IVsQueryEditQuerySave2>(typeof(SVsQueryEditQuerySave));
                        if (scc != null)
                        {
                            uint result;
                            if (scc.QuerySaveFile(diagramFileName, 0, null, out result) != (int)tagVSQuerySaveResult.QSR_SaveOK)
                            {
                                return;
                            }
                        }
                        using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
                        {
                            using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
                            {
                                writer.Write(diagramFileContent.ToArray());
                            }
                        }
                    }
                }
            }
        }
        /// <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;
                }
            }
        }