PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/SSATool.UI.ViewModel/DataServiceCollection.cs

#
C# | 237 lines | 133 code | 30 blank | 74 comment | 8 complexity | 1a385d236cef647fcab544c4713563d5 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1
  1. #region Header
  2. // ------------------------ Licence / Copyright ------------------------
  3. //
  4. // Simple Service Administration Tool for WinNT based systems.
  5. // Copyright © 2010 - Silvan Gehrig
  6. //
  7. // This library is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU Lesser General Public
  9. // License as published by the Free Software Foundation; either
  10. // version 2.1 of the License, or (at your option) any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. // Lesser General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public
  18. // License along with this library; if not, write to the Free Software
  19. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. //
  21. // Author:
  22. // Silvan Gehrig
  23. //
  24. // ---------------------------------------------------------------------
  25. #endregion
  26. #region Usings
  27. using System;
  28. using System.Collections.ObjectModel;
  29. using System.Collections.Specialized;
  30. using SSATool.BL.DM;
  31. using SSATool.Common.Util;
  32. using System.ComponentModel;
  33. #endregion
  34. namespace SSATool.UI.ViewModel
  35. {
  36. /// <summary>
  37. /// Represents a collection of service instances.
  38. /// </summary>
  39. public class DataServiceCollection : ObservableCollection<DataService>
  40. {
  41. #region Declarations
  42. //--------------------------------------------------------------------
  43. // Declarations
  44. //--------------------------------------------------------------------
  45. private readonly ServiceCollection _domainCollection;
  46. private bool _eventAdded = false;
  47. private bool _innerEventsEnabled = true;
  48. private NotifyCollectionChangedEventHandler _onCollectionChanged;
  49. #endregion
  50. #region Properties
  51. //--------------------------------------------------------------------
  52. // Properties
  53. //--------------------------------------------------------------------
  54. /// <summary>
  55. /// Gets the service with the given name.
  56. /// </summary>
  57. /// <param name="serviceName">Name of the service to retrieve.</param>
  58. /// <returns>Returns the retrieved service or fires an exception if it could not be found.</returns>
  59. public DataService this[string serviceName]
  60. {
  61. get { return new DataService(this, _domainCollection[serviceName]); }
  62. }
  63. #endregion
  64. #region Constructors / Destructor
  65. //--------------------------------------------------------------------
  66. // Constructors / Destructor
  67. //--------------------------------------------------------------------
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="DataServiceCollection"/> class.
  70. /// </summary>
  71. /// <param name="domainCollection">Specifies the parent domain collection instance.</param>
  72. public DataServiceCollection(ServiceCollection domainCollection)
  73. {
  74. PreCondition.AssertNotNull(domainCollection, "domainCollection");
  75. _domainCollection = domainCollection;
  76. }
  77. #endregion
  78. #region Methods
  79. //--------------------------------------------------------------------
  80. // Methods
  81. //--------------------------------------------------------------------
  82. /// <summary>
  83. /// Determines whether the current collection contains the specified item.
  84. /// </summary>
  85. /// <param name="serviceName">The name of the service to check.</param>
  86. /// <returns>
  87. /// <c>true</c> if the current collection contains the specified item; otherwise, <c>false</c>.
  88. /// </returns>
  89. public bool Contains(string serviceName)
  90. {
  91. PreCondition.AssertNotNull(serviceName, "serviceName");
  92. return _domainCollection.Contains(serviceName);
  93. }
  94. /// <summary>
  95. /// Refreshes the data with the underlying data source.
  96. /// </summary>
  97. public void Reset()
  98. {
  99. CallWithoutInnerEvents(() => _domainCollection.Reset());
  100. Clear();
  101. foreach (Service service in _domainCollection)
  102. {
  103. Items.Add(new DataService(this, service));
  104. }
  105. OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  106. }
  107. /// <summary>
  108. /// Fires the ChildPropertyChanged event.
  109. /// </summary>
  110. /// <param name="sender">Specifies the sender service instance.</param>
  111. /// <param name="args">Event args to pass to the receivers.</param>
  112. internal void FireChildChanged(DataService sender, PropertyChangedEventArgs args)
  113. {
  114. if (ChildChanged != null)
  115. ChildChanged(sender, args);
  116. }
  117. private void CallWithoutInnerEvents(Action toExecute)
  118. {
  119. try
  120. {
  121. _innerEventsEnabled = false;
  122. toExecute();
  123. }
  124. finally
  125. {
  126. _innerEventsEnabled = true;
  127. }
  128. }
  129. private void EnsureCollectionChangedEventAdded()
  130. {
  131. if (!_eventAdded)
  132. {
  133. _domainCollection.CollectionChanged += OnDomainCollectionCollectionChanged;
  134. _eventAdded = true;
  135. }
  136. }
  137. private void EnsureCollectionChangedEventRemoved()
  138. {
  139. if (_onCollectionChanged != null && _eventAdded)
  140. {
  141. _domainCollection.CollectionChanged -= OnDomainCollectionCollectionChanged;
  142. _eventAdded = false;
  143. }
  144. }
  145. #endregion
  146. #region Events
  147. //--------------------------------------------------------------------
  148. // Events
  149. //--------------------------------------------------------------------
  150. /// <summary>
  151. /// Occurs when a child item mutated.
  152. /// </summary>
  153. public event PropertyChangedEventHandler ChildChanged;
  154. /// <summary>
  155. /// Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.
  156. /// </summary>
  157. public event NotifyCollectionChangedEventHandler Changed
  158. {
  159. add
  160. {
  161. _onCollectionChanged += value;
  162. EnsureCollectionChangedEventAdded();
  163. }
  164. remove
  165. {
  166. _onCollectionChanged -= value;
  167. EnsureCollectionChangedEventRemoved();
  168. }
  169. }
  170. /// <summary>
  171. /// Occurs when an item is added, removed, changed, moved, or the entire list is refreshed.
  172. /// </summary>
  173. public override event NotifyCollectionChangedEventHandler CollectionChanged
  174. {
  175. add
  176. {
  177. _onCollectionChanged += value;
  178. EnsureCollectionChangedEventAdded();
  179. }
  180. remove
  181. {
  182. _onCollectionChanged -= value;
  183. EnsureCollectionChangedEventRemoved();
  184. }
  185. }
  186. private void OnDomainCollectionCollectionChanged(object sender, ServiceCollectionChangedEventArgs e)
  187. {
  188. if (!_innerEventsEnabled)
  189. return;
  190. switch (e.Action)
  191. {
  192. case ServiceCollectionChangedAction.Add:
  193. Add(new DataService(this, e.ChangedItem));
  194. break;
  195. case ServiceCollectionChangedAction.Remove:
  196. Remove(new DataService(this, e.ChangedItem));
  197. break;
  198. case ServiceCollectionChangedAction.Reset:
  199. Reset();
  200. break;
  201. default:
  202. throw new ArgumentOutOfRangeException();
  203. }
  204. }
  205. #endregion
  206. }
  207. }