TableTextElementCollectionInternal.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / MS / Internal / documents / TableTextElementCollectionInternal.cs / 1305600 / TableTextElementCollectionInternal.cs

                            using MS.Utility; 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics; 
using System.Windows;
using System.Windows.Documents; 
 
namespace MS.Internal.Documents
{ 
    /// 
    /// This class is used to generate code for row, row group, and cell collections in 
    /// 
    ///  
    /// 
    internal class TableTextElementCollectionInternal 
        : ContentElementCollection 
        where TParent : TextElement, IAcceptInsertion
        where TElementType : TextElement, IIndexedChild 
    {
        internal TableTextElementCollectionInternal(TParent owner)
            : base(owner)
        { 
        }
 
        ///  
        /// Appends a TItem to the end of the ContentElementCollection.
        ///  
        /// The TItem to be added to the end of the ContentElementCollection.
        /// The ContentElementCollection index at which the TItem has been added.
        /// Adding a null is prohibited.
        ///  
        /// If the item value is null.
        ///  
        ///  
        /// If the new child already has a parent.
        ///  
        public override void Add(TElementType item)
        {
            Version++;
 
            if (item == null)
            { 
                throw new ArgumentNullException("item"); 
            }
 
            if (item.Parent != null)
            {
                throw new System.ArgumentException(SR.Get(SRID.TableCollectionInOtherCollection));
            } 

            Owner.InsertionIndex = Size; 
            item.RepositionWithContent(Owner.ContentEnd); 
            Owner.InsertionIndex = -1;
        } 

        /// 
        /// Removes all elements from the ContentElementCollection.
        ///  
        /// 
        /// Count is set to zero. Capacity remains unchanged. 
        /// To reset the capacity of the ContentElementCollection, call TrimToSize 
        /// or set the Capacity property directly.
        ///  
        public override void Clear()
        {
            Version++;
 
            for (int i = Size - 1; i >= 0; --i)
            { 
                Debug.Assert(BelongsToOwner(Items[i])); 

                Remove(Items[i]); 
            }
            Size = 0;
        }
 
        /// 
        /// Inserts a TItem into the ContentElementCollection at the specified index. 
        ///  
        /// The zero-based index at which value should be inserted.
        /// The TItem to insert.  
        /// 
        /// indexc> is less than zero.
        /// -or-
        /// index is greater than Count. 
        /// 
        ///  
        /// If the item value is null. 
        /// 
        ///  
        /// If Count already equals Capacity, the capacity of the
        /// ContentElementCollection is increased before the new TItem is inserted.
        ///
        /// If index is equal to Count, TItem is added to the 
        /// end of ContentElementCollection.
        /// 
        /// The TItems that follow the insertion point move down to 
        /// accommodate the new TItem. The indexes of the TItems that are
        /// moved are also updated. 
        /// 
        public override void Insert(int index, TElementType item)
        {
            Version++; 

            if (index < 0 || index > Size) 
            { 
                throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionOutOfRange));
            } 
            if (item == null)
            {
                throw new ArgumentNullException("item");
            } 

            if (item.Parent != null) 
            { 
                throw new System.ArgumentException(SR.Get(SRID.TableCollectionInOtherCollection));
            } 

            Owner.InsertionIndex = index;
            if (index == Size)
            { 
                item.RepositionWithContent(Owner.ContentEnd);
            } 
            else 
            {
                TElementType itemInsert = Items[index]; 
                TextPointer insertPosition = new TextPointer(itemInsert.ContentStart, -1);
                item.RepositionWithContent(insertPosition);
            }
            Owner.InsertionIndex = -1; 
        }
 
        ///  
        /// Removes the specified TItem from the ContentElementCollection.
        ///  
        /// The TItem to remove from the ContentElementCollection.
        /// 
        /// If the item value is null.
        ///  
        /// 
        /// If the specified TItem is not in this collection. 
        ///  
        /// 
        /// The TItems that follow the removed TItem move up to occupy 
        /// the vacated spot. The indices of the TItems that are moved
        /// also updated.
        /// 
        public override bool Remove(TElementType item) 
        {
            Version++; 
 
            if (item == null)
            { 
                throw new ArgumentNullException("item");
            }
            if (!BelongsToOwner(item))
            { 
                return false;
            } 
 
            TextPointer startPosition = new TextPointer(item.TextContainer, item.TextElementNode, ElementEdge.BeforeStart, LogicalDirection.Backward);
            TextPointer endPosition = new TextPointer(item.TextContainer, item.TextElementNode, ElementEdge.AfterEnd, LogicalDirection.Backward); 

            Owner.TextContainer.BeginChange();
            try
            { 
                Owner.TextContainer.DeleteContentInternal(startPosition, endPosition);
            } 
            finally 
            {
                Owner.TextContainer.EndChange(); 
            }

            return true;
        } 

        ///  
        /// Removes the TItem at the specified index. 
        /// 
        /// The zero-based index of the TItem to remove. 
        /// 
        /// index is less than zero
        /// - or -
        /// index is equal or greater than count. 
        /// 
        ///  
        /// The TItems that follow the removed TItem move up to occupy 
        /// the vacated spot. The indices of the TItems that are moved
        /// also updated. 
        /// 
        public override void RemoveAt(int index)
        {
            Version++; 

            if (index < 0 || index >= Size) 
            { 
                throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionOutOfRange));
            } 

            Remove(Items[index]);
        }
 

 
        ///  
        /// Removes a range of TItems from the ContentElementCollection.
        ///  
        /// The zero-based index of the range
        /// of TItems to remove
        /// The number of TItems to remove.
        ///  
        /// index is less than zero.
        /// -or- 
        /// count is less than zero. 
        /// 
        ///  
        /// index and count do not denote a valid range of TItems in the ContentElementCollection.
        /// 
        /// 
        /// The TItems that follow the removed TItems move up to occupy 
        /// the vacated spot. The indices of the TItems that are moved are
        /// also updated. 
        ///  
        public override void RemoveRange(int index, int count)
        { 
            Version++;

            if (index < 0 || index >= Size)
            { 
                throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionOutOfRange));
            } 
            if (count < 0) 
            {
                throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionCountNeedNonNegNum)); 
            }
            if (Size - index < count)
            {
                throw new ArgumentException(SR.Get(SRID.TableCollectionRangeOutOfRange)); 
            }
 
            if (count > 0) 
            {
                for (int i = index + count - 1; i >= index; --i) 
                {
                    Debug.Assert(BelongsToOwner(Items[i]));
                    Remove(Items[i]);
                } 
            }
 
        } 

        ///  
        /// Sets the specified TItem at the specified index;
        /// Connects the item to the model tree;
        /// Notifies the TItem about the event.
        ///  
        /// 
        /// If the new item has already a parent or if the slot at the specified index is not null. 
        ///  
        /// 
        /// Note that the function requires that _item[index] == null and 
        /// it also requires that the passed in item is not included into another ContentElementCollection.
        /// 
        internal override void PrivateConnectChild(int index, TElementType item)
        { 
            Debug.Assert(item != null && item.Index == -1);
            Debug.Assert(Items[index] == null); 
 
            // If the TElementType is already parented correctly through a proxy, there's no need
            // to change parentage.  Otherwise, it should be parented to Owner. 
            if (item.Parent is DummyProxy)
            {
                if (LogicalTreeHelper.GetParent(item.Parent) != Owner)
                { 
                    throw new System.ArgumentException(SR.Get(SRID.TableCollectionWrongProxyParent));
                } 
            } 

            // add the item into collection's array 
            Items[index] = item;
            item.Index = index;

            // notify the TElementType about the change 
            item.OnEnterParentTree();
        } 
 

        ///  
        /// Notifies the TItem about the event;
        /// Disconnects the item from the model tree;
        /// Sets the TItem's slot in the collection's array to null.
        ///  
        internal override void PrivateDisconnectChild(TElementType item)
        { 
            Debug.Assert(BelongsToOwner(item) && Items[item.Index] == item); 

            int index = item.Index; 

            item.OnExitParentTree();

            // remove the item from collection's array 
            Items[item.Index] = null;
            item.Index = -1; 
 
            --Size;
 
            for (int i = index; i < Size; ++i)
            {
                Debug.Assert(BelongsToOwner(Items[i + 1]));
 
                Items[i] = Items[i + 1];
                Items[i].Index = i; 
            } 

            Items[Size] = null; 

            item.OnAfterExitParentTree(Owner);
        }
 

        // Helper method - Searches the children collection for the index an item currently exists at - 
        // NOTE - ITEM MUST BE IN TEXT TREE WHEN THIS IS CALLED. 
        internal int FindInsertionIndex(TElementType item)
        { 
            int index = 0;
            object objectSearchFor = item;

            if (item.Parent is DummyProxy) 
            {
                objectSearchFor = item.Parent; 
            } 

            IEnumerator enumChildren = Owner.IsEmpty 
                    ? new RangeContentEnumerator(null, null)
                    : new RangeContentEnumerator(Owner.ContentStart, Owner.ContentEnd);

            while (enumChildren.MoveNext()) 
            {
                if (objectSearchFor == enumChildren.Current) 
                { 
                    return index;
                } 

                if (enumChildren.Current is TElementType || enumChildren.Current is DummyProxy)
                {
                    index++; 
                }
                else 
                { 
                    // We handle junk in the tree, but it really shouldn't be there.
                    Debug.Assert(false, "Garbage in logical tree."); 
                }
            }

            MS.Internal.Invariant.Assert(false); 
            return -1;
        } 
 

        internal void InternalAdd(TElementType item) 
        {
            if (Size == Items.Length)
            {
                EnsureCapacity(Size + 1); 
            }
 
            int index; 

            index = Owner.InsertionIndex; 

            if (index == -1)
            {
                index = FindInsertionIndex(item); 
            }
 
            for (int i = Size - 1; i >= index; --i) 
            {
                Debug.Assert(BelongsToOwner(Items[i])); 

                Items[i + 1] = Items[i];
                Items[i].Index = i + 1;
            } 

            Items[index] = null; 
 
            Size++;
            PrivateConnectChild(index, item); 
        }

        /// 
        /// Performs the actual work of notifying item it is leaving the array, and disconnecting it. 
        /// 
        internal void InternalRemove(TElementType item) 
        { 
            Debug.Assert(BelongsToOwner(item) && Items[item.Index] == item);
 
            PrivateDisconnectChild(item);
        }
        /// 
        /// Indexer for the TableCellCollection. Gets the TableCell stored at the 
        /// zero-based index of the TableCellCollection.
        ///  
        /// This property provides the ability to access a specific TableCell in the 
        /// TableCellCollection by using the following systax: TableCell myTableCell = myTableCellCollection[index].
        ///  
        /// 
        /// index is less than zero -or- index is equal to or greater than Count.
        /// 
        public override TElementType this[int index] 
        {
            get 
            { 

                if (index < 0 || index >= Size) 
                {
                    throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionOutOfRange));
                }
                return (Items[index]); 
            }
            set 
            { 
                if (index < 0 || index >= Size)
                { 
                    throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionOutOfRange));
                }

                if (value == null) 
                {
                    throw new ArgumentNullException("value"); 
                } 

                if (value.Parent != null) 
                {
                    throw new System.ArgumentException(SR.Get(SRID.TableCollectionInOtherCollection));
                }
 
                this.RemoveAt(index);
                this.Insert(index, value); 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK