public void adding_fallback_should_retain_heirchy()
        {
            List <TreeContainerSupport> searchTrees = CreateTrees(6, 4);

            ContainerBinding firstExpectedBinding = registry.AddContainer(searchTrees[0]);

            registry.AddContainer(searchTrees[1].children[3].children[2].children[3]);
            registry.AddContainer(searchTrees[1].children[3].children[2]);
            registry.AddContainer(searchTrees[1]);

            object fallbackContainer = new object();

            registry.SetFallbackContainer(fallbackContainer);

            registry.AddContainer(searchTrees[1].children[3]);

            ContainerBinding topBinding = registry.FindParentBinding(searchTrees [1].children [3].children [2].children [3].children [3]);

            Assert.That(topBinding.Container, Is.EqualTo(searchTrees[1].children[3].children[2].children[3]));
            Assert.That(topBinding.Parent.Container, Is.EqualTo(searchTrees[1].children[3].children[2]));
            Assert.That(topBinding.Parent.Parent.Container, Is.EqualTo(searchTrees[1].children[3]));
            Assert.That(topBinding.Parent.Parent.Parent.Container, Is.EqualTo(searchTrees[1]));
            Assert.That(topBinding.Parent.Parent.Parent.Parent, Is.Not.Null);

            Assert.That(topBinding.Parent.Parent.Parent.Parent.Container, Is.EqualTo(fallbackContainer));
            Assert.That(topBinding.Parent.Parent.Parent.Parent.Parent, Is.Null);
        }
 private void RemoveHandlers(ContainerBinding binding)
 {
     foreach (IViewHandler handler in _handlers)
     {
         binding.RemoveHandler(handler);
     }
 }
 private void AddAllHandlers()
 {
     foreach (object container in _containers)
     {
         ContainerBinding binding = _registry.AddContainer(container);
         AddHandlers(binding);
     }
 }
 private void RemoveRootBinding(ContainerBinding binding)
 {
     _rootBindings.Remove(binding);
     if (_rootContainerRemove != null)
     {
         _rootContainerRemove(binding.Container);
     }
 }
 private void AddRootBinding(ContainerBinding binding)
 {
     _rootBindings.Add(binding);
     if (_rootContainerAdd != null)
     {
         _rootContainerAdd(binding.Container);
     }
 }
        public void returns_root_container_view_bindings_one_item()
        {
            List <TreeContainerSupport> searchTrees          = CreateTrees(1, 1);
            ContainerBinding            expectedBinding      = registry.AddContainer(searchTrees[0]);
            List <ContainerBinding>     expectedRootBindings = new List <ContainerBinding> {
                expectedBinding
            };

            Assert.That(expectedRootBindings, Is.EquivalentTo(registry.RootBindings), "Returns root container view bindings one item");
        }
        public void adding_fallback_should_be_found_as_parent()
        {
            object fallback = new object();

            registry.SetFallbackContainer(fallback);
            object           newView       = new SupportView();
            ContainerBinding parentBinding = registry.FindParentBinding(newView);

            Assert.That(parentBinding.Container, Is.EqualTo(fallback));
        }
        public void SetFallbackContainer(object container)
        {
            _fallbackContainer = container;

            ContainerBinding containerBinding = _registry.SetFallbackContainer(container);

            RemoveAllHandlers(false);
            AddHandlers(containerBinding);

            _registry.FallbackContainerRemove += FallbackContainerBindingRemoved;
        }
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private void RemoveAllHandlers(bool includeFallback)
        {
            if (includeFallback && _fallbackContainer != null)
            {
                _registry.RemoveContainer(_fallbackContainer);
            }

            foreach (object container in _containers)
            {
                ContainerBinding binding = _registry.GetBinding(container);
                RemoveHandlers(binding);
            }
        }
        public void adding_fallback_removes_roots()
        {
            registry.AddContainer(new SupportContainer());
            registry.AddContainer(new SupportContainer());

            Assert.That(registry.RootBindings.Count, Is.EqualTo(2));

            ContainerBinding binding = registry.SetFallbackContainer(new object());

            Assert.That(registry.RootBindings, Is.EqualTo(new List <ContainerBinding> ()
            {
                binding
            }).AsCollection);
        }
        public void finds_correct_nearest_interested_container_view_and_returns_its_binding()
        {
            List <TreeContainerSupport> searchTrees = CreateTrees(5, 4);

            registry.AddContainer(searchTrees[0]);
            registry.AddContainer(searchTrees[1]);

            TreeContainerSupport correctTree = searchTrees[1];

            SupportContainer searchItem = correctTree.children[3].children[3].children[3].children[3];

            ContainerBinding result = registry.FindParentBinding(searchItem);

            Assert.That(correctTree, Is.EqualTo(result.Container), "Finds correct nearest interested container view and returns its binding");
        }
        public void binding_returns_with_correct_interested_parent_chain_if_interested_views_added_in_wrong_order()
        {
            List <TreeContainerSupport> searchTrees = CreateTrees(5, 4);
            ContainerBinding            a           = registry.AddContainer(searchTrees[0]);
            ContainerBinding            b           = registry.AddContainer(searchTrees[1].children[3]);
            ContainerBinding            c           = registry.AddContainer(searchTrees[1]);

            SupportContainer searchItem = searchTrees[1].children[3].children[3].children[3].children[3];

            ContainerBinding result = registry.FindParentBinding(searchItem);

            Assert.That(searchTrees[1].children[3], Is.EqualTo(result.Container), "Binding returns with correct container view");
            Assert.That(searchTrees[1], Is.EqualTo(result.Parent.Container), "Binding returns with correct container parent view");
            Assert.That(result.Parent.Parent, Is.Null, "Further parents are null");
        }
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private ContainerBinding CreateBinding(object container, bool forceRoot = false)
        {
            ContainerBinding binding = new ContainerBinding(container);

            _bindings.Add(binding);

            binding.BINDING_EMPTY += OnBindingEmpty;

            if (!forceRoot)
            {
                binding.Parent = FindParentBinding(container);
            }

            if (binding.Parent == null)
            {
                AddRootBinding(binding);
            }

            // Reparent any bindings which are contained within the new binding AND
            // A. Don't have a parent, OR
            // B. Have a parent that is not contained within the new binding
            foreach (ContainerBinding childBinding in _bindingByContainer.Values)
            {
                if (forceRoot || _parentFinder.Contains(container, childBinding.Container))
                {
                    if (childBinding.Parent == null)
                    {
                        RemoveRootBinding(childBinding);
                        childBinding.Parent = binding;
                    }
                    else if (!_parentFinder.Contains(container, childBinding.Parent.Container) && !forceRoot)
                    {
                        childBinding.Parent = binding;
                    }
                }
            }

            if (_containerAdd != null)
            {
                _containerAdd(binding.Container);
            }
            if (_fallbackContainerAdd != null)
            {
                _fallbackContainerAdd(binding.Container);
            }
            return(binding);
        }
        /*============================================================================*/
        /* Public Functions                                                           */
        /*============================================================================*/

        public void AddContainer(object container)
        {
            if (!ValidContainer(container))
            {
                return;
            }

            _containers.Add(container);

            ContainerBinding containerBinding = _registry.AddContainer(container);

            AddHandlers(containerBinding);

            if (_containerAdded != null)
            {
                _containerAdded(container);
            }
        }
        public void returns_null_if_search_item_is_not_inside_an_included_view()
        {
            List <TreeContainerSupport> searchTrees = CreateTrees(5, 4);

            registry.AddContainer(searchTrees[0]);
            registry.AddContainer(searchTrees[1]);
            registry.AddContainer(searchTrees[1].children[3].children[2].children[3]);
            registry.AddContainer(searchTrees[1].children[3].children[2]);
            registry.AddContainer(searchTrees[1].children[3]);

            registry.RemoveContainer(searchTrees[1].children[3].children[2]);

            SupportContainer searchItem = searchTrees[2].children[3].children[2].children[3].children[3];

            ContainerBinding result = registry.FindParentBinding(searchItem);

            Assert.That(result, Is.Null, "Returns null if not inside an included view");
        }
        private void RemoveBinding(ContainerBinding binding)
        {
            // Remove the binding itself
            _bindingByContainer.Remove(binding.Container);
            _bindings.Remove(binding);

            // Drop the empty binding listener
            binding.BINDING_EMPTY -= OnBindingEmpty;

            if (binding.Parent == null)
            {
                // This binding didn't have a parent, so it was a Root
                RemoveRootBinding(binding);
            }

            // Re-parent the bindings
            foreach (ContainerBinding childBinding in _bindingByContainer.Values)
            {
                if (childBinding.Parent == binding)
                {
                    childBinding.Parent = binding.Parent;
                    if (childBinding.Parent == null)
                    {
                        // This binding used to have a parent,
                        // but no longer does, so it is now a Root
                        AddRootBinding(childBinding);
                    }
                }
            }

            if (_containerRemove != null)
            {
                _containerRemove(binding.Container);
            }

            if (binding == _fallbackBinding)
            {
                _fallbackBinding = null;
                if (_fallbackContainerRemove != null)
                {
                    _fallbackContainerRemove(binding.Container);
                }
            }
        }
        public void returns_root_container_view_bindings_many_items()
        {
            List <TreeContainerSupport> searchTrees = CreateTrees(5, 4);

            ContainerBinding firstExpectedBinding = registry.AddContainer(searchTrees[0]);

            registry.AddContainer(searchTrees[1].children[3].children[2].children[3]);
            registry.AddContainer(searchTrees[1].children[3].children[2]);

            ContainerBinding secondExpectedBinding = registry.AddContainer(searchTrees[1]);

            registry.AddContainer(searchTrees[1].children[3]);

            List <ContainerBinding> expectedRootBindings = new List <ContainerBinding> {
                firstExpectedBinding, secondExpectedBinding
            };

            Assert.That(expectedRootBindings, Is.EquivalentTo(registry.RootBindings), "Returns root container view bindings one item");
        }
        public void RemoveContainer(object container)
        {
            if (!_containers.Remove(container))
            {
                return;
            }

            ContainerBinding binding = _registry.GetBinding(container);

            if (binding != null)
            {
                RemoveHandlers(binding);
            }

            if (_containerRemoved != null)
            {
                _containerRemoved(container);
            }
        }
        public ContainerBinding SetFallbackContainer(object container)
        {
            if (_fallbackBinding != null)
            {
                RemoveBinding(_fallbackBinding);
            }

            if (container == null)
            {
                return(null);
            }

            ContainerBinding binding;

            if (_bindingByContainer.TryGetValue(container, out binding))
            {
                return(_fallbackBinding = binding);
            }

            return(_fallbackBinding = _bindingByContainer[container] = CreateBinding(container, true));
        }
 public void RemoveFallbackContainer()
 {
     _fallbackBinding = null;
 }
 private void OnBindingEmpty(ContainerBinding binding)
 {
     RemoveBinding(binding);
 }