Example #1
0
        public static ObservableViewModel CombineViewModels(IViewModelServiceProvider serviceProvider, ModelContainer modelContainer, IEnumerable <ObservableViewModel> viewModels)
        {
            var combinedViewModel = new ObservableViewModel(serviceProvider, modelContainer);

            var rootNodes = new List <ObservableModelNode>();

            foreach (var viewModel in viewModels)
            {
                if (!(viewModel.RootNode is SingleObservableNode))
                {
                    throw new ArgumentException(@"The view models to combine must contains SingleObservableNode.", "viewModels");
                }

                foreach (var dirtiableViewModel in viewModel.dirtiableViewModels)
                {
                    combinedViewModel.dirtiableViewModels.Add(dirtiableViewModel.Key, dirtiableViewModel.Value.ToList());
                }

                var rootNode = (ObservableModelNode)viewModel.RootNode;
                rootNodes.Add(rootNode);
            }

            if (rootNodes.Count < 2)
            {
                throw new ArgumentException(@"Called CombineViewModels with a collection of view models that is either empty or containt just a single item.", "viewModels");
            }

            CombinedObservableNode rootCombinedNode = CombinedObservableNode.Create(combinedViewModel, "Root", null, typeof(object), rootNodes, null);

            combinedViewModel.Identifier = new ObservableViewModelIdentifier(rootNodes.Select(x => x.ModelGuid));
            rootCombinedNode.Initialize();
            combinedViewModel.RootNode = rootCombinedNode;
            return(combinedViewModel);
        }
Example #2
0
 private void GenerateChildren(IEnumerable <KeyValuePair <string, List <SingleObservableNode> > > commonChildren)
 {
     foreach (var children in commonChildren)
     {
         var contentType = children.Value.First().Type;
         var index       = children.Value.First().Index;
         CombinedObservableNode child = Create(Owner, children.Key, this, contentType, children.Value, index);
         AddChild(child);
         child.Initialize();
     }
 }
Example #3
0
        public static ObservableViewModel CombineViewModels(IViewModelServiceProvider serviceProvider, IReadOnlyCollection <ObservableViewModel> viewModels)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }
            if (viewModels == null)
            {
                throw new ArgumentNullException(nameof(viewModels));
            }
            var combinedViewModel = new ObservableViewModel(serviceProvider);

            var rootNodes = new List <ObservableModelNode>();

            foreach (var viewModel in viewModels)
            {
                if (!(viewModel.RootNode is SingleObservableNode))
                {
                    throw new ArgumentException(@"The view models to combine must contains SingleObservableNode.", nameof(viewModels));
                }

                viewModel.parent = combinedViewModel;
                combinedViewModel.children.Add(viewModel);
                var rootNode = (ObservableModelNode)viewModel.RootNode;
                rootNodes.Add(rootNode);
            }

            if (rootNodes.Count < 2)
            {
                throw new ArgumentException(@"Called CombineViewModels with a collection of view models that is either empty or containt just a single item.", nameof(viewModels));
            }

            // Find best match for the root node type
            var rootNodeType = rootNodes.First().Root.Type;

            if (rootNodes.Skip(1).Any(x => x.Type != rootNodeType))
            {
                rootNodeType = typeof(object);
            }

            CombinedObservableNode rootCombinedNode = CombinedObservableNode.Create(combinedViewModel, "Root", null, rootNodeType, rootNodes, Index.Empty);

            rootCombinedNode.Initialize();
            combinedViewModel.RootNode = rootCombinedNode;
            return(combinedViewModel);
        }
Example #4
0
        private void GenerateListChildren(IEnumerable <KeyValuePair <object, List <SingleObservableNode> > > allChildren)
        {
            int currentIndex = 0;

            foreach (var children in allChildren)
            {
                if (!ShouldCombine(children.Value, CombinedNodes.Count, "(ListItem)", true))
                {
                    continue;
                }

                var contentType = children.Value.First().Type;
                var name        = $"Item {currentIndex}";
                CombinedObservableNode child = Create(Owner, name, this, contentType, children.Value, new Index(currentIndex));
                AddChild(child);
                child.Initialize();
                child.DisplayName = name;
                ++currentIndex;
            }
        }
Example #5
0
        internal static CombinedObservableNode Create(ObservableViewModel ownerViewModel, string name, CombinedObservableNode parent, Type contentType, IEnumerable <SingleObservableNode> combinedNodes, Index index)
        {
            var node = (CombinedObservableNode)Activator.CreateInstance(typeof(CombinedObservableNode <>).MakeGenericType(contentType), ownerViewModel, name, combinedNodes, index);

            return(node);
        }
Example #6
0
 internal static CombinedObservableNode Create(ObservableViewModel ownerViewModel, string name, CombinedObservableNode parent, Type contentType, IEnumerable<SingleObservableNode> combinedNodes, object index)
 {
     var node = (CombinedObservableNode)Activator.CreateInstance(typeof(CombinedObservableNode<>).MakeGenericType(contentType), ownerViewModel, name, combinedNodes, index);
     return node;
 }
        protected CombinedObservableNode(ObservableViewModel ownerViewModel, string name, CombinedObservableNode parentNode, IEnumerable <SingleObservableNode> combinedNodes, object index)
            : base(ownerViewModel, parentNode, index)
        {
            this.combinedNodes = new List <SingleObservableNode>(combinedNodes);
            Name        = name;
            DisplayName = this.combinedNodes.First().DisplayName;

            combinedNodeInitialValues         = new List <object>();
            distinctCombinedNodeInitialValues = new HashSet <object>();

            bool isReadOnly = false;
            bool isVisible  = false;
            bool nullOrder  = false;

            foreach (var node in this.combinedNodes)
            {
                if (node.IsReadOnly)
                {
                    isReadOnly = true;
                }

                if (node.IsVisible)
                {
                    isVisible = true;
                }

                if (node.Order == null)
                {
                    nullOrder = true;
                }

                if (order == node.Order || (!nullOrder && order == null))
                {
                    order = node.Order;
                }

                combinedNodeInitialValues.Add(node.Value);
                distinctCombinedNodeInitialValues.Add(node.Value);
                node.PropertyChanged += NodePropertyChanged;
            }
            IsReadOnly = isReadOnly;
            IsVisible  = isVisible;

            ResetInitialValues = new AnonymousCommand(ServiceProvider, () => { Owner.BeginCombinedAction(); CombinedNodes.Zip(combinedNodeInitialValues).ForEach(x => x.Item1.Value = x.Item2); Refresh(); Owner.EndCombinedAction(Owner.FormatCombinedUpdateMessage(this, null), Path, null); });
        }