using System;
using System.ComponentModel;
using JoeK.Resources;
namespace JoeK.Converters
{
public class YesNoConverter : BooleanConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
if (((string)value) == LocalisableStrings.Yes)
{
return true;
}
if (((string)value) == LocalisableStrings.No)
{
return false;
}
throw new Exception("Values must be \"Yes\" or \"No\"");
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return (((bool)value) ? LocalisableStrings.Yes : LocalisableStrings.No);
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
bool[] bools = new[] { true, false };
StandardValuesCollection svc = new StandardValuesCollection(bools);
return svc;
}
}
}