ElementPositionConverter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // Purpose: Converter of the element position.
  6. //
  7. using System;
  8. using System.ComponentModel;
  9. using System.Globalization;
  10. namespace FastReport.DataVisualization.Charting
  11. {
  12. /// <summary>
  13. /// Element position converter.
  14. /// </summary>
  15. internal class ElementPositionConverter : ExpandableObjectConverter
  16. {
  17. #region Converter methods
  18. /// <summary>
  19. /// Overrides the CanConvertFrom method of TypeConverter.
  20. /// </summary>
  21. /// <param name="context">Descriptor context.</param>
  22. /// <param name="sourceType">Convertion source type.</param>
  23. /// <returns>Indicates if convertion is possible.</returns>
  24. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  25. {
  26. if(sourceType == typeof(string))
  27. {
  28. return true;
  29. }
  30. return base.CanConvertFrom(context, sourceType);
  31. }
  32. /// <summary>
  33. /// Overrides the CanConvertTo method of TypeConverter.
  34. /// </summary>
  35. /// <param name="context">Descriptor context.</param>
  36. /// <param name="destinationType">Destination type.</param>
  37. /// <returns>Indicates if convertion is possible.</returns>
  38. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  39. {
  40. if (destinationType == typeof(string))
  41. {
  42. return true;
  43. }
  44. return base.CanConvertTo(context, destinationType);
  45. }
  46. /// <summary>
  47. /// Overrides the ConvertTo method of TypeConverter.
  48. /// </summary>
  49. /// <param name="context">Descriptor context.</param>
  50. /// <param name="culture">Culture information.</param>
  51. /// <param name="value">Value to convert.</param>
  52. /// <param name="destinationType">Convertion destination type.</param>
  53. /// <returns>Converted object.</returns>
  54. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  55. {
  56. if (destinationType == typeof(string))
  57. {
  58. return ((ElementPosition)value).ToString();
  59. }
  60. return base.ConvertTo(context, culture, value, destinationType);
  61. }
  62. /// <summary>
  63. /// Overrides the ConvertFrom method of TypeConverter.
  64. /// Converts from string with comma separated values.
  65. /// </summary>
  66. /// <param name="context">Descriptor context.</param>
  67. /// <param name="culture">Culture information.</param>
  68. /// <param name="value">Value to convert from.</param>
  69. /// <returns>Indicates if convertion is possible.</returns>
  70. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  71. {
  72. string posValue = value as string;
  73. if(posValue != null)
  74. {
  75. if (String.Compare(posValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
  76. {
  77. return new ElementPosition();
  78. }
  79. else
  80. {
  81. string[] array = posValue.Split(',');
  82. if(array.Length == 4)
  83. {
  84. return new ElementPosition(
  85. float.Parse(array[0], System.Globalization.CultureInfo.CurrentCulture),
  86. float.Parse(array[1], System.Globalization.CultureInfo.CurrentCulture),
  87. float.Parse(array[2], System.Globalization.CultureInfo.CurrentCulture),
  88. float.Parse(array[3], System.Globalization.CultureInfo.CurrentCulture));
  89. }
  90. else
  91. {
  92. throw(new ArgumentException( SR.ExceptionElementPositionConverter ));
  93. }
  94. }
  95. }
  96. return base.ConvertFrom(context, culture, value);
  97. }
  98. #endregion
  99. }
  100. }