Advertisement
Guest User

Will Pittenger

a guest
May 20th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. namespace Org.WillPittenger.AdBlockPlusIniParserWPF.Core.Ctrl.ForEnums
  2. {
  3. using Ext;
  4.  
  5. public class RadioBtnGroup : System.Windows.Controls.StackPanel, System.ComponentModel.INotifyPropertyChanged
  6. {
  7. #region Constructors & Destructors
  8. public RadioBtnGroup()
  9. {
  10. }
  11. #endregion
  12.  
  13. #region Delegates
  14. #endregion
  15.  
  16. #region Events
  17. public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
  18. #endregion
  19.  
  20. #region Constants
  21. public static class PropNames
  22. {
  23. public const string strGroupName = "GroupName";
  24.  
  25. public const string strTypeOfEnum = "TypeOfEnum";
  26. public const string strValName = "ValName";
  27. }
  28. #endregion
  29.  
  30. #region Helper Types
  31. #endregion
  32.  
  33. #region Members
  34. private static System.Collections.Generic.SortedList<System.Type, Sets.HashSet<string>> allKnownEnumTypes = new System.Collections
  35. .Generic.SortedList<System.Type, Sets.HashSet<string>>();
  36.  
  37. public readonly string strGroupName = System.Guid.NewGuid().ToString();
  38.  
  39. public System.Type typeOfEnum = null;
  40. public string strVal = null;
  41.  
  42. #region Dependency Properties
  43. private static readonly System.Windows.DependencyProperty propGroupName = System.Windows.DependencyProperty.Register(PropNames.strGroupName,
  44. typeof(string), typeof(RadioBtnGroup));
  45.  
  46. private static readonly System.Windows.DependencyProperty propTypeOfEnum = System.Windows.DependencyProperty.Register(PropNames.strTypeOfEnum,
  47. typeof(string), typeof(RadioBtnGroup));
  48. private static readonly System.Windows.DependencyProperty propValName = System.Windows.DependencyProperty.Register(PropNames.strValName, typeof(string),
  49. typeof(RadioBtnGroup));
  50. #endregion
  51. #endregion
  52.  
  53. #region Properties
  54. #region Dependency Properties
  55. public static System.Windows.DependencyProperty GroupNameProperty => propGroupName;
  56.  
  57. public static System.Windows.DependencyProperty TypeOfEnumProperty => propTypeOfEnum;
  58. public static System.Windows.DependencyProperty ValNameProperty = propValName;
  59. #endregion
  60.  
  61. public string GroupName => strGroupName;
  62.  
  63. [System.ComponentModel.Category("Common")]
  64. [System.ComponentModel.DisplayName("Type of Enum")]
  65. public string TypeOfEnum
  66. {
  67. get => typeOfEnum.ToString();
  68.  
  69. set
  70. {
  71. if(value.IsEmpty())
  72. {
  73. typeOfEnum = null;
  74.  
  75. return;
  76. }
  77.  
  78. System.Type typeRequested = System.Type.GetType(value + ", Org.WillPittenger.AdBlockPlusIniParserWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
  79.  
  80. if(typeRequested == null)
  81. {
  82. typeOfEnum = null;
  83.  
  84. throw new System.Exception("Unable to find requested type.");
  85. }
  86.  
  87. if(!typeRequested.IsEnum)
  88. {
  89. typeOfEnum = null;
  90.  
  91. throw new System.Exception("Requested type isn't an enum.");
  92. }
  93.  
  94. typeOfEnum = typeRequested;
  95.  
  96. if(!allKnownEnumTypes.ContainsKey(typeOfEnum))
  97. allKnownEnumTypes[typeOfEnum] = new Sets.HashSet<string>(System.Enum.GetNames(typeOfEnum));
  98. }
  99. }
  100.  
  101. [System.ComponentModel.Category("Common")]
  102. [System.ComponentModel.DisplayName("Current Selected Value")]
  103. public string ValName
  104. {
  105. get => strVal;
  106.  
  107. set
  108. {
  109. if(value == null)
  110. strVal = null;
  111. else
  112. {
  113. if(typeOfEnum == null)
  114. {
  115. strVal = null;
  116.  
  117. throw new System.Exception("Can't set values until a source type is specified.");
  118. }
  119.  
  120. if(!IsValForEnumTypeValid(value))
  121. {
  122. strVal = null;
  123.  
  124. throw new System.Exception("Specified value isn't part of the specified type");
  125. }
  126.  
  127. strVal = value;
  128. }
  129.  
  130. FirePropChanged(PropNames.strValName);
  131. }
  132. }
  133. #endregion
  134.  
  135. #region Methods
  136. private void FirePropChanged(string strPropName) => PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(strPropName));
  137.  
  138. public EnumType GetValAsEnum<EnumType>()
  139. {
  140. if(strVal == null)
  141. throw new System.Exception("Value of this radio button group is null");
  142.  
  143. if(typeof(EnumType) != typeOfEnum)
  144. throw new System.Exception("Invalid type cast.");
  145.  
  146. return (EnumType)System.Enum.Parse(typeof(EnumType), strVal);
  147. }
  148.  
  149. public void SetValFromEnum<EnumType>(EnumType valNew)
  150. {
  151. if(typeof(EnumType) != typeOfEnum)
  152. throw new System.Exception("Invalid value in the specified enum type or this value is from the wrong enum type for this radio button group.");
  153.  
  154. strVal = valNew.ToString();
  155. }
  156.  
  157. internal void RegisterBtn(RadioBtn radioRegisterThis)
  158. {
  159. System.Diagnostics.Debug.Assert(typeOfEnum != null);
  160. if(typeOfEnum == null)
  161. throw new System.Exception("TypeOfNum is still nulll. Set it before adding radio buttons");
  162.  
  163. radioRegisterThis.Checked += OnChildRadioBtnNowChecked;
  164. radioRegisterThis.GroupName = strGroupName;
  165. }
  166.  
  167. internal bool IsValForEnumTypeValid(string strValToTest) => !(allKnownEnumTypes[typeOfEnum] < strValToTest);
  168. #endregion
  169.  
  170. #region Event Handlers
  171. private void OnChildRadioBtnNowChecked(object objSender, System.Windows.RoutedEventArgs e)
  172. {
  173. System.Diagnostics.Debug.Assert(objSender is RadioBtn);
  174.  
  175. RadioBtn radioSender = (RadioBtn)objSender;
  176.  
  177. System.Diagnostics.Debug.Assert(radioSender.Val > allKnownEnumTypes[typeOfEnum]);
  178.  
  179. strVal = radioSender.Val;
  180. }
  181. #endregion
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement