
#region Comment

/*
 * Project£º    FineUI
 * 
 * FileName:    BoundField.cs
 * CreatedOn:   2008-05-27
 * CreatedBy:   30372245@qq.com
 * 
 * 
 * Description£º
 *      ->
 *   
 * History£º
 *      ->
 * 
 * 
 * 
 * 
 */

#endregion

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Globalization;
using System.Reflection;


namespace FineUI
{
    /// <summary>
    /// ±í¸ñÊý¾Ý°ó¶¨ÁÐ
    /// </summary>
    [ToolboxItem(false)]
    [ParseChildren(true)]
    [PersistChildren(false)]
    public class BoundField : BaseField
    {
        #region Properties

        private bool _enabled = true;

        /// <summary>
        /// ÊÇ·ñ¿ÉÓÃ
        /// </summary>
        [Category(CategoryName.OPTIONS)]
        [DefaultValue(true)]
        [Description("ÊÇ·ñ¿ÉÓÃ")]
        public override bool Enabled
        {
            get
            {
                return _enabled;
            }
            set
            {
                _enabled = value;
            }
        }


        private string _dataField = String.Empty;

        /// <summary>
        /// ×Ö¶ÎÃû³Æ
        /// </summary>
        [Category(CategoryName.OPTIONS)]
        [DefaultValue("")]
        [Description("×Ö¶ÎÃû³Æ")]
        public string DataField
        {
            get
            {
                return _dataField;
            }
            set
            {
                _dataField = value;
            }
        }


        private string _dataFormatString = String.Empty;

        /// <summary>
        /// ×Ö¶Î¸ñÊ½»¯×Ö·û´®
        /// </summary>
        [Category(CategoryName.OPTIONS)]
        [DefaultValue("")]
        [Description("×Ö¶Î¸ñÊ½»¯×Ö·û´®")]
        public string DataFormatString
        {
            get
            {
                return _dataFormatString;
            }
            set
            {
                _dataFormatString = value;
            }
        }


        private string _nullDisplayText = String.Empty;

        /// <summary>
        /// ´¦ÀíÊý¾Ý¿âÖÐnullÖµ£¬Ä¬ÈÏÎª¿Õ×Ö·û´®
        /// </summary>
        [Category(CategoryName.OPTIONS)]
        [DefaultValue("")]
        [Description("´¦ÀíÊý¾Ý¿âÖÐnullÖµ£¬Ä¬ÈÏÎª¿Õ×Ö·û´®")]
        public string NullDisplayText
        {
            get
            {
                return _nullDisplayText;
            }
            set
            {
                _nullDisplayText = value;
            }
        }


        private bool _htmlEncode = true;

        /// <summary>
        /// ÏÔÊ¾Ö®Ç°½øÐÐHTML±àÂë£¨Ä¬ÈÏÎªtrue£©
        /// </summary>
        [Category(CategoryName.OPTIONS)]
        [DefaultValue(true)]
        [Description("ÏÔÊ¾Ö®Ç°½øÐÐHTML±àÂë£¨Ä¬ÈÏÎªtrue£©")]
        public bool HtmlEncode
        {
            get
            {
                return _htmlEncode;
            }
            set
            {
                _htmlEncode = value;
            }
        }


        //private bool _htmlEncodeFormatString = true;

        ///// <summary>
        ///// ÊÇ·ñÔÚÓ¦ÓÃDataFormatStringÊôÐÔÖ®ºó½øÐÐHTML±àÂë£¨Ä¬ÈÏÎªtrue£©
        ///// </summary>
        //[Category(CategoryName.OPTIONS)]
        //[DefaultValue(true)]
        //[Description("ÊÇ·ñÔÚÓ¦ÓÃDataFormatStringÊôÐÔÖ®ºó½øÐÐHTML±àÂë£¨Ä¬ÈÏÎªtrue£©")]
        [Obsolete("´ËÊôÐÔÒÑ·Ï³ý£¬ÇëÊ¹ÓÃHtmlEncodeÊôÐÔ")]
        public bool HtmlEncodeFormatString
        {
            get
            {
                return _htmlEncode;
            }
            set
            {
                _htmlEncode = value;
            }
        }

        #endregion

        #region Methods

        internal override object GetColumnValue(GridRow row)
        {
            string text = String.Empty;

            if (!String.IsNullOrEmpty(DataField))
            {
                object value = row.GetPropertyValue(DataField);

                if (value == null || value == DBNull.Value || (value is String && String.IsNullOrEmpty(value.ToString())))
                {
                    text = NullDisplayText;
                }
                else
                {
                    if (!String.IsNullOrEmpty(DataFormatString))
                    {
                        text = String.Format(DataFormatString, value);
                    }
                    else
                    {
                        text = value.ToString();
                    }

                    if (HtmlEncode)
                    {
                        text = HttpUtility.HtmlEncode(text);
                    }
                }
            }

            HtmlNodeBuilder nb = new HtmlNodeBuilder("span");

            nb.InnerProperty = text;

            if (!Enabled)
            {
                nb.SetProperty("class", "x-item-disabled");
                nb.SetProperty("disabled", "disabled");
            }


            string result = nb.ToString();

            string tooltip = GetTooltipString(row);
            if (!String.IsNullOrEmpty(tooltip))
            {
                result = result.ToString().Insert("<span".Length, tooltip);
            }

            // Èç¹û½á¹ûÊÇ <span>°ó¶¨µÄÊý¾Ý</span>
            if (result.StartsWith("<span>"))
            {
                result = result.Substring("<span>".Length, result.Length - "<span></span>".Length);
            }

            return result;
        }

        //public override string GetFieldType()
        //{
        //    return "string";
        //}

        #endregion

        #region OnFirstPreRender

        /// <summary>
        /// äÖÈ¾ HTML Ö®Ç°µ÷ÓÃ£¨Ò³ÃæµÚÒ»´Î¼ÓÔØ»òÕßÆÕÍ¨»Ø·¢£©
        /// </summary>
        protected override void OnFirstPreRender()
        {
            base.OnFirstPreRender();


            string jsContent = String.Format("var {0}={1};", XID, OB.ToString());
            AddGridColumnScript(jsContent);
            
        }

        #endregion
    }
}



