public MultiSelectionWithCaptionDescriptorBuilder(
     Func <TSourceObject, TItemSource, bool> isActiveFilter,
     IVMDescriptor itemDescriptor
     )
     : base(itemDescriptor, isActiveFilter)
 {
 }
        public void HandleChange(IBehaviorContext context, CollectionChangedArgs <TItemVM> args)
        {
            // The parent is set first and cleared last to allow proper propagation of
            // validation and change notification.

            IVMDescriptor itemDescriptor = this.GetItemDescriptor();

            foreach (IViewModel item in args.NewItems)
            {
                // TODO: Should we check old descriptor, should the Descriptor property
                //       handle this, or is it OK as it is now?
                // TODO: Test this feature.
                // The 'Descriptor' must be set before accessing the 'Kernel'!
                item.Descriptor = itemDescriptor;

                item.Kernel.Parents.Add(args.Collection.OwnerVM);
                item.Kernel.OwnerCollections.Add(args.Collection);
            }

            this.HandleChangeNext(context, args);

            foreach (IViewModel item in args.OldItems)
            {
                item.Kernel.Parents.Remove(args.Collection.OwnerVM);
                item.Kernel.OwnerCollections.Remove(args.Collection);
            }
        }
        public IVMPropertyDescriptor <IVMCollection <TChildVM> > CollectionProperty <TChildVM>(
            IVMDescriptor itemDescriptor,
            IBehavior valueAccessor,
            IBehavior sourceAccessor = null,
            Action <BehaviorChainConfiguration> chainConfigurationAction = null
            ) where TChildVM : IViewModel
        {
            return(CustomProperty <IVMCollection <TChildVM> >(
                       DefaultBehaviorChainTemplateKeys.CollectionProperty,
                       BehaviorFactoryConfigurations.ForChildProperty <TOwnerVM, TChildVM, TSourceObject>(),
                       config => {
                config.Enable(PropertyBehaviorKeys.ValueAccessor, valueAccessor);

                config.Enable(
                    CollectionPropertyBehaviorKeys.ItemDescriptorProvider,
                    new ItemDescriptorProviderBehavior(itemDescriptor)
                    );

                if (sourceAccessor != null)
                {
                    config.Enable(PropertyBehaviorKeys.SourceAccessor, sourceAccessor);
                }
            },
                       chainConfigurationAction
                       ));
        }
 public ViewModelStub(IVMDescriptor descriptor)
 {
     _fakeValues = new Dictionary <IVMPropertyDescriptor, object>();
     Descriptor  = descriptor;
     Kernel      = new VMKernel(this, Descriptor, ServiceLocator.Current);
     _context    = Kernel;
 }
Beispiel #5
0
 internal static PropertyDescriptorCollection GetPropertyDescriptors(this IVMDescriptor descriptor)
 {
     return(descriptor
            .Behaviors
            .GetNextBehavior <TypeDescriptorProviderBehavior>()
            .PropertyDescriptors);
 }
        public static PropertyDescriptorCollection GetPropertiesOf(Type type)
        {
            Type itemType = TypeService.GetItemType(collectionType: type);

            if (itemType != null)
            {
                type = itemType;
            }

            IVMDescriptor childDescriptor = ClassDescriptorAttribute
                                            .GetClassDescriptorOf(type);

            if (childDescriptor != null)
            {
                var browsableDescriptors = childDescriptor
                                           .GetPropertyDescriptors()
                                           .OfType <ViewModelPropertyDescriptor>()
                                           .Select(actual => new BrowsablePropertyDescriptor(actual))
                                           .ToArray();

                return(new PropertyDescriptorCollection(browsableDescriptors));
            }

            return(TypeDescriptor.GetProperties(type));
        }
 public IVMPropertyDescriptor <IVMCollection <TItemVM> > With(IVMDescriptor itemDescriptor)
 {
     return(_factory.CollectionProperty <TItemVM>(
                itemDescriptor,
                valueAccessor: new PopulatedCollectionAccessorBehavior <TItemVM>(),
                sourceAccessor: _sourceCollectionAccessor
                ));
 }
 public ValidatorBuilderOperationCollection(
     IVMDescriptor descriptor,
     VMDescriptorConfiguration config
     )
 {
     _descriptor = descriptor;
     _config     = config;
 }
Beispiel #9
0
        public SingleSelectionDescriptorBuilderBase(
            IVMDescriptor itemDescriptor,
            Func <TSourceObject, TItemSource, bool> isActiveFilter
            )
        {
            WithProperties((d, b) => {
                var v = b.GetPropertyBuilder();

                d.SelectedItem = v.VM.DelegatesTo(
                    (vm) => vm.GetSelectedItem(),
                    (vm, val) => vm.SetSelectedItem(val)
                    );

                d.AllItems = v
                             .Collection
                             .PopulatedWith(vm => {
                    IEnumerable <TItemSource> selectableItems = SelectionHelpers
                                                                .GetSelectableSourceItems <TSourceObject, TItemSource>(vm);

                    return(selectableItems.Select(x => vm.GetItemVM(x)));
                })
                             .With(itemDescriptor);
            });

            WithViewModelBehaviors(b => {
                b.OverrideUpdateFromSourceProperties(
                    x => x.AllSourceItems,
                    x => x.SelectedSourceItem,
                    x => x.AllItems,
                    x => x.SelectedItem
                    );
                b.OverrideUpdateSourceProperties(
                    x => x.SelectedSourceItem
                    );

                b.AppendBehavior(
                    new SelectionItemViewModelCacheBehavior <TItemSource, TItemVM>(itemDescriptor)
                    );

                b.PrependBehavior(new ItemProviderBehavior <TSourceObject, TItemSource>()
                {
                    IsActiveFilter = isActiveFilter
                });
            });

            WithBehaviors(b => {
                b.Property(x => x.AllSourceItems).IsCached();

                b.Property(x => x.SelectedItem).PrependBehavior(new SelectedItemRefreshBehavior <TItemVM>());

                // TODO: Make this configurable.
                b.Property(x => x.SelectedSourceItem).RequiresLoadedProperty(x => x.AllSourceItems);
            });

            WithValidators(b => {
                b.EnableParentValidation(x => x.SelectedItem);
            });
        }
 /// <inheritdoc />
 IVMPropertyDescriptor <IVMCollection <TItemVM> > ICollectionPropertyBuilder <TSourceObject> .Of <TItemVM>(
     IVMDescriptor itemDescriptor
     )
 {
     return(Custom.CollectionProperty <TItemVM>(
                itemDescriptor,
                new StoredCollectionAccessorBehavior <TItemVM>()
                ));
 }
        /// <summary>
        ///   Creates concrete <see cref="BehaviorChain"/>s from the <see
        ///   cref="BehaviorChainConfiguration"/>s and assigns them to the <paramref
        ///   name="descriptor"/> and its VM properties.
        /// </summary>
        internal void ApplyTo(IVMDescriptor descriptor)
        {
            var chain = ViewModelConfiguration.CreateChain();

            chain.Initialize(descriptor);
            descriptor.Behaviors = chain;

            PropertyConfigurations.ApplyToProperties(descriptor);
        }
        private static void RequireClassDescriptorAttributeOnItemType()
        {
            // TODO: Clean up?
            IVMDescriptor classDescriptor = ClassDescriptorAttribute.GetClassDescriptorOf(typeof(T));

            if (classDescriptor == null)
            {
                throw new ArgumentException(EViewModels.BrowsableListRequiresClassDescriptorAttribute);
            }
        }
Beispiel #13
0
        public VMKernel(IViewModel vm, IVMDescriptor descriptor, IServiceLocator serviceLocator)
        {
            Check.NotNull(vm, nameof(vm));
            Check.NotNull(descriptor, nameof(descriptor));
            Check.NotNull(serviceLocator, nameof(serviceLocator));

            _vm            = vm;
            _descriptor    = descriptor;
            ServiceLocator = serviceLocator;
        }
 public ValidatorBuilderOperation(
     IVMDescriptor descriptor,
     VMDescriptorConfiguration config
     )
 {
     Descriptor = descriptor;
     Config     = config;
     Path       = PathDefinition.Empty;
     ActionArgs = new Stack <IValidator>();
 }
Beispiel #15
0
        public BehaviorInitializationContext(
            IVMDescriptor descriptor,
            IVMPropertyDescriptor property = null
            )
        {
            Check.NotNull(descriptor, nameof(descriptor));

            Fields     = descriptor.Fields;
            Descriptor = descriptor;
            Property   = property;
        }
Beispiel #16
0
        /// <summary>
        ///   Creates concrete <see cref="BehaviorChain"/> objects for each registered
        ///   <see cref="BehaviorChainConfiguration"/> and assigns it to the <see
        ///   cref="IVMPropertyDescriptor.Behaviors"/> property of the <see cref="IVMPropertyDescriptor"/>
        ///   object for which the <see cref="BehaviorChainConfiguration"/> was
        ///   registered.
        /// </summary>
        internal void ApplyToProperties(IVMDescriptor parentDescriptor)
        {
            foreach (var pair in _propertyConfigurations)
            {
                IVMPropertyDescriptor      property = pair.Key;
                BehaviorChainConfiguration config   = pair.Value;

                var chain = config.CreateChain();
                chain.Initialize(parentDescriptor, property);
                property.Behaviors = chain;
            }
        }
Beispiel #17
0
        /// <summary>
        ///   Initializes the behavior chain for the given VM descriptor and
        ///   optionally VM property.
        /// </summary>
        public static void Initialize(
            this BehaviorChain chain,
            IVMDescriptor descriptor,
            IVMPropertyDescriptor property = null
            )
        {
            var context = new BehaviorInitializationContext(descriptor, property);

            chain.TryCall <IBehaviorInitializationBehavior>(x =>
                                                            x.Initialize(context)
                                                            );
        }
            IVMPropertyDescriptor <IVMCollection <TItemVM> > ICollectionPropertyBuilderWithSource <TItemSource> .With <TItemVM>(
                IVMDescriptor itemDescriptor
                )
            {
                IValueAccessorBehavior <IVMCollection <TItemVM> > valueAccessor =
                    new WrapperCollectionAccessorBehavior <TItemVM, TItemSource>(_shouldCacheSourceCollection);

                return(_factory.CollectionPropertyWithSource <TItemVM, TItemSource>(
                           itemDescriptor,
                           valueAccessor,
                           _sourceCollectionAccessor
                           ));
            }
Beispiel #19
0
        private void AssertStandardValidators(
            IVMDescriptor descriptor,
            ValidationStep step,
            PathDefinition targetPath,
            IValidator validator,
            Func <ValidatorConditionArgs <EmployeeVM, IViewModel <ProjectVMDescriptor> >, bool> condition1 = null,
            Func <ValidatorConditionArgs <EmployeeVM, IViewModel <ProjectVMDescriptor> >, bool> condition2 = null
            )
        {
            IValidator expectedValidator = validator;

            if (condition2 != null)
            {
                expectedValidator = new ConditionalValidator(
                    new DelegateValidatorCondition <EmployeeVM, IViewModel <ProjectVMDescriptor> >(condition2, 0),
                    expectedValidator
                    );
            }

            if (condition1 != null)
            {
                expectedValidator = new ConditionalValidator(
                    new DelegateValidatorCondition <EmployeeVM, IViewModel <ProjectVMDescriptor> >(condition1, 0),
                    expectedValidator
                    );
            }

            expectedValidator = new ConditionalValidator(
                new ValidationStepCondition(step),
                new ConditionalValidator(
                    new ValidationTargetCondition(targetPath),
                    expectedValidator
                    )
                );

            ValidatorExecutorBehavior expectedBehavior = new ValidatorExecutorBehavior();

            expectedBehavior.AddValidator(expectedValidator);

            Assert.AreEqual(
                expectedBehavior.ToString(),
                descriptor
                .Behaviors
                .GetNextBehavior <ValidatorExecutorBehavior>()
                .ToString()
                );
        }
Beispiel #20
0
        private void AssertDependencySetup <TAction>(
            IVMDescriptor descriptor,
            PathDefinition sourcePath,
            ChangeType[] changeTypes,
            PathDefinition targetPath = null
            ) where TAction : DependencyAction
        {
            var behavior = descriptor.Behaviors.GetNextBehavior <DeclarativeDependencyBehavior>();

            Assert.AreEqual(
                sourcePath.ToString(),
                behavior.Dependencies.First().SourcePath.ToString()
                );

            Assert.IsInstanceOfType(
                behavior.Dependencies.First().Action,
                typeof(TAction)
                );

            CollectionAssert.AreEquivalent(
                changeTypes,
                behavior.Dependencies.First().ChangeTypes
                );

            if (targetPath != null)
            {
                DependencyAction action = behavior.Dependencies.First().Action;
                if (action is ValidationAction)
                {
                    Assert.AreEqual(
                        targetPath.ToString(),
                        ((ValidationAction)action).Target.Path.ToString()
                        );
                }
                else if (action is RefreshAction)
                {
                    Assert.AreEqual(
                        targetPath.ToString(),
                        ((RefreshAction)action).Target.Path.ToString()
                        );
                }
                else
                {
                    Assert.Fail("When asserting a target path the action have to be a RefreshAction or ValidationAction");
                }
            }
        }
Beispiel #21
0
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            if (instance != null)
            {
                // Interface is implemented by ViewModelWithTypeDescriptor
                return((ICustomTypeDescriptor)instance);
            }

            if (objectType != null)
            {
                IVMDescriptor classDescriptor = ClassDescriptorAttribute.GetClassDescriptorOf(objectType);
                if (classDescriptor != null)
                {
                    return(new ViewModelTypeDescriptor(classDescriptor));
                }
            }

            return(base.GetTypeDescriptor(objectType, instance)); // return an empty TypeDescriptor
        }
Beispiel #22
0
 public IVMPropertyDescriptor GetProperty(IVMDescriptor descriptor)
 {
     return(GetProperty((TDescriptor)descriptor));
 }
        public MultiSelectionDescriptorBuilderBase(
            IVMDescriptor itemDescriptor,
            Func <TSourceObject, TItemSource, bool> isActiveFilter
            )
        {
            WithProperties((d, b) => {
                var v = b.GetPropertyBuilder();

                d.SelectedItems = v
                                  .Collection
                                  .Wraps(x => x.SelectedSourceItems)
                                  .With <TItemVM>(itemDescriptor);

                d.AllItems = v
                             .Collection
                             .PopulatedWith(vm => {
                    IEnumerable <TItemSource> selectableItems = SelectionHelpers
                                                                .GetSelectableSourceItems <TSourceObject, TItemSource>(vm);

                    return(selectableItems.Select(x => vm.GetItemVM(x)));
                })
                             .With(itemDescriptor);
            });

            WithViewModelBehaviors(b => {
                b.OverrideUpdateFromSourceProperties(
                    x => x.AllSourceItems,
                    x => x.SelectedSourceItems,
                    x => x.AllItems,
                    x => x.SelectedItems
                    );
                b.OverrideUpdateSourceProperties(
                    x => x.SelectedSourceItems
                    );

                b.AppendBehavior(
                    new SelectionItemViewModelCacheBehavior <TItemSource, TItemVM>(itemDescriptor)
                    );

                b.AppendBehavior(new ItemProviderBehavior <TSourceObject, TItemSource>()
                {
                    IsActiveFilter = isActiveFilter
                });
            });

            WithBehaviors(b => {
                b.Property(x => x.SelectedItems).Enable(
                    PropertyBehaviorKeys.ValueAccessor,
                    new SelectedItemsAccessorBehavior <TVM, TItemVM, TItemSource>()
                    );

                // This behavior allows a bound comobox to assign a new list to the 'SelectedItems'
                // property every time the selection changes.
                b.Property(x => x.SelectedItems).Enable(
                    PropertyBehaviorKeys.DisplayValueAccessor,
                    new SettableListDisplayValueBehavior <TItemVM>()
                    );

                b.Property(x => x.SelectedItems).AddChangeHandler((vm, args) => {
                    vm.RaisePropertyChangedForSelectedItems(); // HACK!
                });

                b.Property(x => x.AllSourceItems).IsCached();
                b.Property(x => x.SelectedItems).SupportsDisplayValueConversion();

                b.Property(x => x.SelectedItems).PrependBehavior(new SelectedItemsRefreshBehavior <TItemVM>());

                // TODO: Make this configurable.
                b.Property(x => x.SelectedSourceItems).RequiresLoadedProperty(x => x.AllSourceItems);
            });

            WithValidators(b => {
                b.EnableParentValidation(x => x.SelectedItems);
                b.EnableParentViewModelValidation();
            });
        }
 public void Initialize(BehaviorInitializationContext context)
 {
     _vmDescriptor = context.Descriptor;
     this.InitializeNext(context);
 }
 public SelectionItemViewModelCacheBehavior(
     IVMDescriptor itemDescriptor
     )
 {
     _itemDescriptor = itemDescriptor;
 }
Beispiel #26
0
 public ItemDescriptorProviderBehavior(IVMDescriptor itemDescriptor)
 {
     Check.NotNull(itemDescriptor, nameof(itemDescriptor));
     ItemDescriptor = itemDescriptor;
 }