#if FRAMEWORK20
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using DevComponents.Editors;
using DevComponents.Editors.DateTimeAdv;
using System.Runtime.Serialization;
namespace DevComponents.DotNetBar.Validator
{
    /// 
    /// Represents base validator used by SuperValidator component.
    /// 
    [ToolboxItem(false), DesignTimeVisible(false)]
    public abstract class ValidatorBase : Component
    {
        #region Events
        /// 
        /// Occurs when validator retrieves the value for the control. It allows you to return value for the controls validator does not recognize.
        /// 
        public event ValidatorGetValueEventHandler GetValue;
        #endregion
        #region Implementation
        private string _ErrorMessage = "";
        /// 
        /// Gets or sets the error message that is displayed by error provider when validation fails.
        /// 
        [DefaultValue(""), Category("Appearance"), Description("Indicates error message that is displayed when validation fails."), Localizable(true)]
        public virtual string ErrorMessage
        {
            get { return _ErrorMessage; }
            set
            {
                _ErrorMessage = value;
            }
        }
        private bool _LastValidationResult = true;
        /// 
        /// Gets the last validation result returned from Validate call. True if validation was successful or false if validation failed.
        /// 
        [Browsable(false)]
        public bool LastValidationResult
        {
            get { return _LastValidationResult; }
            internal set
            {
            	_LastValidationResult = value;
            }
        }
        
        /// 
        /// Validates the input control.
        /// 
        /// Input control to validate.
        /// true if validation is successful otherwise false
        public abstract bool Validate(Control input);
        /// 
        /// Gets the input control value.
        /// 
        /// Control to return value for.
        /// Controls value
        protected virtual object GetControlValue(Control input)
        {
            return GetControlValue(input, _ValuePropertyName);
        }
        /// 
        /// Gets the input control value.
        /// 
        /// Control to return value for.
        /// Controls value
        protected virtual object GetControlValue(Control input, string valuePropertyName)
        {
            ValidatorGetValueEventArgs args = new ValidatorGetValueEventArgs(input, this);
            OnGetValue(args);
            if (args.ValueSet) return args.Value;
            if (_SuperValidator != null)
            {
                _SuperValidator.InvokeGetValue(args);
                if (args.ValueSet) return args.Value;
            }
            if (!string.IsNullOrEmpty(valuePropertyName))
            {
                return TypeDescriptor.GetProperties(input)[valuePropertyName].GetValue(input);
            }
            if (input is ComboBox)
            {
                ComboBox box = (ComboBox)input;
                return box.SelectedIndex;
            }
            else if (input is DevComponents.DotNetBar.Controls.ComboTree)
            {
                DevComponents.DotNetBar.Controls.ComboTree cbt = (DevComponents.DotNetBar.Controls.ComboTree)input;
                return cbt.SelectedIndex;
            }
            if (input is DoubleInput)
                return ((DoubleInput)input).ValueObject;
            else if(input is IntegerInput)
                return ((IntegerInput)input).ValueObject;
            else if (input is DateTimeInput)
                return ((DateTimeInput)input).ValueObject;
            object value = input.Text;
            string text = input.Text;
            long l = 0;
            int i = 0;
            double d = 0;
            if (long.TryParse(text, out l))
                value = l;
            else if (int.TryParse(text, out i))
                value = i;
            else if (double.TryParse(text, out d))
                value = d;
            return value;
        }
        private SuperValidator _SuperValidator = null;
        /// 
        /// Returns SuperValidator control validator is assigned to.
        /// 
        [Browsable(false)]
        public SuperValidator SuperValidator
        {
            get { return _SuperValidator; }
            internal set { _SuperValidator = value; }
        }
        /// 
        /// Raises the GetValue event.
        /// 
        /// Event arguments.
        protected virtual void OnGetValue(ValidatorGetValueEventArgs e)
        {
            ValidatorGetValueEventHandler handler = GetValue;
            if (handler != null) handler(this, e);
        }
        private bool _DisplayError = true;
        /// 
        /// Gets or sets whether error is displayed using the error provider on SuperValidator when validation fails. Default value is true.
        /// 
        [DefaultValue(true), Category("Behavior"), Description("Indicates whether error is displayed using the error provider on SuperValidator when validation fails.")]
        public bool DisplayError
        {
            get { return _DisplayError; }
            set
            {
                _DisplayError = value;
            }
        }
        private bool _Enabled = true;
        /// 
        /// Gets or sets whether validator is enabled. Default value is true.
        /// 
        [DefaultValue(true), Category("Behavior"), Description("Indicates whether validator is enabled.")]
        public bool Enabled
        {
            get { return _Enabled; }
            set
            {
                _Enabled = value;
            }
        }
        private string _OptionalValidationGroup = "";
        /// 
        /// Gets or sets the group name validation belongs to. When control belongs to optional validation group the validation is considered successful when any of the controls in the group validates.
        /// 
        [DefaultValue(""), Category("Behavior"), Description("Specifies group name validation belongs to. When control belongs to optional validation group the validation is considered successful when any of the controls in the group validates.")]
        public string OptionalValidationGroup
        {
            get { return _OptionalValidationGroup; }
            set
            {
                if (value == null) value = "";
                _OptionalValidationGroup = value;
            }
        }
        private eHighlightColor _HighlightColor = eHighlightColor.None;
        /// 
        /// Gets or sets the highlight color for control when validation fails if Highlighter component is used on SuperValidator. Default Value is None.
        /// 
        [DefaultValue(eHighlightColor.None), Category("Appearance"), Description("Indicates highlight color for control when validation fails if Highlighter component is used on SuperValidator.")]
        public eHighlightColor HighlightColor
        {
            get { return _HighlightColor; }
            set { _HighlightColor = value; }
        }
        private string _ValuePropertyName = "";
        /// 
        /// Gets or sets the value property name for the control to validate.
        /// 
        [DefaultValue(""), Category("Behavior"), Description("Indicates value property name for the control to validate.")]
        public string ValuePropertyName
        {
            get { return _ValuePropertyName; }
            set
            {
                _ValuePropertyName = value;
            }
        }
        
        #endregion
    }
    #region ValidatorGetValue
    public class ValidatorGetValueEventArgs : EventArgs
    {
        /// 
        /// Initializes a new instance of the ValidatorGetValueEventArgs class.
        /// 
        /// 
        /// 
        public ValidatorGetValueEventArgs(Control control, ValidatorBase validator)
        {
            Control = control;
            Validator = validator;
        }
        /// 
        /// Gets Control to retrieve value for.
        /// 
        public readonly Control Control;
        /// 
        /// Gets validator that is requesting value.
        /// 
        public readonly ValidatorBase Validator;
        private object _Value;
        /// 
        /// Gets or sets the value that will be used by validator.
        /// 
        public object Value
        {
            get { return _Value; }
            set
            {
                _Value = value;
                ValueSet = true;
            }
        }
        internal bool ValueSet = false;
        /// 
        /// Resets the Value set and indicates that validator will internally retrieve value for the control.
        /// 
        public void ResetValue()
        {
            _Value = null;
            ValueSet = false;
        }
    }
    public delegate void ValidatorGetValueEventHandler(object sender, ValidatorGetValueEventArgs ea);
    #endregion
}
#endif