ContentElementCollection.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

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

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// File: ContentElementCollection.cs 
//
// Description: Generic Collection of Table related objects. 
// 
//---------------------------------------------------------------------------
 
using MS.Utility;
using System;
using System.Collections.Generic;
using System.Collections; 
using System.Diagnostics;
using System.Threading; 
using System.Windows; 
using System.Windows.Documents;
 
namespace MS.Internal.Documents
{

    ///  
    /// A ContentElementCollection is an ordered collection of TItems.
    ///  
    ///  
    /// ContentElementCollection provides public access for TItems
    /// reading and manipulating. 
    /// 
    internal abstract class ContentElementCollection : IList, IList
        where TParent : TextElement, IAcceptInsertion
        where TItem : FrameworkContentElement, IIndexedChild 
    {
        //----------------------------------------------------- 
        // 
        //  Constructors
        // 
        //-----------------------------------------------------

        #region Constructors
 
        internal ContentElementCollection(TParent owner)
        { 
            Debug.Assert(owner != null); 
            _owner = owner;
            Items = new TItem[DefaultCapacity]; 
        }

        #endregion Constructors
 
        //------------------------------------------------------
        // 
        //  Public Methods 
        //
        //----------------------------------------------------- 

        #region Public Methods

        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        public void CopyTo(Array array, int index) 
        {
            if (array == null)
            {
                throw new ArgumentNullException("array"); 
            }
            if (array.Rank != 1) 
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionRankMultiDimNotSupported));
            } 
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", SR.Get(SRID.TableCollectionOutOfRangeNeedNonNegNum));
            } 
            if (array.Length - index < Size)
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionInvalidOffLen)); 
            }
 
            Array.Copy(Items, 0, array, index, Size);
        }

        ///  
        /// Strongly typed version of ICollection.CopyTo.
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        public void CopyTo(TItem[] array, int index)
        { 
            if (array == null)
            { 
                throw new ArgumentNullException("array"); 
            }
            if (index < 0) 
            {
                throw new ArgumentOutOfRangeException("index", SR.Get(SRID.TableCollectionOutOfRangeNeedNonNegNum));
            }
            if (array.Length - index < Size) 
            {
                throw new ArgumentException(SR.Get(SRID.TableCollectionInvalidOffLen)); 
            } 

            Array.Copy(Items, 0, array, index, Size); 
        }

        IEnumerator IEnumerable.GetEnumerator()
        { 
            return this.GetEnumerator();
        } 
        ///  
        ///     
        ///  
        internal IEnumerator GetEnumerator()
        {
            return (new ContentElementCollectionEnumeratorSimple(this));
        } 

        ///  
        ///      
        /// 
        IEnumerator IEnumerable.GetEnumerator() 
        {
            return (new ContentElementCollectionEnumeratorSimple(this));
        }
 
        /// 
        /// 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. 
        ///  
        abstract public void Add(TItem item);
 
        /// 
        /// 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. 
        /// 
        abstract public void Clear(); 

        /// 
        /// Determines whether a TItem is in the ContentElementCollection.
        ///  
        /// The TItem to locate in the ContentElementCollection.
        /// The value can be a null reference. 
        /// true if TItem is found in the ContentElementCollection; 
        /// otherwise, false.
        public bool Contains(TItem item) 
        {
            if (BelongsToOwner(item))
            {
                Debug.Assert(Items[item.Index] == item); 
                return (true);
            } 
 
            return (false);
        } 

        /// 
        /// Returns the zero-based index of the TItem. If the TItem is not
        /// in the ContentElementCollection, -1 is returned. 
        /// 
        /// The TItem to locate in the ContentElementCollection. 
        public int IndexOf(TItem item) 
        {
            if (BelongsToOwner(item)) 
            {
                return item.Index;
            }
            else 
            {
                return (-1); 
            } 
        }
 
        /// 
        /// 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.
        ///  
        abstract public void Insert(int index, TItem item); 

        ///  
        /// 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. 
        /// 
        abstract public bool Remove(TItem item); 
 
        /// 
        /// 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.
        /// 
        abstract public void RemoveAt(int 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. 
        /// 
        abstract public void RemoveRange(int index, int count); 


        /// 
        /// Sets the capacity to the actual number of elements in the ContentElementCollection. 
        /// 
        ///  
        /// This method can be used to minimize a ContentElementCollection's memory overhead 
        /// if no new elements will be added to the collection.
        /// 
        /// To completely clear all elements in a ContentElementCollection, call the Clear method
        /// before calling TrimToSize.
        /// 
        public void TrimToSize() 
        {
            PrivateCapacity = Size; 
        } 

        #endregion Public Methods 

        //------------------------------------------------------
        //
        //  Private Types 
        //
        //------------------------------------------------------ 
 
        #region Protected Types
 
        protected class ContentElementCollectionEnumeratorSimple : IEnumerator, IEnumerator
        {
            internal ContentElementCollectionEnumeratorSimple(ContentElementCollection collection)
            { 
                Debug.Assert(collection != null);
 
                _collection = collection; 
                _index = -1;
                Version = _collection.Version; 
                _currentElement = collection;
            }

            public bool MoveNext() 
            {
                if (Version != _collection.Version) 
                { 
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged));
                } 

                if (_index < (_collection.Size - 1))
                {
                    _index++; 
                    _currentElement = _collection[_index];
                    return (true); 
                } 
                else
                { 
                    _currentElement = _collection;
                    _index = _collection.Size;
                    return (false);
                } 
            }
 
            public TItem Current 
            {
                get 
                {
                    if (_currentElement == _collection)
                    {
                        if (_index == -1) 
                        {
                            throw new InvalidOperationException(SR.Get(SRID.EnumeratorNotStarted)); 
                        } 
                        else
                        { 
                            throw new InvalidOperationException(SR.Get(SRID.EnumeratorReachedEnd));
                        }
                    }
                    return (TItem)_currentElement; 
                }
            } 
 
            /// 
            ///      
            /// 
            object IEnumerator.Current
            {
                get 
                {
                    return this.Current; 
                } 
            }
 

            public void Reset()
            {
                if (Version != _collection.Version) 
                {
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged)); 
                } 
                _currentElement = _collection;
                _index = -1; 
            }

            public void Dispose()
            { 
                GC.SuppressFinalize(this);
            } 
 

            private ContentElementCollection _collection; 
            private int _index;
            protected int Version;
            private object _currentElement;
        } 

        protected class DummyProxy : DependencyObject 
        { 
        }
 
        #endregion Protected Types

        //-------------------------------------------------------------------
        // 
        //  IList Members
        // 
        //-------------------------------------------------------------------- 
        #region IList Members
 
        int IList.Add(object value)
        {
            if (value == null)
            { 
                throw new ArgumentNullException("value");
            } 
 
            TItem item = value as TItem;
 
            // : This chunk is moved to the correcponding override in TableCollumnCollection, to keep same behavior
            //if (item == null)
            //{
            //    throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value"); 
            //}
 
            this.Add(item); 

            return ((IList)this).IndexOf(item); 
        }

        void IList.Clear()
        { 
            this.Clear();
        } 
 
        bool IList.Contains(object value)
        { 
            TItem item = value as TItem;

            if (item == null)
            { 
                return false;
            } 
 
            return this.Contains(item);
        } 

        int IList.IndexOf(object value)
        {
            TItem item = value as TItem; 

            if (item == null) 
            { 
                return -1;
            } 

            return this.IndexOf(item);
        }
 
        void IList.Insert(int index, object value)
        { 
            if (value == null) 
            {
                throw new ArgumentNullException("value"); 
            }

            TItem newItem = value as TItem;
 
            if (newItem == null)
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value"); 
            }
 
            this.Insert(index, newItem);
        }

        bool IList.IsFixedSize 
        {
            get 
            { 
                return false;
            } 
        }

        bool IList.IsReadOnly
        { 
            get
            { 
                return this.IsReadOnly; 
            }
        } 

        void IList.Remove(object value)
        {
            TItem item = value as TItem; 

            if (item == null) 
            { 
                return;
            } 

            this.Remove(item);
        }
 
        void IList.RemoveAt(int index)
        { 
            this.RemoveAt(index); 
        }
 
        object IList.this[int index]
        {
            get
            { 
                return this[index];
            } 
 
            set
            { 
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                } 

                TItem item = value as TItem; 
 
                if (item == null)
                { 
                    throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value");
                }

                this[index] = item; 
            }
        } 
 
        #endregion IList Members
 

        //-----------------------------------------------------
        //
        //  Public Properties 
        //
        //----------------------------------------------------- 
 
        #region Public Properties
 
        public abstract TItem this[int index]
        {
            get;
            set; 
        }
 
        ///  
        /// 
        ///  
        public int Count
        {
            get
            { 
                return (Size);
            } 
        } 

        ///  
        ///     
        ///     
        /// 
        public bool IsReadOnly  //  bool IList.IsReadOnly {get;}; bool ICollection.IsReadOnly {get;} 
        {
            get 
            { 
                return false;
            } 
        }

        /// 
        ///  
        /// 
        /// Always returns false. 
        ///  
        /// 
        public bool IsSynchronized 
        {
            get
            {
                return (false); 
            }
        } 
 
        /// 
        ///  
        /// 
        public object SyncRoot
        {
            get 
            {
                // 
 
                return (this);
            } 
        }

        /// 
        /// Gets or sets the number of elements that the ContentElementCollection can contain. 
        /// 
        ///  
        /// The number of elements that the ContentElementCollection can contain. 
        /// 
        ///  
        /// Capacity is the number of elements that the ContentElementCollection is capable of storing.
        /// Count is the number of Visuals that are actually in the ContentElementCollection.
        ///
        /// Capacity is always greater than or equal to Count. If Count exceeds 
        /// Capacity while adding elements, the capacity of the ContentElementCollection is increased.
        /// 
        /// By default the capacity is 8. 
        /// 
        ///  
        /// Capacity is set to a value that is less than Count.
        /// 
        /// 
        public int Capacity 
        {
            get 
            { 
                return PrivateCapacity;
            } 
            set
            {
                PrivateCapacity = value;
            } 
        }
        public TParent Owner 
        { 
            get { return _owner; }
        } 

        #endregion Public Properties

 
        #region Protected Properties
 
        protected TItem[] Items 
        {
            get { return _items; } 
            private set { _items = value; }
        }

        protected int Size 
        {
            get { return _size; } 
            set { _size = value; } 
        }
 
        protected int Version
        {
            get { return _version; }
            set { _version = value; } 
        }
 
        protected int DefaultCapacity 
        {
            get { return c_defaultCapacity; } 
        }
        #endregion Protected Properties

        //----------------------------------------------------- 
        //
        //  Private Methods 
        // 
        //------------------------------------------------------
 
        #region Internal Methods


        ///  
        /// Ensures that the capacity of this list is at least the given minimum
        /// value. If the currect capacity of the list is less than min, the 
        /// capacity is increased to min. 
        /// 
        internal void EnsureCapacity(int min) 
        {
            if (PrivateCapacity < min)
            {
                PrivateCapacity = Math.Max(min, PrivateCapacity * 2); 
            }
        } 
 
        /// 
        /// 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. 
        /// 
        abstract internal void PrivateConnectChild(int index, TItem item);

 
        /// 
        /// 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.
        ///  
        abstract internal void PrivateDisconnectChild(TItem item);


        ///  
        /// Removes specified TItem from the ContentElementCollection.
        ///  
        /// TItem to remove. 
        internal void PrivateRemove(TItem item)
        { 
            Debug.Assert(BelongsToOwner(item) && Items[item.Index] == item);

            int index = item.Index;
 
            PrivateDisconnectChild(item);
 
            --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;
        } 

        // helper method: return true if the item belongs to the collection's owner
        internal bool BelongsToOwner(TItem item)
        { 
            if (item == null)
                return false; 
 
            DependencyObject node = item.Parent;
            if (node is DummyProxy) 
            {
                node = LogicalTreeHelper.GetParent(node);
            }
 
            return (node == Owner);
        } 
 
        #endregion Internal Methods
 
        //-----------------------------------------------------
        //
        //  Private Properties
        // 
        //------------------------------------------------------
 
        #region Internal Properties 

        ///  
        /// PrivateCapacity sets/gets the Capacity of the collection.
        /// 
        internal int PrivateCapacity
        { 
            get
            { 
                return (Items.Length); 
            }
            set 
            {
                if (value != Items.Length)
                {
                    if (value < Size) 
                    {
                        throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionNotEnoughCapacity)); 
                    } 

                    if (value > 0) 
                    {
                        TItem[] newItems = new TItem[value];
                        if (Size > 0)
                        { 
                            Array.Copy(Items, 0, newItems, 0, Size);
                        } 
                        Items = newItems; 
                    }
                    else 
                    {
                        Items = new TItem[DefaultCapacity];
                    }
                } 
            }
        } 
 
        #endregion Internal Properties
 
        //------------------------------------------------------
        //
        //  Protected Fields
        // 
        //-----------------------------------------------------
 
        #region Protected Fields 
        private readonly TParent _owner;      //  owner of the collection
        private TItem[] _items;              //  storage of items 
        private int _size;                          //  size of the collection
        private int _version;                       //  version tracks updates in the collection
        protected const int c_defaultCapacity = 8;    //  default capacity of the collection
        #endregion Protected Fields 

    } 
 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// File: ContentElementCollection.cs 
//
// Description: Generic Collection of Table related objects. 
// 
//---------------------------------------------------------------------------
 
using MS.Utility;
using System;
using System.Collections.Generic;
using System.Collections; 
using System.Diagnostics;
using System.Threading; 
using System.Windows; 
using System.Windows.Documents;
 
namespace MS.Internal.Documents
{

    ///  
    /// A ContentElementCollection is an ordered collection of TItems.
    ///  
    ///  
    /// ContentElementCollection provides public access for TItems
    /// reading and manipulating. 
    /// 
    internal abstract class ContentElementCollection : IList, IList
        where TParent : TextElement, IAcceptInsertion
        where TItem : FrameworkContentElement, IIndexedChild 
    {
        //----------------------------------------------------- 
        // 
        //  Constructors
        // 
        //-----------------------------------------------------

        #region Constructors
 
        internal ContentElementCollection(TParent owner)
        { 
            Debug.Assert(owner != null); 
            _owner = owner;
            Items = new TItem[DefaultCapacity]; 
        }

        #endregion Constructors
 
        //------------------------------------------------------
        // 
        //  Public Methods 
        //
        //----------------------------------------------------- 

        #region Public Methods

        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        public void CopyTo(Array array, int index) 
        {
            if (array == null)
            {
                throw new ArgumentNullException("array"); 
            }
            if (array.Rank != 1) 
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionRankMultiDimNotSupported));
            } 
            if (index < 0)
            {
                throw new ArgumentOutOfRangeException("index", SR.Get(SRID.TableCollectionOutOfRangeNeedNonNegNum));
            } 
            if (array.Length - index < Size)
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionInvalidOffLen)); 
            }
 
            Array.Copy(Items, 0, array, index, Size);
        }

        ///  
        /// Strongly typed version of ICollection.CopyTo.
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        /// 
        ///  
        /// 
        ///  
        ///  
        /// 
        ///  
        /// 
        /// 
        public void CopyTo(TItem[] array, int index)
        { 
            if (array == null)
            { 
                throw new ArgumentNullException("array"); 
            }
            if (index < 0) 
            {
                throw new ArgumentOutOfRangeException("index", SR.Get(SRID.TableCollectionOutOfRangeNeedNonNegNum));
            }
            if (array.Length - index < Size) 
            {
                throw new ArgumentException(SR.Get(SRID.TableCollectionInvalidOffLen)); 
            } 

            Array.Copy(Items, 0, array, index, Size); 
        }

        IEnumerator IEnumerable.GetEnumerator()
        { 
            return this.GetEnumerator();
        } 
        ///  
        ///     
        ///  
        internal IEnumerator GetEnumerator()
        {
            return (new ContentElementCollectionEnumeratorSimple(this));
        } 

        ///  
        ///      
        /// 
        IEnumerator IEnumerable.GetEnumerator() 
        {
            return (new ContentElementCollectionEnumeratorSimple(this));
        }
 
        /// 
        /// 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. 
        ///  
        abstract public void Add(TItem item);
 
        /// 
        /// 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. 
        /// 
        abstract public void Clear(); 

        /// 
        /// Determines whether a TItem is in the ContentElementCollection.
        ///  
        /// The TItem to locate in the ContentElementCollection.
        /// The value can be a null reference. 
        /// true if TItem is found in the ContentElementCollection; 
        /// otherwise, false.
        public bool Contains(TItem item) 
        {
            if (BelongsToOwner(item))
            {
                Debug.Assert(Items[item.Index] == item); 
                return (true);
            } 
 
            return (false);
        } 

        /// 
        /// Returns the zero-based index of the TItem. If the TItem is not
        /// in the ContentElementCollection, -1 is returned. 
        /// 
        /// The TItem to locate in the ContentElementCollection. 
        public int IndexOf(TItem item) 
        {
            if (BelongsToOwner(item)) 
            {
                return item.Index;
            }
            else 
            {
                return (-1); 
            } 
        }
 
        /// 
        /// 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.
        ///  
        abstract public void Insert(int index, TItem item); 

        ///  
        /// 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. 
        /// 
        abstract public bool Remove(TItem item); 
 
        /// 
        /// 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.
        /// 
        abstract public void RemoveAt(int 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. 
        /// 
        abstract public void RemoveRange(int index, int count); 


        /// 
        /// Sets the capacity to the actual number of elements in the ContentElementCollection. 
        /// 
        ///  
        /// This method can be used to minimize a ContentElementCollection's memory overhead 
        /// if no new elements will be added to the collection.
        /// 
        /// To completely clear all elements in a ContentElementCollection, call the Clear method
        /// before calling TrimToSize.
        /// 
        public void TrimToSize() 
        {
            PrivateCapacity = Size; 
        } 

        #endregion Public Methods 

        //------------------------------------------------------
        //
        //  Private Types 
        //
        //------------------------------------------------------ 
 
        #region Protected Types
 
        protected class ContentElementCollectionEnumeratorSimple : IEnumerator, IEnumerator
        {
            internal ContentElementCollectionEnumeratorSimple(ContentElementCollection collection)
            { 
                Debug.Assert(collection != null);
 
                _collection = collection; 
                _index = -1;
                Version = _collection.Version; 
                _currentElement = collection;
            }

            public bool MoveNext() 
            {
                if (Version != _collection.Version) 
                { 
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged));
                } 

                if (_index < (_collection.Size - 1))
                {
                    _index++; 
                    _currentElement = _collection[_index];
                    return (true); 
                } 
                else
                { 
                    _currentElement = _collection;
                    _index = _collection.Size;
                    return (false);
                } 
            }
 
            public TItem Current 
            {
                get 
                {
                    if (_currentElement == _collection)
                    {
                        if (_index == -1) 
                        {
                            throw new InvalidOperationException(SR.Get(SRID.EnumeratorNotStarted)); 
                        } 
                        else
                        { 
                            throw new InvalidOperationException(SR.Get(SRID.EnumeratorReachedEnd));
                        }
                    }
                    return (TItem)_currentElement; 
                }
            } 
 
            /// 
            ///      
            /// 
            object IEnumerator.Current
            {
                get 
                {
                    return this.Current; 
                } 
            }
 

            public void Reset()
            {
                if (Version != _collection.Version) 
                {
                    throw new InvalidOperationException(SR.Get(SRID.EnumeratorVersionChanged)); 
                } 
                _currentElement = _collection;
                _index = -1; 
            }

            public void Dispose()
            { 
                GC.SuppressFinalize(this);
            } 
 

            private ContentElementCollection _collection; 
            private int _index;
            protected int Version;
            private object _currentElement;
        } 

        protected class DummyProxy : DependencyObject 
        { 
        }
 
        #endregion Protected Types

        //-------------------------------------------------------------------
        // 
        //  IList Members
        // 
        //-------------------------------------------------------------------- 
        #region IList Members
 
        int IList.Add(object value)
        {
            if (value == null)
            { 
                throw new ArgumentNullException("value");
            } 
 
            TItem item = value as TItem;
 
            // : This chunk is moved to the correcponding override in TableCollumnCollection, to keep same behavior
            //if (item == null)
            //{
            //    throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value"); 
            //}
 
            this.Add(item); 

            return ((IList)this).IndexOf(item); 
        }

        void IList.Clear()
        { 
            this.Clear();
        } 
 
        bool IList.Contains(object value)
        { 
            TItem item = value as TItem;

            if (item == null)
            { 
                return false;
            } 
 
            return this.Contains(item);
        } 

        int IList.IndexOf(object value)
        {
            TItem item = value as TItem; 

            if (item == null) 
            { 
                return -1;
            } 

            return this.IndexOf(item);
        }
 
        void IList.Insert(int index, object value)
        { 
            if (value == null) 
            {
                throw new ArgumentNullException("value"); 
            }

            TItem newItem = value as TItem;
 
            if (newItem == null)
            { 
                throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value"); 
            }
 
            this.Insert(index, newItem);
        }

        bool IList.IsFixedSize 
        {
            get 
            { 
                return false;
            } 
        }

        bool IList.IsReadOnly
        { 
            get
            { 
                return this.IsReadOnly; 
            }
        } 

        void IList.Remove(object value)
        {
            TItem item = value as TItem; 

            if (item == null) 
            { 
                return;
            } 

            this.Remove(item);
        }
 
        void IList.RemoveAt(int index)
        { 
            this.RemoveAt(index); 
        }
 
        object IList.this[int index]
        {
            get
            { 
                return this[index];
            } 
 
            set
            { 
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                } 

                TItem item = value as TItem; 
 
                if (item == null)
                { 
                    throw new ArgumentException(SR.Get(SRID.TableCollectionElementTypeExpected, typeof(TItem).Name), "value");
                }

                this[index] = item; 
            }
        } 
 
        #endregion IList Members
 

        //-----------------------------------------------------
        //
        //  Public Properties 
        //
        //----------------------------------------------------- 
 
        #region Public Properties
 
        public abstract TItem this[int index]
        {
            get;
            set; 
        }
 
        ///  
        /// 
        ///  
        public int Count
        {
            get
            { 
                return (Size);
            } 
        } 

        ///  
        ///     
        ///     
        /// 
        public bool IsReadOnly  //  bool IList.IsReadOnly {get;}; bool ICollection.IsReadOnly {get;} 
        {
            get 
            { 
                return false;
            } 
        }

        /// 
        ///  
        /// 
        /// Always returns false. 
        ///  
        /// 
        public bool IsSynchronized 
        {
            get
            {
                return (false); 
            }
        } 
 
        /// 
        ///  
        /// 
        public object SyncRoot
        {
            get 
            {
                // 
 
                return (this);
            } 
        }

        /// 
        /// Gets or sets the number of elements that the ContentElementCollection can contain. 
        /// 
        ///  
        /// The number of elements that the ContentElementCollection can contain. 
        /// 
        ///  
        /// Capacity is the number of elements that the ContentElementCollection is capable of storing.
        /// Count is the number of Visuals that are actually in the ContentElementCollection.
        ///
        /// Capacity is always greater than or equal to Count. If Count exceeds 
        /// Capacity while adding elements, the capacity of the ContentElementCollection is increased.
        /// 
        /// By default the capacity is 8. 
        /// 
        ///  
        /// Capacity is set to a value that is less than Count.
        /// 
        /// 
        public int Capacity 
        {
            get 
            { 
                return PrivateCapacity;
            } 
            set
            {
                PrivateCapacity = value;
            } 
        }
        public TParent Owner 
        { 
            get { return _owner; }
        } 

        #endregion Public Properties

 
        #region Protected Properties
 
        protected TItem[] Items 
        {
            get { return _items; } 
            private set { _items = value; }
        }

        protected int Size 
        {
            get { return _size; } 
            set { _size = value; } 
        }
 
        protected int Version
        {
            get { return _version; }
            set { _version = value; } 
        }
 
        protected int DefaultCapacity 
        {
            get { return c_defaultCapacity; } 
        }
        #endregion Protected Properties

        //----------------------------------------------------- 
        //
        //  Private Methods 
        // 
        //------------------------------------------------------
 
        #region Internal Methods


        ///  
        /// Ensures that the capacity of this list is at least the given minimum
        /// value. If the currect capacity of the list is less than min, the 
        /// capacity is increased to min. 
        /// 
        internal void EnsureCapacity(int min) 
        {
            if (PrivateCapacity < min)
            {
                PrivateCapacity = Math.Max(min, PrivateCapacity * 2); 
            }
        } 
 
        /// 
        /// 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. 
        /// 
        abstract internal void PrivateConnectChild(int index, TItem item);

 
        /// 
        /// 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.
        ///  
        abstract internal void PrivateDisconnectChild(TItem item);


        ///  
        /// Removes specified TItem from the ContentElementCollection.
        ///  
        /// TItem to remove. 
        internal void PrivateRemove(TItem item)
        { 
            Debug.Assert(BelongsToOwner(item) && Items[item.Index] == item);

            int index = item.Index;
 
            PrivateDisconnectChild(item);
 
            --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;
        } 

        // helper method: return true if the item belongs to the collection's owner
        internal bool BelongsToOwner(TItem item)
        { 
            if (item == null)
                return false; 
 
            DependencyObject node = item.Parent;
            if (node is DummyProxy) 
            {
                node = LogicalTreeHelper.GetParent(node);
            }
 
            return (node == Owner);
        } 
 
        #endregion Internal Methods
 
        //-----------------------------------------------------
        //
        //  Private Properties
        // 
        //------------------------------------------------------
 
        #region Internal Properties 

        ///  
        /// PrivateCapacity sets/gets the Capacity of the collection.
        /// 
        internal int PrivateCapacity
        { 
            get
            { 
                return (Items.Length); 
            }
            set 
            {
                if (value != Items.Length)
                {
                    if (value < Size) 
                    {
                        throw new ArgumentOutOfRangeException(SR.Get(SRID.TableCollectionNotEnoughCapacity)); 
                    } 

                    if (value > 0) 
                    {
                        TItem[] newItems = new TItem[value];
                        if (Size > 0)
                        { 
                            Array.Copy(Items, 0, newItems, 0, Size);
                        } 
                        Items = newItems; 
                    }
                    else 
                    {
                        Items = new TItem[DefaultCapacity];
                    }
                } 
            }
        } 
 
        #endregion Internal Properties
 
        //------------------------------------------------------
        //
        //  Protected Fields
        // 
        //-----------------------------------------------------
 
        #region Protected Fields 
        private readonly TParent _owner;      //  owner of the collection
        private TItem[] _items;              //  storage of items 
        private int _size;                          //  size of the collection
        private int _version;                       //  version tracks updates in the collection
        protected const int c_defaultCapacity = 8;    //  default capacity of the collection
        #endregion Protected Fields 

    } 
 
}

// 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