SvgElementCollection.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. /// <summary>
  9. /// Represents a collection of <see cref="SvgElement"/>s.
  10. /// </summary>
  11. public sealed class SvgElementCollection : IList<SvgElement>
  12. {
  13. private List<SvgElement> _elements;
  14. private SvgElement _owner;
  15. private bool _mock;
  16. /// <summary>
  17. /// Initialises a new instance of an <see cref="SvgElementCollection"/> class.
  18. /// </summary>
  19. /// <param name="owner">The owner <see cref="SvgElement"/> of the collection.</param>
  20. internal SvgElementCollection(SvgElement owner)
  21. : this(owner, false)
  22. {
  23. }
  24. internal SvgElementCollection(SvgElement owner, bool mock)
  25. {
  26. if (owner == null)
  27. {
  28. throw new ArgumentNullException("owner");
  29. }
  30. this._elements = new List<SvgElement>();
  31. this._owner = owner;
  32. this._mock = mock;
  33. }
  34. /// <summary>
  35. /// Returns the index of the specified <see cref="SvgElement"/> in the collection.
  36. /// </summary>
  37. /// <param name="item">The <see cref="SvgElement"/> to search for.</param>
  38. /// <returns>The index of the element if it is present; otherwise -1.</returns>
  39. public int IndexOf(SvgElement item)
  40. {
  41. return this._elements.IndexOf(item);
  42. }
  43. /// <summary>
  44. /// Inserts the given <see cref="SvgElement"/> to the collection at the specified index.
  45. /// </summary>
  46. /// <param name="index">The index that the <paramref name="item"/> should be added at.</param>
  47. /// <param name="item">The <see cref="SvgElement"/> to be added.</param>
  48. public void Insert(int index, SvgElement item)
  49. {
  50. InsertAndForceUniqueID(index, item, true, true, LogIDChange);
  51. }
  52. private void LogIDChange(SvgElement elem, string oldId, string newID)
  53. {
  54. System.Diagnostics.Debug.WriteLine("ID of SVG element " + elem.ToString() + " changed from " + oldId + " to " + newID);
  55. }
  56. public void InsertAndForceUniqueID(int index, SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
  57. {
  58. AddToIdManager(item, this._elements[index], autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID);
  59. this._elements.Insert(index, item);
  60. item._parent.OnElementAdded(item, index);
  61. }
  62. public void RemoveAt(int index)
  63. {
  64. SvgElement element = this[index];
  65. if (element != null)
  66. {
  67. this.Remove(element);
  68. }
  69. }
  70. public SvgElement this[int index]
  71. {
  72. get { return this._elements[index]; }
  73. set { this._elements[index] = value; }
  74. }
  75. public void Add(SvgElement item)
  76. {
  77. this.AddAndForceUniqueID(item, true, true, LogIDChange);
  78. }
  79. public void AddAndForceUniqueID(SvgElement item, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
  80. {
  81. AddToIdManager(item, null, autoForceUniqueID, autoFixChildrenID, logElementOldIDNewID);
  82. this._elements.Add(item);
  83. item._parent.OnElementAdded(item, this.Count - 1);
  84. }
  85. private void AddToIdManager(SvgElement item, SvgElement sibling, bool autoForceUniqueID = true, bool autoFixChildrenID = true, Action<SvgElement, string, string> logElementOldIDNewID = null)
  86. {
  87. if (!this._mock)
  88. {
  89. if (this._owner.OwnerDocument != null)
  90. {
  91. this._owner.OwnerDocument.IdManager.AddAndForceUniqueID(item, sibling, autoForceUniqueID, logElementOldIDNewID);
  92. if (!(item is SvgDocument)) //don't add subtree of a document to parent document
  93. {
  94. foreach (var child in item.Children)
  95. {
  96. child.ApplyRecursive(e => this._owner.OwnerDocument.IdManager.AddAndForceUniqueID(e, null, autoFixChildrenID, logElementOldIDNewID));
  97. }
  98. }
  99. }
  100. //if all checked, set parent
  101. item._parent = this._owner;
  102. }
  103. }
  104. public void Clear()
  105. {
  106. while (this.Count > 0)
  107. {
  108. SvgElement element = this[0];
  109. this.Remove(element);
  110. }
  111. }
  112. public bool Contains(SvgElement item)
  113. {
  114. return this._elements.Contains(item);
  115. }
  116. public void CopyTo(SvgElement[] array, int arrayIndex)
  117. {
  118. this._elements.CopyTo(array, arrayIndex);
  119. }
  120. public int Count
  121. {
  122. get { return this._elements.Count; }
  123. }
  124. public bool IsReadOnly
  125. {
  126. get { return false; }
  127. }
  128. public bool Remove(SvgElement item)
  129. {
  130. bool removed = this._elements.Remove(item);
  131. if (removed)
  132. {
  133. this._owner.OnElementRemoved(item);
  134. if (!this._mock)
  135. {
  136. item._parent = null;
  137. if (this._owner.OwnerDocument != null)
  138. {
  139. item.ApplyRecursiveDepthFirst(this._owner.OwnerDocument.IdManager.Remove);
  140. }
  141. }
  142. }
  143. return removed;
  144. }
  145. /// <summary>
  146. /// expensive recursive search for nodes of type T
  147. /// </summary>
  148. /// <typeparam name="T"></typeparam>
  149. /// <returns></returns>
  150. public IEnumerable<T> FindSvgElementsOf<T>() where T : SvgElement
  151. {
  152. return _elements.Where(x => x is T).Select(x => x as T).Concat(_elements.SelectMany(x => x.Children.FindSvgElementsOf<T>()));
  153. }
  154. /// <summary>
  155. /// expensive recursive search for first node of type T
  156. /// </summary>
  157. /// <typeparam name="T"></typeparam>
  158. /// <returns></returns>
  159. public T FindSvgElementOf<T>() where T : SvgElement
  160. {
  161. return _elements.OfType<T>().FirstOrDefault() ?? _elements.Select(x => x.Children.FindSvgElementOf<T>()).FirstOrDefault<T>(x => x != null);
  162. }
  163. public T GetSvgElementOf<T>() where T : SvgElement
  164. {
  165. return _elements.FirstOrDefault(x => x is T) as T;
  166. }
  167. public IEnumerator<SvgElement> GetEnumerator()
  168. {
  169. return this._elements.GetEnumerator();
  170. }
  171. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  172. {
  173. return this._elements.GetEnumerator();
  174. }
  175. }
  176. }
  177. #pragma warning restore