Ejemplo n.º 1
0
        internal ExplorerEFElement GetExplorerEFElementForEFObject(EditingContext editingContext, EFObject targetEFObject)
        {
            if (targetEFObject == null)
            {
                return(null);
            }

            //
            //  Find the EFObject we want to navigate to for the given EFObject
            //
            var targetEFElement = GetNavigationTarget(targetEFObject);

            //
            // get all EFElement nodes on the path from our target node to the root.
            //
            var elementStack = new LinkedList <EFElement>();
            var e            = targetEFElement;

            while (e != null)
            {
                elementStack.AddLast(e);
                e = e.Parent as EFElement;
            }

            //
            // unwind the stack, making sure that each element's explorer model node exists.
            //
            var explorerElement = ViewModel.RootNode;

            while (elementStack.Count > 0)
            {
                var e2 = elementStack.Last.Value;
                elementStack.RemoveLast();
                var viewModelType = ModelToExplorerModelXRef.GetViewModelTypeForEFElement(editingContext, e2);
                // if viewModelType is null then that kind of EFElement is not displayed
                // in the Explorer (e.g. S-side StorageEntityContainer and children)
                if (null != viewModelType)
                {
                    var nextElement = ModelToExplorerModelXRef.GetNewOrExisting(editingContext, e2, explorerElement, viewModelType);

                    // There are "dummy-nodes" in the view-model that don't correspond to a node in the model.
                    // We need to skip over them here.
                    if (nextElement != null &&
                        elementStack.Count > 0)
                    {
                        nextElement = nextElement.GetParentNodeForElement(elementStack.Last.Value);
                        Debug.Assert(nextElement != null, "GetParentNodeForElement returned null");
                    }

                    if (nextElement != null)
                    {
                        explorerElement = nextElement;
                    }
                }
            }
            Debug.Assert(explorerElement != null, "no explorer element found for targetEFObject");

            if (explorerElement != null)
            {
                //
                // now make sure that our target node in the explorer tree is visible.
                //
                explorerElement.ExpandTreeViewToMe();
            }

            return(explorerElement);
        }