Esempio n. 1
0
        /// <inheritdoc />
        public IParameterVisualiser InstantiateVisualiser(Type type)
        {
            Type visualiserType           = VisualiserType(type);
            IParameterVisualiserInfo info = AttributeMapping[type];

            if (info.IsGeneric)
            {
                return((IParameterVisualiser)Activator.CreateInstance(visualiserType.MakeGenericType(type)));
            }

            return((IParameterVisualiser)Activator.CreateInstance(visualiserType));
        }
Esempio n. 2
0
        /// <summary>
        /// Add a new <see cref="IParameterVisualiser"/> to the mapping. This requires the class itself and some additional information.
        /// </summary>
        /// <param name="visualiserClass">The class that represents the type (e.g. <see cref="SigmaCheckBox"/>).
        /// For the default Sigma parameter handling, <see ref="visualiserClass"/> has to implement <see cref="IParameterVisualiser"/> and
        /// requires a public parameterless constructor.</param>
        /// <param name="parameterInfo">The info for the visualiser (type it represents ... ).</param>
        /// <returns><c>True</c> if the type could be added successfully. <c>False</c> otherwise (e.g. an <see cref="IParameterVisualiser"/> with a higher
        /// priority has already been added).</returns>
        public virtual bool Add(Type visualiserClass, IParameterVisualiserInfo parameterInfo)
        {
            if (!typeof(IParameterVisualiser).IsAssignableFrom(visualiserClass))
            {
                _log.Warn($"{visualiserClass.Name} does not implement the interface {nameof(IParameterVisualiser)} - be aware that this can cause weird errors when using Sigmas default parameter display.");
            }

            if (visualiserClass.GetConstructor(Type.EmptyTypes) == null)
            {
                _log.Warn($"{visualiserClass.Name} does not have a public parameterless constructor - be aware that this can cause weird errors when using Sigmas default parameter display.");
            }

            if (!typeof(UIElement).IsAssignableFrom(visualiserClass))
            {
                _log.Warn($"{visualiserClass.Name} does not derive from {nameof(UIElement)} - be aware that this can cause weird errors when using Sigmas default parameter display.");
            }

            Type storedClass;
            IParameterVisualiserInfo storedAttribte;

            // if the mapping has already been added
            if (TypeMapping.TryGetValue(parameterInfo.Type, out storedClass) && AttributeMapping.TryGetValue(parameterInfo.Type, out storedAttribte))
            {
                // if the a differnt type is being represented (necessarry for generics)
                if (!ReferenceEquals(visualiserClass, storedClass))
                {
                    // if the new values have a lower priority, we return false
                    if (parameterInfo.Priority <= storedAttribte.Priority)
                    {
                        _log.Warn($"{parameterInfo.Type} is currently visualised by {storedClass.Name}; {visualiserClass.Name} tried to be the visualiser but has a lower priority ({parameterInfo.Priority} <= {storedAttribte.Priority}).");

                        return(false);
                    }

                    _log.Debug($"{parameterInfo.Type} was visualised by {storedClass.Name}; {visualiserClass.Name} has a higher priority and is therefore the new visualiser ({parameterInfo.Priority} > {storedAttribte.Priority}).");
                }
            }

            TypeMapping[parameterInfo.Type]      = visualiserClass;
            AttributeMapping[parameterInfo.Type] = parameterInfo;

            return(true);
        }