#if FRAMEWORK20
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using DevComponents.DotNetBar.Validator;
namespace DevComponents.DotNetBar.Design
{
    public class RequiredFieldValidatorConverter : ExpandableObjectConverter
    {
        /// 
        /// Creates new instance of the class.
        /// 
        public RequiredFieldValidatorConverter() { }
        /// 
        /// Checks whether conversion can be made to specified type.
        /// 
        /// Context Information.
        /// Destination type.
        /// 
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor))
                return true;
            return base.CanConvertTo(context, destinationType);
        }
        /// 
        /// Converts object to specified type.
        /// 
        /// Context information.
        /// Culture information.
        /// Object to convert.
        /// Destination type.
        /// Object converted to destination type.
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
                throw new ArgumentNullException("destinationType");
            if ((destinationType == typeof(InstanceDescriptor)) && (value is RequiredFieldValidator))
            {
                RequiredFieldValidator validator = (RequiredFieldValidator)value;
                Type[] constructorParams = null;
                MemberInfo constructorMemberInfo = null;
                object[] constructorValues = null;
                if (string.IsNullOrEmpty(validator.OptionalValidationGroup))
                {
                    constructorParams = new Type[] { typeof(string) };
                    constructorMemberInfo = typeof(RequiredFieldValidator).GetConstructor(constructorParams);
                    constructorValues = new object[] { validator.ErrorMessage};
                }
                else
                {
                    constructorParams = new Type[] { typeof(string), typeof(string) };
                    constructorMemberInfo = typeof(RequiredFieldValidator).GetConstructor(constructorParams);
                    constructorValues = new object[] { validator.ErrorMessage, validator.OptionalValidationGroup };
                }
                
                if (constructorMemberInfo != null)
                {
                    return new InstanceDescriptor(constructorMemberInfo, constructorValues);
                }
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}
#endif