OptionsServiceImpl.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using SRDebugger.Internal;
  7. using SRF.Service;
  8. namespace SRDebugger.Services.Implementation
  9. {
  10. [Service(typeof(IOptionsService))]
  11. public class OptionsServiceImpl : IOptionsService
  12. {
  13. public OptionsServiceImpl()
  14. {
  15. this._optionsReadonly = new ReadOnlyCollection<OptionDefinition>(this._options);
  16. this.Scan(SROptions.Current);
  17. SROptions.Current.PropertyChanged += this.OnSROptionsPropertyChanged;
  18. }
  19. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  20. public event EventHandler OptionsUpdated;
  21. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  22. public event EventHandler<PropertyChangedEventArgs> OptionsValueUpdated;
  23. public ICollection<OptionDefinition> Options
  24. {
  25. get
  26. {
  27. return this._optionsReadonly;
  28. }
  29. }
  30. public void Scan(object obj)
  31. {
  32. this.AddContainer(obj);
  33. }
  34. public void AddContainer(object obj)
  35. {
  36. if (this._optionContainerLookup.ContainsKey(obj))
  37. {
  38. throw new Exception("An object should only be added once.");
  39. }
  40. ICollection<OptionDefinition> collection = SRDebuggerUtil.ScanForOptions(obj);
  41. this._optionContainerLookup.Add(obj, collection);
  42. if (collection.Count > 0)
  43. {
  44. this._options.AddRange(collection);
  45. this.OnOptionsUpdated();
  46. INotifyPropertyChanged notifyPropertyChanged = obj as INotifyPropertyChanged;
  47. if (notifyPropertyChanged != null)
  48. {
  49. notifyPropertyChanged.PropertyChanged += this.OnPropertyChanged;
  50. }
  51. }
  52. }
  53. public void RemoveContainer(object obj)
  54. {
  55. if (!this._optionContainerLookup.ContainsKey(obj))
  56. {
  57. return;
  58. }
  59. ICollection<OptionDefinition> collection = this._optionContainerLookup[obj];
  60. this._optionContainerLookup.Remove(obj);
  61. foreach (OptionDefinition item in collection)
  62. {
  63. this._options.Remove(item);
  64. }
  65. INotifyPropertyChanged notifyPropertyChanged = obj as INotifyPropertyChanged;
  66. if (notifyPropertyChanged != null)
  67. {
  68. notifyPropertyChanged.PropertyChanged -= this.OnPropertyChanged;
  69. }
  70. this.OnOptionsUpdated();
  71. }
  72. private void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
  73. {
  74. if (this.OptionsValueUpdated != null)
  75. {
  76. this.OptionsValueUpdated(this, propertyChangedEventArgs);
  77. }
  78. }
  79. private void OnSROptionsPropertyChanged(object sender, string propertyName)
  80. {
  81. this.OnPropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
  82. }
  83. private void OnOptionsUpdated()
  84. {
  85. if (this.OptionsUpdated != null)
  86. {
  87. this.OptionsUpdated(this, EventArgs.Empty);
  88. }
  89. }
  90. private readonly Dictionary<object, ICollection<OptionDefinition>> _optionContainerLookup = new Dictionary<object, ICollection<OptionDefinition>>();
  91. private readonly List<OptionDefinition> _options = new List<OptionDefinition>();
  92. private readonly IList<OptionDefinition> _optionsReadonly;
  93. }
  94. }