Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Org.WillPittenger.AdBlockPlusIniParserWPF.Core.Ctrl.ForEnums
- {
- using Ext;
- public class RadioBtnGroup : System.Windows.Controls.StackPanel, System.ComponentModel.INotifyPropertyChanged
- {
- #region Constructors & Destructors
- public RadioBtnGroup()
- {
- }
- #endregion
- #region Delegates
- #endregion
- #region Events
- public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
- #endregion
- #region Constants
- public static class PropNames
- {
- public const string strGroupName = "GroupName";
- public const string strTypeOfEnum = "TypeOfEnum";
- public const string strValName = "ValName";
- }
- #endregion
- #region Helper Types
- #endregion
- #region Members
- private static System.Collections.Generic.SortedList<System.Type, Sets.HashSet<string>> allKnownEnumTypes = new System.Collections
- .Generic.SortedList<System.Type, Sets.HashSet<string>>();
- public readonly string strGroupName = System.Guid.NewGuid().ToString();
- public System.Type typeOfEnum = null;
- public string strVal = null;
- #region Dependency Properties
- private static readonly System.Windows.DependencyProperty propGroupName = System.Windows.DependencyProperty.Register(PropNames.strGroupName,
- typeof(string), typeof(RadioBtnGroup));
- private static readonly System.Windows.DependencyProperty propTypeOfEnum = System.Windows.DependencyProperty.Register(PropNames.strTypeOfEnum,
- typeof(string), typeof(RadioBtnGroup));
- private static readonly System.Windows.DependencyProperty propValName = System.Windows.DependencyProperty.Register(PropNames.strValName, typeof(string),
- typeof(RadioBtnGroup));
- #endregion
- #endregion
- #region Properties
- #region Dependency Properties
- public static System.Windows.DependencyProperty GroupNameProperty => propGroupName;
- public static System.Windows.DependencyProperty TypeOfEnumProperty => propTypeOfEnum;
- public static System.Windows.DependencyProperty ValNameProperty = propValName;
- #endregion
- public string GroupName => strGroupName;
- [System.ComponentModel.Category("Common")]
- [System.ComponentModel.DisplayName("Type of Enum")]
- public string TypeOfEnum
- {
- get => typeOfEnum.ToString();
- set
- {
- if(value.IsEmpty())
- {
- typeOfEnum = null;
- return;
- }
- System.Type typeRequested = System.Type.GetType(value + ", Org.WillPittenger.AdBlockPlusIniParserWPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
- if(typeRequested == null)
- {
- typeOfEnum = null;
- throw new System.Exception("Unable to find requested type.");
- }
- if(!typeRequested.IsEnum)
- {
- typeOfEnum = null;
- throw new System.Exception("Requested type isn't an enum.");
- }
- typeOfEnum = typeRequested;
- if(!allKnownEnumTypes.ContainsKey(typeOfEnum))
- allKnownEnumTypes[typeOfEnum] = new Sets.HashSet<string>(System.Enum.GetNames(typeOfEnum));
- }
- }
- [System.ComponentModel.Category("Common")]
- [System.ComponentModel.DisplayName("Current Selected Value")]
- public string ValName
- {
- get => strVal;
- set
- {
- if(value == null)
- strVal = null;
- else
- {
- if(typeOfEnum == null)
- {
- strVal = null;
- throw new System.Exception("Can't set values until a source type is specified.");
- }
- if(!IsValForEnumTypeValid(value))
- {
- strVal = null;
- throw new System.Exception("Specified value isn't part of the specified type");
- }
- strVal = value;
- }
- FirePropChanged(PropNames.strValName);
- }
- }
- #endregion
- #region Methods
- private void FirePropChanged(string strPropName) => PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(strPropName));
- public EnumType GetValAsEnum<EnumType>()
- {
- if(strVal == null)
- throw new System.Exception("Value of this radio button group is null");
- if(typeof(EnumType) != typeOfEnum)
- throw new System.Exception("Invalid type cast.");
- return (EnumType)System.Enum.Parse(typeof(EnumType), strVal);
- }
- public void SetValFromEnum<EnumType>(EnumType valNew)
- {
- if(typeof(EnumType) != typeOfEnum)
- 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.");
- strVal = valNew.ToString();
- }
- internal void RegisterBtn(RadioBtn radioRegisterThis)
- {
- System.Diagnostics.Debug.Assert(typeOfEnum != null);
- if(typeOfEnum == null)
- throw new System.Exception("TypeOfNum is still nulll. Set it before adding radio buttons");
- radioRegisterThis.Checked += OnChildRadioBtnNowChecked;
- radioRegisterThis.GroupName = strGroupName;
- }
- internal bool IsValForEnumTypeValid(string strValToTest) => !(allKnownEnumTypes[typeOfEnum] < strValToTest);
- #endregion
- #region Event Handlers
- private void OnChildRadioBtnNowChecked(object objSender, System.Windows.RoutedEventArgs e)
- {
- System.Diagnostics.Debug.Assert(objSender is RadioBtn);
- RadioBtn radioSender = (RadioBtn)objSender;
- System.Diagnostics.Debug.Assert(radioSender.Val > allKnownEnumTypes[typeOfEnum]);
- strVal = radioSender.Val;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement