private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute description, object initialValue)
        {
            // The view models expecting value types shouldn't be given null, avoid that
            if (registration.SupportedType.IsValueType)
            {
                if (initialValue == null)
                {
                    initialValue = Activator.CreateInstance(registration.SupportedType);
                }
            }

            // This assumes the type can be converted, that has been checked when the VM was created
            if (initialValue != null && initialValue.GetType() != registration.SupportedType)
            {
                initialValue = Convert.ChangeType(initialValue, registration.SupportedType);
            }

            IParameter[] parameters = new IParameter[]
            {
                new ConstructorArgument("targetDescription", description),
                new ConstructorArgument("initialValue", initialValue)
            };
            DataModelInputViewModel viewModel = (DataModelInputViewModel)registration.PluginInfo.Kernel.Get(registration.ViewModelType, parameters);

            viewModel.CompatibleConversionTypes = registration.CompatibleConversionTypes;
            return(viewModel);
        }
        public DataModelInputViewModel GetDataModelInputViewModel(Type propertyType, DataModelPropertyAttribute description, object initialValue, Action <object, bool> updateCallback)
        {
            lock (_registeredDataModelEditors)
            {
                // Prefer a VM that natively supports the type
                DataModelVisualizationRegistration match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == propertyType);
                // Fall back on a VM that supports the type through conversion
                if (match == null)
                {
                    match = _registeredDataModelEditors.FirstOrDefault(d => d.CompatibleConversionTypes.Contains(propertyType));
                }
                // Lastly try getting an enum VM if the provided type is an enum
                if (match == null && propertyType.IsEnum)
                {
                    match = _registeredDataModelEditors.FirstOrDefault(d => d.SupportedType == typeof(Enum));
                }

                if (match != null)
                {
                    DataModelInputViewModel viewModel = InstantiateDataModelInputViewModel(match, description, initialValue);
                    viewModel.UpdateCallback = updateCallback;
                    return(viewModel);
                }

                return(null);
            }
        }
        public DataModelDisplayViewModel GetDataModelDisplayViewModel(Type propertyType, DataModelPropertyAttribute description, bool fallBackToDefault)
        {
            lock (_registeredDataModelDisplays)
            {
                DataModelDisplayViewModel result;

                DataModelVisualizationRegistration match = _registeredDataModelDisplays.FirstOrDefault(d => d.SupportedType == propertyType);
                if (match != null)
                {
                    result = (DataModelDisplayViewModel)match.PluginInfo.Kernel.Get(match.ViewModelType);
                }
                else if (!fallBackToDefault)
                {
                    result = null;
                }
                else
                {
                    result = _kernel.Get <DefaultDataModelDisplayViewModel>();
                }

                if (result != null)
                {
                    result.PropertyDescription = description;
                }

                return(result);
            }
        }
        public DataModelVisualizationRegistration RegisterDataModelDisplay <T>(PluginInfo pluginInfo) where T : DataModelDisplayViewModel
        {
            Type viewModelType = typeof(T);

            lock (_registeredDataModelDisplays)
            {
                Type supportedType = viewModelType.BaseType.GetGenericArguments()[0];
                DataModelVisualizationRegistration existing = _registeredDataModelDisplays.FirstOrDefault(r => r.SupportedType == supportedType);
                if (existing != null)
                {
                    if (existing.PluginInfo != pluginInfo)
                    {
                        throw new ArtemisPluginException($"Cannot register data model display for type {supportedType.Name} " +
                                                         $"because an editor was already registered by {pluginInfo.Name}");
                    }

                    return(existing);
                }

                _kernel.Bind(viewModelType).ToSelf();
                DataModelVisualizationRegistration registration = new DataModelVisualizationRegistration(this, RegistrationType.Display, pluginInfo, supportedType, viewModelType);
                _registeredDataModelDisplays.Add(registration);
                return(registration);
            }
        }
Beispiel #5
0
        private DataModelInputViewModel InstantiateDataModelInputViewModel(DataModelVisualizationRegistration registration, DataModelPropertyAttribute?description, object?initialValue)
        {
            // This assumes the type can be converted, that has been checked when the VM was created
            if (initialValue != null && initialValue.GetType() != registration.SupportedType)
            {
                initialValue = Convert.ChangeType(initialValue, registration.SupportedType);
            }

            IParameter[] parameters =
            {
                new ConstructorArgument("targetDescription", description),
                new ConstructorArgument("initialValue",      initialValue)
            };

            // If this ever happens something is likely wrong with the plugin unload detection
            if (registration.Plugin.Kernel == null)
            {
                throw new ArtemisSharedUIException("Cannot InstantiateDataModelInputViewModel for a registration by an uninitialized plugin");
            }

            DataModelInputViewModel viewModel = (DataModelInputViewModel)registration.Plugin.Kernel.Get(registration.ViewModelType, parameters);

            viewModel.CompatibleConversionTypes = registration.CompatibleConversionTypes;
            return(viewModel);
        }
        public void RemoveDataModelDisplay(DataModelVisualizationRegistration registration)
        {
            lock (_registeredDataModelDisplays)
            {
                if (_registeredDataModelDisplays.Contains(registration))
                {
                    registration.Unsubscribe();
                    _registeredDataModelDisplays.Remove(registration);

                    _kernel.Unbind(registration.ViewModelType);
                }
            }
        }
        public DataModelVisualizationRegistration RegisterDataModelInput <T>(PluginInfo pluginInfo, IReadOnlyCollection <Type> compatibleConversionTypes = null) where T : DataModelInputViewModel
        {
            if (compatibleConversionTypes == null)
            {
                compatibleConversionTypes = new List <Type>();
            }
            Type viewModelType = typeof(T);

            lock (_registeredDataModelEditors)
            {
                Type supportedType = viewModelType.BaseType.GetGenericArguments()[0];
                DataModelVisualizationRegistration existing = _registeredDataModelEditors.FirstOrDefault(r => r.SupportedType == supportedType);
                if (existing != null)
                {
                    if (existing.PluginInfo != pluginInfo)
                    {
                        throw new ArtemisPluginException($"Cannot register data model input for type {supportedType.Name} " +
                                                         $"because an editor was already registered by {pluginInfo.Name}");
                    }

                    return(existing);
                }

                _kernel.Bind(viewModelType).ToSelf();

                // Create the registration
                DataModelVisualizationRegistration registration = new DataModelVisualizationRegistration(this, RegistrationType.Input, pluginInfo, supportedType, viewModelType)
                {
                    // Apply the compatible conversion types to the registration
                    CompatibleConversionTypes = compatibleConversionTypes
                };

                _registeredDataModelEditors.Add(registration);
                return(registration);
            }
        }