PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/PlotterChildrenCollection.cs

#
C# | 209 lines | 159 code | 36 blank | 14 comment | 26 complexity | 04f5be6a730acf5f4801de87fdbb2584 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using System.Windows.Markup;
  7. using Microsoft.Research.DynamicDataDisplay.Charts;
  8. using System.Windows;
  9. using System.Windows.Controls.Primitives;
  10. using System.Collections;
  11. namespace Microsoft.Research.DynamicDataDisplay.Common
  12. {
  13. /// <summary>
  14. /// Contains all charts added to ChartPlotter.
  15. /// </summary>
  16. [ContentWrapper(typeof(ViewportUIContainer))]
  17. public sealed class PlotterChildrenCollection : D3Collection<IPlotterElement>, IList
  18. {
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="PlotterChildrenCollection"/> class.
  21. /// </summary>
  22. internal PlotterChildrenCollection(Plotter plotter)
  23. {
  24. if (plotter == null)
  25. throw new ArgumentNullException("plotter");
  26. this.plotter = plotter;
  27. }
  28. private readonly Plotter plotter;
  29. public Plotter Plotter
  30. {
  31. get { return plotter; }
  32. }
  33. /// <summary>
  34. /// Called before item added to collection. Enables to perform validation.
  35. /// </summary>
  36. /// <param name="item">The adding item.</param>
  37. protected override void OnItemAdding(IPlotterElement item)
  38. {
  39. if (item == null)
  40. throw new ArgumentNullException("item");
  41. }
  42. /// <summary>
  43. /// This override enables notifying about removing each element, instead of
  44. /// notifying about collection reset.
  45. /// </summary>
  46. protected override void ClearItems()
  47. {
  48. var items = new List<IPlotterElement>(base.Items);
  49. foreach (var item in items)
  50. {
  51. Remove(item);
  52. }
  53. }
  54. #region Foreign content
  55. public void Add(FrameworkElement content)
  56. {
  57. if (content == null)
  58. throw new ArgumentNullException("content");
  59. IPlotterElement plotterElement = content as IPlotterElement;
  60. if (plotterElement != null)
  61. {
  62. Add(plotterElement);
  63. }
  64. else
  65. {
  66. ViewportUIContainer container = new ViewportUIContainer(content);
  67. Add(container);
  68. }
  69. }
  70. #endregion // end of Foreign content
  71. #region IList Members
  72. int IList.Add(object value)
  73. {
  74. if (value == null)
  75. throw new ArgumentNullException("value");
  76. FrameworkElement content = value as FrameworkElement;
  77. if (content != null)
  78. {
  79. Add(content);
  80. return 0;
  81. }
  82. IPlotterElement element = value as IPlotterElement;
  83. if (element != null)
  84. {
  85. Add(element);
  86. return 0;
  87. }
  88. throw new ArgumentException(String.Format("Children of type '{0}' are not supported.", value.GetType()));
  89. }
  90. void IList.Clear()
  91. {
  92. Clear();
  93. }
  94. bool IList.Contains(object value)
  95. {
  96. IPlotterElement element = value as IPlotterElement;
  97. return element != null && Contains(element);
  98. }
  99. int IList.IndexOf(object value)
  100. {
  101. IPlotterElement element = value as IPlotterElement;
  102. if (element != null)
  103. return IndexOf(element);
  104. return -1;
  105. }
  106. void IList.Insert(int index, object value)
  107. {
  108. IPlotterElement element = value as IPlotterElement;
  109. if (element != null)
  110. {
  111. Insert(index, element);
  112. }
  113. }
  114. bool IList.IsFixedSize
  115. {
  116. get { return false; }
  117. }
  118. bool IList.IsReadOnly
  119. {
  120. get { return false; }
  121. }
  122. void IList.Remove(object value)
  123. {
  124. IPlotterElement element = value as IPlotterElement;
  125. if (element != null)
  126. Remove(element);
  127. }
  128. void IList.RemoveAt(int index)
  129. {
  130. RemoveAt(index);
  131. }
  132. object IList.this[int index]
  133. {
  134. get
  135. {
  136. return this[index];
  137. }
  138. set
  139. {
  140. IPlotterElement element = value as IPlotterElement;
  141. if (element != null)
  142. this[index] = element;
  143. }
  144. }
  145. #endregion
  146. #region ICollection Members
  147. void ICollection.CopyTo(Array array, int index)
  148. {
  149. IPlotterElement[] elements = array as IPlotterElement[];
  150. if (elements != null)
  151. CopyTo(elements, index);
  152. }
  153. int ICollection.Count
  154. {
  155. get { return Count; }
  156. }
  157. bool ICollection.IsSynchronized
  158. {
  159. get { return false; }
  160. }
  161. object ICollection.SyncRoot
  162. {
  163. get { return null; }
  164. }
  165. #endregion
  166. #region IEnumerable Members
  167. IEnumerator IEnumerable.GetEnumerator()
  168. {
  169. return GetEnumerator();
  170. }
  171. #endregion
  172. }
  173. }