Пример #1
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param>
        /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>
        /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed.
        /// </returns>
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            PresentationElement shape = context.Instance as PresentationElement;

            if (shape == null)
            {
                return(value);
            }

            _externalAssembly = shape.ModelElement as DotNetAssembly;
            if (_externalAssembly == null)
            {
                return(value);
            }

            using (
                Transaction transaction =
                    _externalAssembly.Store.TransactionManager.BeginTransaction("Retrieve assembly infos"))
            {
                IAssemblySelectorDialog selector = ServiceLocator.Instance.GetService <IAssemblySelectorDialog>();
                if (selector.ShowDialog(1))
                {
                    _externalAssembly.InitFromAssembly(selector.SelectedAssemblies[0], true);

                    // Vérification si il n'y a pas des relations orphelines
                    // TODO
                    transaction.Commit();
                }
            }

            return(value);
        }
        // Ajout de l'élément
        /// <summary>
        /// Alerts listeners that a rule has been used.
        /// </summary>
        /// <param name="e">An ElementAddedEventArgs that contains the event data.</param>
        public override void ElementAdded(ElementAddedEventArgs e)
        {
            #region Condition

            // Test the element
            DotnetAssemblyShape shape = e.ModelElement as DotnetAssemblyShape;
            if (shape == null)
            {
                return;
            }

            // Teste qu'elle est la source qui a générée l'événement
            if (shape.Store.TransactionManager.CurrentTransaction.TopLevelTransaction.IsSerializing ||
                shape.Store.InUndoRedoOrRollback)
            {
                return;
            }

            #endregion

            #region Traitement

            DotNetAssembly externalAssembly = shape.ModelElement as DotNetAssembly;
            if (externalAssembly != null && !String.IsNullOrEmpty(externalAssembly.FullName))
            {
                return;
            }

            using (
                Transaction transaction =
                    externalAssembly.Store.TransactionManager.BeginTransaction("Retrieve assembly infos"))
            {
                try
                {
                    IAssemblySelectorDialog selector = ServiceLocator.Instance.GetService <IAssemblySelectorDialog>();
                    if (selector.ShowDialog(1))
                    {
                        externalAssembly.InitFromAssembly(selector.SelectedAssemblies[0], true);
                        transaction.Commit();
                    }
                }
                catch (Exception ex)
                {
                    ILogger logger = ServiceLocator.Instance.GetService <ILogger>();
                    if (logger != null)
                    {
                        logger.WriteError("Import assembly", "Reflection error", ex);
                    }
                    transaction.Rollback();
                }
            }

            #endregion
        }
Пример #3
0
        /// <summary>
        /// Importe des assemblies dans le modèle
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="assemblies">Liste des assemblies à importer</param>
        internal static void ImportAssemblies(CandleModel model, List <Assembly> assemblies)
        {
            if (assemblies.Count == 0)
            {
                return;
            }

            // Si il y a plusieurs assemblies, on va demander lequel est le public
            Assembly mainAssembly = assemblies[0];

            if (assemblies.Count > 1)
            {
                if (assemblies.Count > 1)
                {
                    SelectAssemblyForm form = new SelectAssemblyForm(assemblies);
                    if (form.ShowDialog() == DialogResult.Cancel)
                    {
                        throw new CanceledByUser();
                    }
                    mainAssembly = form.SelectedAssembly;
                }
            }

            // D'abord le composant
            BinaryComponent component = model.Component as BinaryComponent;

            if (component == null)
            {
                component = CreateComponent(model, mainAssembly);
            }
            else if (component.Assemblies.Count > 0)
            {
                // Si il y avait dèjà des assemblies, on n'initialisera pas l'assembly public
                mainAssembly = null;
            }

            // Puis insertion des assemblies, en deux passages car il peut y avoir des dépendances entre assemblies
            //  - Insertion des assemblies
            //  - Création des dépendances
            //
            using (Transaction transaction = model.Store.TransactionManager.BeginTransaction("Adding component"))
            {
                // Insertion des assemblies internes
                foreach (Assembly assembly in assemblies)
                {
                    if (component.FindDotNetAssemblyModelFromAssembly(assembly.GetName()) == null)
                    {
                        DotNetAssembly dotnetAssembly = new DotNetAssembly(model.Store);
                        component.Assemblies.Add(dotnetAssembly);
                        dotnetAssembly.InitFromAssembly(assembly, false);

                        // C'est l'assembly public --> Création d'un port public
                        dotnetAssembly.Visibility = assembly == mainAssembly ? Visibility.Public : Visibility.Private;
                    }
                }

                transaction.Commit();
            }

            // Création des dépendances externes
            using (Transaction transaction = model.Store.TransactionManager.BeginTransaction("Adding references"))
            {
                model.Store.TransactionManager.CurrentTransaction.TopLevelTransaction.Context.ContextInfo.Add("Import assemblies", null);
                foreach (Assembly assembly in assemblies)
                {
                    DotNetAssembly externalAssembly = component.FindDotNetAssemblyModelFromAssembly(assembly.GetName());
                    Debug.Assert(externalAssembly != null);
                    externalAssembly.InsertDependencies(assembly);
                }

                transaction.Commit();
            }
        }
Пример #4
0
        /// <summary>
        /// Execute the command
        /// </summary>
        public void Exec()
        {
            // Affiche la fenetre de dialogue permettant de choisir les assemblies
            IAssemblySelectorDialog selector = ServiceLocator.Instance.GetService <IAssemblySelectorDialog>();

            if (selector.ShowDialog(1)) // Une seule sélection possible
            {
                Assembly asm = selector.SelectedAssemblies[0];
                using (Transaction transaction = _element.Store.TransactionManager.BeginTransaction("Update model version"))
                {
                    // Mise à jour de la version du composant et son emplacement initial
                    // et création des nouvelles dépendances (les dépendances actuelles ne seront pas touchées
                    // mais il faudra s'assurer qu'elles soient bien à jour car la procédure ne vérifie que le nom
                    // et ignore la version)
                    _element.InitFromAssembly(asm, true /* creation des nouvelles dépendances*/);
                    transaction.Commit();
                }

                // Vérification si les assemblies existantes possédent la bonne version
                foreach (AssemblyName assemblyName in asm.GetReferencedAssemblies())
                {
                    // On ignore les assemblies systèmes
                    if (Utils.StringCompareEquals(assemblyName.Name, "mscorlib") ||
                        assemblyName.Name.StartsWith("System", StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }

                    // On regarde si cette assembly existe déjà dans le modèle
                    DotNetAssembly eam = _element.Component.FindDotNetAssemblyModelFromAssembly(assemblyName);
                    if (eam != null)
                    {
                        if (!assemblyName.Version.Equals((Version)eam.Version))
                        {
                            using (Transaction transaction = _element.Store.TransactionManager.BeginTransaction("Update assembly version"))
                            {
                                eam.Version = new VersionInfo(assemblyName.Version);
                                transaction.Commit();
                            }
                        }
                    }
                    else
                    {
                        // Idem pour les composants externes
                        ExternalComponent esm = _element.Component.Model.FindExternalComponentByName(assemblyName.Name);
                        if (esm != null && esm.MetaData != null && esm.MetaData.ComponentType == ComponentType.Library)
                        {
                            if (!assemblyName.Version.Equals((Version)esm.Version))
                            {
                                // Recherche si il existe un modèle avec la bonne version
                                List <ComponentModelMetadata> versions = RepositoryManager.Instance.ModelsMetadata.Metadatas.GetAllVersions(esm.Id);
                                ComponentModelMetadata        metadata = versions.Find(delegate(ComponentModelMetadata m) { return(assemblyName.Version.Equals(m.Version)); });
                                using (Transaction transaction = _element.Store.TransactionManager.BeginTransaction("Update model version"))
                                {
                                    if (metadata != null)
                                    {
                                        // OK on met a jour
                                        esm.Version = metadata.Version;
                                    }
                                    else // erreur On a pas trouvé de modéle
                                    {
                                        // Sauvegarde de la bonne version
                                        esm.Version = new VersionInfo(assemblyName.Version);
                                        // On force à null pour obliger l'utilisateur à sélectionner un
                                        // modéle
                                        esm.ModelMoniker = Guid.Empty;
                                    }
                                    transaction.Commit();
                                }
                            }
                        }
                    }
                }

                // Demande si il faut aussi mettre à jour le n° de version du composant
                if (_element.Visibility == Visibility.Public)
                {
                    if (MessageBox.Show(String.Format("Do you want to change the current version of the component from {0} to {1}",
                                                      _element.Component.Model.Version, _element.Version), "Component version change", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        using (Transaction transaction = _element.Store.TransactionManager.BeginTransaction("Update model version"))
                        {
                            _element.Component.Model.Version = _element.Version;
                            transaction.Commit();
                        }
                    }
                }
            }
        }