#region COPYRIGHT
//
//     THIS IS GENERATED BY TEMPLATE
//     
//     AUTHOR  :     ROYE
//     DATE       :     2010
//
//     COPYRIGHT (C) 2010, TIANXIAHOTEL TECHNOLOGIES CO., LTD. ALL RIGHTS RESERVED.
//
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Data;

namespace System.Windows.Forms
{
    public class ObjectSelection<T>
    {
        private ListSelection<T> _Container;

        public ObjectSelection(T item, ListSelection<T> container)
            : base()
        {
            _Container = container;
            Item = item;
        }

        /// <summary>
        /// 
        /// </summary>
        public int Count { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public T Item { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool Selected { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string NameConcatenated
        {
            get { return _Container.SelectedNames; }
        }

        public string Name
        {
            get
            {
                string name = null;
                if (string.IsNullOrEmpty(_Container.DisplayNameProperty))
                {
                    name = Item.ToString();
                }
                else if (Item is DataRow) // A specific implementation for DataRow
                {
                    name = ((DataRow)((Object)Item))[_Container.DisplayNameProperty].ToString();
                }
                else
                {
                    PropertyDescriptorCollection pds = TypeDescriptor.GetProperties(Item);
                    foreach (PropertyDescriptor pd in pds)
                    {
                        if (pd.Name.CompareTo(_Container.DisplayNameProperty) == 0)
                        {
                            name = (string)pd.GetValue(Item).ToString();
                            break;
                        }
                    }
                    if (string.IsNullOrEmpty(name))
                    {
                        name = Item.GetPropertyValue(_Container.DisplayNameProperty).ToString();
                        //PropertyInfo pi = Item.GetType().GetProperty(_Container.DisplayNameProperty);
                        //if (pi == null)
                        //{
                        //    throw new Exception(String.Format("Property {0} cannot be found on {1}.",
                        //        _Container.DisplayNameProperty,
                        //        Item.GetType()));
                        //}
                        //name = pi.GetValue(Item, null).ToString();
                    }
                }
                return _Container.ShowCounts ? String.Format("{0} [{1}]", name, Count) : name;
            }
        }
    }
}
