// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. // // Purpose: Converter of the element position. // using System; using System.ComponentModel; using System.Globalization; namespace FastReport.DataVisualization.Charting { /// /// Element position converter. /// internal class ElementPositionConverter : ExpandableObjectConverter { #region Converter methods /// /// Overrides the CanConvertFrom method of TypeConverter. /// /// Descriptor context. /// Convertion source type. /// Indicates if convertion is possible. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if(sourceType == typeof(string)) { return true; } return base.CanConvertFrom(context, sourceType); } /// /// Overrides the CanConvertTo method of TypeConverter. /// /// Descriptor context. /// Destination type. /// Indicates if convertion is possible. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) { return true; } return base.CanConvertTo(context, destinationType); } /// /// Overrides the ConvertTo method of TypeConverter. /// /// Descriptor context. /// Culture information. /// Value to convert. /// Convertion destination type. /// Converted object. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return ((ElementPosition)value).ToString(); } return base.ConvertTo(context, culture, value, destinationType); } /// /// Overrides the ConvertFrom method of TypeConverter. /// Converts from string with comma separated values. /// /// Descriptor context. /// Culture information. /// Value to convert from. /// Indicates if convertion is possible. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string posValue = value as string; if(posValue != null) { if (String.Compare(posValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0) { return new ElementPosition(); } else { string[] array = posValue.Split(','); if(array.Length == 4) { return new ElementPosition( float.Parse(array[0], System.Globalization.CultureInfo.CurrentCulture), float.Parse(array[1], System.Globalization.CultureInfo.CurrentCulture), float.Parse(array[2], System.Globalization.CultureInfo.CurrentCulture), float.Parse(array[3], System.Globalization.CultureInfo.CurrentCulture)); } else { throw(new ArgumentException( SR.ExceptionElementPositionConverter )); } } } return base.ConvertFrom(context, culture, value); } #endregion } }