FontUnitConverter.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: FontUnitConverter
  4. *
  5. * Author: Gaurav Vaish, Gonzalo Paniagua Javier
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 95%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. * (c) 2002 Ximian, Inc. (http://www.ximian.com)
  13. */
  14. using System;
  15. using System.Globalization;
  16. using System.ComponentModel;
  17. using System.Web;
  18. using System.Web.UI;
  19. namespace System.Web.UI.WebControls
  20. {
  21. public class FontUnitConverter : TypeConverter
  22. {
  23. static StandardValuesCollection valuesCollection;
  24. static string creatingValues = "creating value collection";
  25. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  26. {
  27. if(sourceType == typeof(string))
  28. return true;
  29. return base.CanConvertFrom(context, sourceType);
  30. }
  31. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  32. {
  33. if(value == null)
  34. return null;
  35. if(value is string)
  36. {
  37. string val = ((string)value).Trim();
  38. if(val.Length == 0)
  39. {
  40. return FontUnit.Empty;
  41. }
  42. return FontUnit.Parse(val, culture);
  43. }
  44. return base.ConvertFrom(context, culture, value);
  45. }
  46. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  47. {
  48. if(value != null && value is FontUnit)
  49. {
  50. FontUnit val = (FontUnit)value;
  51. if(val.Type == FontSize.NotSet)
  52. {
  53. return String.Empty;
  54. }
  55. return val.ToString(culture);
  56. }
  57. return base.ConvertTo(context, culture, value, destinationType);
  58. }
  59. public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  60. {
  61. if (valuesCollection != null)
  62. return valuesCollection;
  63. lock (creatingValues) {
  64. if (valuesCollection != null)
  65. return valuesCollection;
  66. Array values = Enum.GetValues (typeof (FontUnit));
  67. Array.Sort (values);
  68. valuesCollection = new StandardValuesCollection (values);
  69. }
  70. return valuesCollection;
  71. }
  72. public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  73. {
  74. return false;
  75. }
  76. public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  77. {
  78. return true;
  79. }
  80. }
  81. }