#if FRAMEWORK20
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
namespace DevComponents.DotNetBar.Validator
{
    /// 
    /// Describes required field validator used with SuperValidator control.
    /// 
    [TypeConverter("DevComponents.DotNetBar.Design.RequiredFieldValidatorConverter, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=90f470f34c89ccaf"), DesignTimeVisible(false), ToolboxItem(false), Localizable(true)]
    public class RequiredFieldValidator : ValidatorBase
    {
        #region Events
        /// 
        /// Occurs when controls value needs to be evaluated to check whether it is empty. You can use this event to perform custom evaluation.
        /// 
        public event EvaluateIsEmptyEventHandler EvaluateIsEmpty;
        #endregion
        #region Constructors
        /// 
        /// Initializes a new instance of the RequiredFieldValidator class.
        /// 
        public RequiredFieldValidator()
        {
        }
        /// 
        /// Initializes a new instance of the RequiredFieldValidator class.
        /// 
        public RequiredFieldValidator(string errorMessage)
        {
            this.ErrorMessage = errorMessage;
        }
        /// 
        /// Initializes a new instance of the RequiredFieldValidator class.
        /// 
        public RequiredFieldValidator(string errorMessage, string optionalValidationGroup)
        {
            this.ErrorMessage = errorMessage;
            this.OptionalValidationGroup = optionalValidationGroup;
        }
        #endregion
        #region Implementation
        public override bool Validate(System.Windows.Forms.Control input)
        {
            object value = GetControlValue(input);
            this.LastValidationResult = !IsEmpty(input, value);
            return LastValidationResult;
        }
        protected virtual bool IsEmpty(System.Windows.Forms.Control input, object value)
        {
            EvaluateIsEmptyEventArgs args = new EvaluateIsEmptyEventArgs(input, value, this);
            OnEvaluateIsEmpty(args);
            if (args.IsEmptySet) return args.IsEmpty;
            if (value == null) return true;
            if (value is string)
            {
                if (_IsEmptyStringValid)
                    return value == null;
                else
                    return string.IsNullOrEmpty((string)value);
            }
            else if ((input is ComboBox || input is DevComponents.DotNetBar.Controls.ComboTree) && value is int)
                return ((int)value) < 0;
            return false;
        }
        /// 
        /// Raises EvaluateIsEmpty event.
        /// 
        /// Event Arguments
        protected virtual void OnEvaluateIsEmpty(EvaluateIsEmptyEventArgs args)
        {
            EvaluateIsEmptyEventHandler h = EvaluateIsEmpty;
            if (h != null) h(this, args);
        }
        private bool _IsEmptyStringValid = false;
        /// 
        /// Indicates whether empty string of zero length is considered valid input.
        /// 
        [DefaultValue(false), Category("Behavior"), Description("Indicates whether empty string of zero length is considered valid input.")]
        public bool IsEmptyStringValid
        {
            get { return _IsEmptyStringValid; }
            set
            {
                _IsEmptyStringValid = value;
            }
        }
        
        #endregion
    }
    #region EvaluateIsEmptyEventArgs
    public class EvaluateIsEmptyEventArgs : EventArgs
    {
        /// 
        /// Initializes a new instance of the ValidatorGetValueEventArgs class.
        /// 
        /// 
        /// 
        public EvaluateIsEmptyEventArgs(Control control, object value, ValidatorBase validator)
        {
            Control = control;
            Validator = validator;
            Value = value;
        }
        /// 
        /// Gets Control to retrieve value for.
        /// 
        public readonly object Control;
        /// 
        /// Gets the Value to evaluate.
        /// 
        public readonly object Value;
        /// 
        /// Gets validator that is requesting value.
        /// 
        public readonly ValidatorBase Validator;
        private bool _IsEmpty;
        /// 
        /// Gets or sets the value that will be used by validator.
        /// 
        public bool IsEmpty
        {
            get { return _IsEmpty; }
            set
            {
                _IsEmpty = value;
                IsEmptySet = true;
            }
        }
        internal bool IsEmptySet = false;
        /// 
        /// Resets the Value set and indicates that validator will internally retrieve value for the control.
        /// 
        public void ResetIsEmpty()
        {
            _IsEmpty = false;
            IsEmptySet = false;
        }
    }
    public delegate void EvaluateIsEmptyEventHandler(object sender, EvaluateIsEmptyEventArgs ea);
    #endregion
}
#endif