Exemplo n.º 1
0
            internal void Parent(ModelItem parent, ModelItem child)
            {
                if (this._featureManager == null)
                {
                    throw new NotSupportedException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_ParentNotSupported, new object[2]
                    {
                        (object)parent.ItemType.Name,
                        (object)child.ItemType.Name
                    }));
                }
                ParentAdapter parentAdapter1 = (ParentAdapter)null;

                using (IEnumerator <FeatureProvider> enumerator = FeatureExtensions.CreateFeatureProviders(this._featureManager, typeof(ParentAdapter), parent).GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        ParentAdapter parentAdapter2 = (ParentAdapter)enumerator.Current;
                        ModelItem     parent1        = parentAdapter2.RedirectParent(parent, child.ItemType);
                        if (parent1 == null)
                        {
                            throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_InvalidRedirectParent, new object[1]
                            {
                                (object)parentAdapter2.GetType().Name
                            }));
                        }
                        if (parent1 != parent)
                        {
                            this.Parent(parent1, child);
                            return;
                        }
                        parentAdapter1 = parentAdapter2;
                    }
                }
                if (parentAdapter1 == null || !parentAdapter1.CanParent(parent, child.ItemType))
                {
                    throw new NotSupportedException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_ParentNotSupported, new object[2]
                    {
                        (object)parent.ItemType.Name,
                        (object)child.ItemType.Name
                    }));
                }
                ModelItem parent2 = child.Parent;

                if (parent2 == parent)
                {
                    return;
                }
                if (parent2 != null)
                {
                    using (IEnumerator <FeatureProvider> enumerator = FeatureExtensions.CreateFeatureProviders(this._featureManager, typeof(ParentAdapter), parent2).GetEnumerator())
                    {
                        if (enumerator.MoveNext())
                        {
                            ((ParentAdapter)enumerator.Current).RemoveParent(parent2, parent, child);
                        }
                    }
                }
                parentAdapter1.Parent(parent, child);
            }
Exemplo n.º 2
0
 internal bool CanParent(ModelItem parent, Type childType, ModelItem childItem, out ModelItem redirectedParent)
 {
     redirectedParent = (ModelItem)null;
     if (this._featureManager == null)
     {
         return(false);
     }
     using (IEnumerator <FeatureProvider> enumerator = FeatureExtensions.CreateFeatureProviders(this._featureManager, typeof(ParentAdapter), parent).GetEnumerator())
     {
         if (enumerator.MoveNext())
         {
             ParentAdapter parentAdapter = (ParentAdapter)enumerator.Current;
             redirectedParent = parentAdapter.RedirectParent(parent, childType);
             if (redirectedParent == null)
             {
                 throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_InvalidRedirectParent, new object[1]
                 {
                     (object)parentAdapter.GetType().Name
                 }));
             }
             if (redirectedParent.Equals((object)childItem))
             {
                 return(false);
             }
             ModelItem modelItem     = redirectedParent;
             ViewItem  logicalParent = ModelParent.ItemParentImplementationService.GetLogicalParent(modelItem);
             if (childItem != null)
             {
                 for (; logicalParent != (ViewItem)null; logicalParent = ModelParent.ItemParentImplementationService.GetLogicalParent(modelItem))
                 {
                     if (logicalParent.Equals((object)childItem.View))
                     {
                         return(false);
                     }
                     modelItem = modelItem.Parent;
                 }
             }
             if (childItem != null && parentAdapter.IsParent(redirectedParent, childItem))
             {
                 return(true);
             }
             if (redirectedParent == parent)
             {
                 return(parentAdapter.CanParent(parent, childType));
             }
             parent = redirectedParent;
             return(this.CanParent(parent, childType, childItem, out redirectedParent));
         }
     }
     return(false);
 }
Exemplo n.º 3
0
        /// <summary>
        /// When redirecting the add operation, find the parent to add the new control to.
        /// </summary>
        /// <param name="adapterService">Adapter service.</param>
        /// <param name="parent">The parent item.</param>
        /// <param name="childType">The type of child item.</param>
        /// <param name="index">Index of the last child of the parent.</param>
        /// <returns>The parent to add the new control to.</returns>
        private static ModelItem FindSuitableParent(AdapterService adapterService, ModelItem parent, Type childType, int index)
        {
            ModelItem suitableParent = null;

            if (adapterService != null)
            {
                ParentAdapter parentAdapter = adapterService.GetAdapter <ParentAdapter>(parent.Content.Collection[index].ItemType);
                Debug.Assert(parentAdapter != null, "Parent Adapter cannot be null");
                List <ModelItem> parentList       = new List <ModelItem>();
                ModelItem        targetParent     = parent.Content.Collection[index];
                ModelItem        redirectedParent = parentAdapter.RedirectParent(parent.Content.Collection[index], childType);
                parentList.Add(targetParent);
                parentList.Add(redirectedParent);
                while (redirectedParent != targetParent)
                {
                    targetParent  = redirectedParent;
                    parentAdapter = adapterService.GetAdapter <ParentAdapter>(targetParent.ItemType);
                    Debug.Assert(parentAdapter != null, "Parent Adapter for Redirected Parent cannot be null");
                    redirectedParent = parentAdapter.RedirectParent(targetParent, childType);
                    if (parentList.Contains(redirectedParent))
                    {
                        break; // To avoid recursion
                    }
                    else
                    {
                        parentList.Add(redirectedParent);
                    }
                }

                if (parentAdapter.CanParent(targetParent, childType))
                {
                    suitableParent = targetParent;
                }
            }

            return(suitableParent);
        }