EnumLocalizer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections;
  3. namespace PaintDotNet
  4. {
  5. /// <summary>
  6. /// Provides convenience methods for converting between enumeration values
  7. /// and their localized value names. This class only works with strings
  8. /// loaded via the PdnResources class.
  9. /// </summary>
  10. public sealed class EnumLocalizer
  11. {
  12. private Type enumType;
  13. private Hashtable valueToName;
  14. private Hashtable nameToValue;
  15. private static Hashtable typeToWrapper;
  16. public Type EnumType
  17. {
  18. get
  19. {
  20. return this.enumType;
  21. }
  22. }
  23. public object[] GetEnumValues()
  24. {
  25. ICollection valueNames = this.valueToName.Keys;
  26. object[] values = new object[valueNames.Count];
  27. int index = 0;
  28. foreach (string valueName in valueNames)
  29. {
  30. values[index] = Enum.Parse(this.enumType, valueName);
  31. ++index;
  32. }
  33. return values;
  34. }
  35. public string[] GetLocalizedNames()
  36. {
  37. ICollection keysCollection = this.nameToValue.Keys;
  38. string[] keysArray = new string[keysCollection.Count];
  39. int index = 0;
  40. foreach (string key in keysCollection)
  41. {
  42. keysArray[index] = key;
  43. ++index;
  44. }
  45. return keysArray;
  46. }
  47. public static string EnumValueToLocalizedName(Type enumType, object enumValue)
  48. {
  49. EnumLocalizer wrapper = EnumLocalizer.Create(enumType);
  50. return wrapper.EnumValueToLocalizedName(enumValue);
  51. }
  52. public string EnumValueToLocalizedName(object enumValue)
  53. {
  54. object retValue = this.valueToName[enumValue.ToString()];
  55. if (retValue == null)
  56. {
  57. this.valueToName.Remove(enumValue);
  58. return null;
  59. }
  60. else
  61. {
  62. return (string)retValue;
  63. }
  64. }
  65. public object LocalizedNameToEnumValue(string locName)
  66. {
  67. object enumValueName = this.nameToValue[locName];
  68. if (enumValueName == null)
  69. {
  70. this.nameToValue.Remove(locName);
  71. return null;
  72. }
  73. else
  74. {
  75. object enumValue = Enum.Parse(this.enumType, (string)enumValueName);
  76. return enumValue;
  77. }
  78. }
  79. public string LocalizedNameToEnumValueName(string locName)
  80. {
  81. object enumValueName = this.nameToValue[locName];
  82. if (enumValueName == null)
  83. {
  84. this.nameToValue.Remove(locName);
  85. return null;
  86. }
  87. else
  88. {
  89. return (string)enumValueName;
  90. }
  91. }
  92. public static EnumLocalizer Create(Type enumType)
  93. {
  94. if (typeToWrapper == null)
  95. {
  96. typeToWrapper = new Hashtable();
  97. }
  98. object wrapper = typeToWrapper[enumType];
  99. if (wrapper == null)
  100. {
  101. wrapper = new EnumLocalizer(enumType);
  102. typeToWrapper[enumType] = wrapper;
  103. }
  104. return (EnumLocalizer)wrapper;
  105. }
  106. private EnumLocalizer(Type enumType)
  107. {
  108. this.enumType = enumType;
  109. this.valueToName = new Hashtable();
  110. this.nameToValue = new Hashtable();
  111. foreach (string enumValueName in Enum.GetNames(this.enumType))
  112. {
  113. string resourceName = this.enumType.Name + "." + enumValueName;
  114. string localizedName = PdnResources.GetString(resourceName);
  115. this.valueToName.Add(enumValueName, localizedName);
  116. this.nameToValue.Add(localizedName, enumValueName);
  117. }
  118. }
  119. }
  120. }