コード例 #1
0
ファイル: TestViewSupport.cs プロジェクト: valmac/nesper
        public void TestFindDescendent()
        {
            // Test a deep find
            IList <View> descendents = ViewSupport.FindDescendent(top, child_2_2_1);

            Assert.AreEqual(2, descendents.Count);
            Assert.AreEqual(child_2, descendents[0]);
            Assert.AreEqual(child_2_2, descendents[1]);

            descendents = ViewSupport.FindDescendent(top, child_2_1_1);
            Assert.AreEqual(2, descendents.Count);
            Assert.AreEqual(child_2, descendents[0]);
            Assert.AreEqual(child_2_1, descendents[1]);

            descendents = ViewSupport.FindDescendent(top, child_2_1);
            Assert.AreEqual(1, descendents.Count);
            Assert.AreEqual(child_2, descendents[0]);

            // Test a shallow find
            descendents = ViewSupport.FindDescendent(top, child_2);
            Assert.AreEqual(0, descendents.Count);

            // Test a no find
            descendents = ViewSupport.FindDescendent(top, new SupportSchemaNeutralView());
            Assert.AreEqual(null, descendents);
        }
コード例 #2
0
        /// <summary>
        /// Removes a view from a parent view returning the orphaned parent views in a list.
        /// </summary>
        /// <param name="parentViewable">parent to remove view from</param>
        /// <param name="viewToRemove">view to remove</param>
        /// <returns>
        /// chain of orphaned views
        /// </returns>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="IllegalStateException">
        /// </exception>
        protected internal static IList <View> RemoveChainLeafView(Viewable parentViewable, Viewable viewToRemove)
        {
            IList <View> removedViews = new List <View>();

            // The view to remove must be a leaf node - non-leaf views are just not removed
            if (viewToRemove.HasViews)
            {
                return(removedViews);
            }

            // Find child viewToRemove among descendent views
            IList <View> viewPath = ViewSupport.FindDescendent(parentViewable, viewToRemove);

            if (viewPath == null)
            {
                String message = "Viewable not found when removing view " + viewToRemove;
                throw new ArgumentException(message);
            }

            // The viewToRemove is a direct child view of the stream
            if (viewPath.IsEmpty())
            {
                bool isViewRemoved = parentViewable.RemoveView((View)viewToRemove);

                if (!isViewRemoved)
                {
                    String message = "Failed to remove immediate child view " + viewToRemove;
                    Log.Fatal(".remove " + message);
                    throw new IllegalStateException(message);
                }

                removedViews.Add((View)viewToRemove);
                return(removedViews);
            }

            View[] viewPathArray = viewPath.ToArray();
            View   currentView   = (View)viewToRemove;

            // Remove child from parent views until a parent view has more children,
            // or there are no more parents (index=0).
            for (int index = viewPathArray.Length - 1; index >= 0; index--)
            {
                bool isViewRemoved = viewPathArray[index].RemoveView(currentView);
                removedViews.Add(currentView);

                if (!isViewRemoved)
                {
                    String message = "Failed to remove view " + currentView;
                    Log.Fatal(".remove " + message);
                    throw new IllegalStateException(message);
                }

                // If the parent views has more child views, we are done
                if (viewPathArray[index].HasViews)
                {
                    break;
                }

                // The parent of the top parent is the stream, remove from stream
                if (index == 0)
                {
                    parentViewable.RemoveView(viewPathArray[0]);
                    removedViews.Add(viewPathArray[0]);
                }
                else
                {
                    currentView = viewPathArray[index];
                }
            }

            return(removedViews);
        }