/// <inheritdoc/>
        protected override void ExecuteSync(INodePresenter nodePresenter, object parameter, object preExecuteResult)
        {
            var assetNodePresenter   = nodePresenter as IAssetNodePresenter;
            var dictionaryDescriptor = (DictionaryDescriptor)nodePresenter.Descriptor;
            var value = nodePresenter.Value;

            NodeIndex newKey;

            if (dictionaryDescriptor.KeyType == typeof(string))
            {
                newKey = GenerateStringKey(value, dictionaryDescriptor, parameter as string);
            }
            else if (dictionaryDescriptor.KeyType.IsEnum)
            {
                newKey = new NodeIndex(parameter);
            }
            else
            {
                newKey = new NodeIndex(Activator.CreateInstance(dictionaryDescriptor.KeyType));
            }

            var newItem  = dictionaryDescriptor.ValueType.Default();
            var instance = CreateInstance(dictionaryDescriptor.ValueType);

            if (!AddNewItemCommand.IsReferenceType(dictionaryDescriptor.ValueType) && (assetNodePresenter == null || !assetNodePresenter.IsObjectReference(instance)))
            {
                newItem = instance;
            }

            nodePresenter.AddItem(newItem, newKey);
        }
Esempio n. 2
0
        /// <inheritdoc/>
        protected override void ExecuteSync(INodePresenter nodePresenter, object parameter, object preExecuteResult)
        {
            var assetNodePresenter   = nodePresenter as IAssetNodePresenter;
            var dictionaryDescriptor = (DictionaryDescriptor)nodePresenter.Descriptor;
            var value = nodePresenter.Value;

            NodeIndex?newKey;

            if (parameter != null && TypeDescriptor.GetConverter(dictionaryDescriptor.KeyType).CanConvertFrom(parameter.GetType()))
            {
                newKey = GenerateGenericKey(value, dictionaryDescriptor, parameter);
            }
            else if (dictionaryDescriptor.KeyType.IsEnum)
            {
                newKey = new NodeIndex(parameter);
            }
            else
            {
                newKey = new NodeIndex(Activator.CreateInstance(dictionaryDescriptor.KeyType));
            }

            if (newKey != null)
            {
                if (!dictionaryDescriptor.ContainsKey(nodePresenter.Value, newKey.Value.Value))
                {
                    var newItem  = dictionaryDescriptor.ValueType.Default();
                    var instance = CreateInstance(dictionaryDescriptor.ValueType);
                    if (!AddNewItemCommand.IsReferenceType(dictionaryDescriptor.ValueType) && (assetNodePresenter == null || !assetNodePresenter.IsObjectReference(instance)))
                    {
                        newItem = instance;
                    }

                    nodePresenter.AddItem(newItem, newKey.Value);
                }
            }
        }