FRCollectionBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Text;
  5. namespace FastReport.Utils
  6. {
  7. /// <summary>
  8. /// Represents a collection of FastReport base objects.
  9. /// </summary>
  10. public class FRCollectionBase : CollectionBase
  11. {
  12. private Base owner;
  13. /// <summary>
  14. /// Gets an owner of this collection.
  15. /// </summary>
  16. public Base Owner
  17. {
  18. get { return owner; }
  19. }
  20. /// <summary>
  21. /// Adds the specified elements to the end of this collection.
  22. /// </summary>
  23. /// <param name="range">Range of elements.</param>
  24. public void AddRange(Base[] range)
  25. {
  26. foreach (Base c in range)
  27. {
  28. Add(c);
  29. }
  30. }
  31. /// <summary>
  32. /// Adds the specified elements to the end of this collection.
  33. /// </summary>
  34. /// <param name="range">Collection of elements.</param>
  35. public void AddRange(ObjectCollection range)
  36. {
  37. foreach (Base c in range)
  38. {
  39. Add(c);
  40. }
  41. }
  42. /// <summary>
  43. /// Adds an object to the end of this collection.
  44. /// </summary>
  45. /// <param name="value">Object to add.</param>
  46. /// <returns>Index of the added object.</returns>
  47. public int Add(Base value)
  48. {
  49. if (value == null)
  50. return -1;
  51. return List.Add(value);
  52. }
  53. /// <summary>
  54. /// Inserts an object into this collection at the specified index.
  55. /// </summary>
  56. /// <param name="index">The zero-based index at which value should be inserted.</param>
  57. /// <param name="value">The object to insert.</param>
  58. public void Insert(int index, Base value)
  59. {
  60. if (value != null)
  61. List.Insert(index, value);
  62. }
  63. /// <summary>
  64. /// Removes the specified object from the collection.
  65. /// </summary>
  66. /// <param name="value">Object to remove.</param>
  67. public void Remove(Base value)
  68. {
  69. if (Contains(value))
  70. List.Remove(value);
  71. }
  72. /// <summary>
  73. /// Returns the zero-based index of the first occurrence of an object.
  74. /// </summary>
  75. /// <param name="value">The object to locate in the collection.</param>
  76. /// <returns>The zero-based index of the first occurrence of value within the entire collection, if found;
  77. /// otherwise, -1.</returns>
  78. public int IndexOf(Base value)
  79. {
  80. return List.IndexOf(value);
  81. }
  82. /// <summary>
  83. /// Determines whether an element is in the collection.
  84. /// </summary>
  85. /// <param name="value">The object to locate in the collection.</param>
  86. /// <returns><b>true</b> if object is found in the collection; otherwise, <b>false</b>.</returns>
  87. public bool Contains(Base value)
  88. {
  89. return List.Contains(value);
  90. }
  91. /// <summary>
  92. /// Returns an array of collection items.
  93. /// </summary>
  94. /// <returns></returns>
  95. public object[] ToArray()
  96. {
  97. return InnerList.ToArray();
  98. }
  99. /// <summary>
  100. /// Determines whether two collections are equal.
  101. /// </summary>
  102. /// <param name="list">The collection to compare with.</param>
  103. /// <returns><b>true</b> if collections are equal; <b>false</b> otherwise.</returns>
  104. public bool Equals(FRCollectionBase list)
  105. {
  106. bool result = Count == list.Count;
  107. if (result)
  108. {
  109. for (int i = 0; i < list.Count; i++)
  110. if (List[i] != list.List[i])
  111. {
  112. result = false;
  113. break;
  114. }
  115. }
  116. return result;
  117. }
  118. /// <summary>
  119. /// Copies the content to another collection.
  120. /// </summary>
  121. /// <param name="list">The collection to copy to.</param>
  122. public void CopyTo(FRCollectionBase list)
  123. {
  124. list.Clear();
  125. for (int i = 0; i < Count; i++)
  126. list.Add(List[i] as Base);
  127. }
  128. /// <inheritdoc/>
  129. protected override void OnInsert(int index, Object value)
  130. {
  131. if (Owner != null)
  132. {
  133. Base c = value as Base;
  134. c.Parent = null;
  135. c.SetParent(Owner);
  136. }
  137. }
  138. /// <inheritdoc/>
  139. protected override void OnRemove(int index, object value)
  140. {
  141. if (Owner != null)
  142. (value as Base).SetParent(null);
  143. }
  144. /// <inheritdoc/>
  145. protected override void OnClear()
  146. {
  147. if (owner != null)
  148. {
  149. while (Count > 0)
  150. {
  151. (List[0] as Base).Dispose();
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// Initializes a new instance of the <b>FRCollectionBase</b> class with default settings.
  157. /// </summary>
  158. public FRCollectionBase() : this(null)
  159. {
  160. }
  161. /// <summary>
  162. /// Initializes a new instance of the <b>FRCollectionBase</b> class with specified owner.
  163. /// </summary>
  164. /// <param name="owner">The owner of this collection.</param>
  165. public FRCollectionBase(Base owner)
  166. {
  167. this.owner = owner;
  168. }
  169. }
  170. }