示例#1
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);
        }
示例#2
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));
        }
示例#3
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 (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);
        }