Exemplo n.º 1
0
        /// <inheritdoc/>
        public override bool CanAttach(INodePresenter nodePresenter)
        {
            // We are in a dictionary...
            var dictionaryDescriptor = nodePresenter.Descriptor as DictionaryDescriptor;

            if (dictionaryDescriptor == null)
            {
                return(false);
            }

            // ... that is not read-only...
            var memberCollection = (nodePresenter as MemberNodePresenter)?.MemberAttributes.OfType <MemberCollectionAttribute>().FirstOrDefault()
                                   ?? nodePresenter.Descriptor.Attributes.OfType <MemberCollectionAttribute>().FirstOrDefault();

            if (memberCollection?.ReadOnly == true)
            {
                return(false);
            }

            // ... can construct key type...
            if (!AddNewItemCommand.CanConstruct(dictionaryDescriptor.KeyType))
            {
                return(false);
            }

            // ... and can construct value type
            var elementType = dictionaryDescriptor.ValueType;

            return(AddNewItemCommand.CanAdd(elementType));
        }
        /// <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);
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override bool CanAttach(INodePresenter nodePresenter)
        {
            // We are in a collection or dictionary...
            var collectionNode = (nodePresenter as ItemNodePresenter)?.OwnerCollection;
            var collectionDescriptor = collectionNode?.Descriptor as CollectionDescriptor;
            var dictionaryDescriptor = collectionNode?.Descriptor as DictionaryDescriptor;
            if (collectionDescriptor == null && dictionaryDescriptor == null)
                return false;

            // ... that is not read-only...
            var memberCollection = (collectionNode as MemberNodePresenter)?.MemberAttributes.OfType<MemberCollectionAttribute>().FirstOrDefault()
                                   ?? collectionNode.Descriptor.Attributes.OfType<MemberCollectionAttribute>().FirstOrDefault();
            if (memberCollection?.ReadOnly == true)
                return false;

            // ... and supports remove...
            if (collectionDescriptor != null)
            {
                var elementType = collectionDescriptor.ElementType;
                // We also add the same conditions that for AddNewItem
                return collectionDescriptor.HasRemoveAt && AddNewItemCommand.CanAdd(elementType);
            }
            // TODO: add a HasRemove in the dictionary descriptor and test it!
            return true;
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public override bool CanAttach(INodePresenter nodePresenter)
        {
            // We are in a dictionary
            if (!(nodePresenter.Descriptor is DictionaryDescriptor dictionaryDescriptor))
            {
                return(false);
            }

            // It is not read-only
            var memberCollection = (nodePresenter as MemberNodePresenter)?.MemberAttributes
                                   .OfType <MemberCollectionAttribute>()
                                   .FirstOrDefault() ??
                                   nodePresenter.Descriptor.Attributes
                                   .OfType <MemberCollectionAttribute>()
                                   .FirstOrDefault();

            if (memberCollection?.ReadOnly == true)
            {
                return(false);
            }

            // It can construct the key type
            if (!AddNewItemCommand.CanConstruct(dictionaryDescriptor.KeyType))
            {
                return(false);
            }

            // And it can construct the value type
            var elementType = dictionaryDescriptor.ValueType;

            return(AddNewItemCommand.CanAdd(elementType));
        }
Exemplo n.º 5
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);
                }
            }
        }