Click here to Skip to main content
11,581,353 members (61,572 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
Add your own
alternative version

SmartLists - Extended Lists with Events

, 29 Sep 2010 BSD 8.4K 93 10
SmartLists library is a collection of classes that extends the standard C# List object
//*****************************************************************************/
// Copyright (c) 2010 Luigi Grilli
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace Odbms.SmartLists
{
    /// <summary>
    /// An extended NamedList for objects that have a parent object and also a Name property
    /// When objects are added to the list the parent is automatically set
    /// and set to null when they are removed
    /// </summary>
    /// <typeparam name="T">Type of the items contained in the list</typeparam>
    /// <typeparam name="P">Type of the parent of the list</typeparam>
    public class ParentedNamedList<T, P> : NamedList<T>
                            where T : class, IParented<P>, INamed
                            where P : class
    {
        /// <summary>
        /// Event generated when the Parent property is changed
        /// </summary>
        public event EventHandler ParentChanged;

        private P _parent = null;
        private ParentedNamedList<T, P> _readOnlyParentedNamedList = null;

        #region Constructors
        /// <summary>
        /// Creates a new ParentedNamedList
        /// </summary>
        public ParentedNamedList()
        {
        }

        /// <summary>
        /// Creates a new ParentedNamedList
        /// </summary>
        public ParentedNamedList(P parent)
        {
            this.Parent = parent;
        }

        /// <summary>
        /// Creates a new readonly ParentedNamedList referencing the specified list
        /// </summary>
        /// <param name="list">Base list</param>
        protected ParentedNamedList(ParentedNamedList<T, P> list, bool readOnly)
            : base(list, readOnly)
        {
        }
        #endregion

        #region Public Members
        /// <summary>
        /// Get or set the parent of the list members
        /// </summary>
        public P Parent
        {
            get
            {
                if (this.IsReadOnly)
                    return ((ParentedNamedList<T, P>)this.InternalList).Parent;

                return _parent;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("Parent can't be set to null");

                if (this.IsReadOnly)
                    throw new ReadOnlyListException();

                if (this.Parent == value)
                    return;

                _parent = value;
                this.OnParentChanged();
            }
        }

        /// <summary>
        /// Creates a readonly reference to this list
        /// </summary>
        /// <returns></returns>
        public ParentedNamedList<T, P> AsReadOnlyParentedNamedList()
        {
            if (this.IsReadOnly)
                return this;

            if (_readOnlyParentedNamedList == null)
                _readOnlyParentedNamedList = new ParentedNamedList<T, P>(this, true);

            return _readOnlyParentedNamedList;
        }
        #endregion

        #region Overrides and Protected Members
        /// <summary>
        /// Called after adding a new item to the list
        /// This sets the item parent
        /// </summary>
        /// <param name="item">Item added</param>
        protected override void OnAfterAdd(T item)
        {
            item.Parent = this.Parent;
            base.OnAfterAdd(item);
        }

        /// <summary>
        /// Called after removing an item from the list
        /// This sets to null the item parent
        /// </summary>
        /// <param name="item">Item removed</param>
        protected override void OnAfterRemove(T item)
        {
            item.Parent = null;
            base.OnAfterRemove(item);
        }

        /// <summary>
        /// Called after a new item has been inserted on the list
        /// This sets the item parent
        /// </summary>
        /// <param name="index">Index where the item has been inserted</param>
        /// <param name="item">Item inserted</param>
        protected override void OnAfterInsert(int index, T item)
        {
            item.Parent = this.Parent;
            base.OnAfterInsert(index, item);
        }

        /// <summary>
        /// Called after all items from the list have been removed
        /// All removed items parent are set to null
        /// </summary>
        /// <param name="items">Items removed from the list</param>
        protected override void OnAfterClear(T[] items)
        {
            foreach (T item in items)
                item.Parent = null;

            base.OnAfterClear(items);
        }

        /// <summary>
        /// Sets the Parent property of all the items in the list
        /// and fires then ParentChanged event
        /// </summary>
        protected virtual void OnParentChanged()
        {
            foreach (T item in this)
                item.Parent = this.Parent;

            if (this.ParentChanged != null)
                this.ParentChanged(this, EventArgs.Empty);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Luigi Grilli
Software Developer (Senior) Scee
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 30 Sep 2010
Article Copyright 2010 by Luigi Grilli
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid