StyleCollection.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using System.IO;
  6. using FastReport.Utils;
  7. namespace FastReport
  8. {
  9. /// <summary>
  10. /// Represents a collection of styles used in the <see cref="Report.Styles"/>.
  11. /// </summary>
  12. public class StyleCollection : CollectionBase, IFRSerializable
  13. {
  14. private string name;
  15. /// <summary>
  16. /// Gets or sets the name of the style.
  17. /// </summary>
  18. public string Name
  19. {
  20. get { return name; }
  21. set { name = value; }
  22. }
  23. /// <summary>
  24. /// Gets or sets the element at the specified index.
  25. /// </summary>
  26. /// <param name="index">Index of an element.</param>
  27. /// <returns>The element at the specified index.</returns>
  28. public Style this[int index]
  29. {
  30. get { return List[index] as Style; }
  31. set { List[index] = value; }
  32. }
  33. /// <summary>
  34. /// Adds the specified elements to the end of this collection.
  35. /// </summary>
  36. /// <param name="range"></param>
  37. public void AddRange(Style[] range)
  38. {
  39. foreach (Style s in range)
  40. {
  41. Add(s);
  42. }
  43. }
  44. /// <summary>
  45. /// Adds an object to the end of this collection.
  46. /// </summary>
  47. /// <param name="value">Object to add.</param>
  48. /// <returns>Index of the added object.</returns>
  49. public int Add(Style value)
  50. {
  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, Style value)
  59. {
  60. List.Insert(index, value);
  61. }
  62. /// <summary>
  63. /// Removes the specified object from the collection.
  64. /// </summary>
  65. /// <param name="value">Object to remove.</param>
  66. public void Remove(Style value)
  67. {
  68. int i = IndexOf(value);
  69. if (i != -1)
  70. List.RemoveAt(i);
  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(Style value)
  79. {
  80. return List.IndexOf(value);
  81. }
  82. /// <summary>
  83. /// Returns the zero-based index of the first occurrence of a style with specified name.
  84. /// </summary>
  85. /// <param name="value">The name to locate in the collection.</param>
  86. /// <returns>The zero-based index of the first occurrence of value within the entire collection, if found;
  87. /// otherwise, -1.</returns>
  88. public int IndexOf(string value)
  89. {
  90. for (int i = 0; i < Count; i++)
  91. {
  92. Style s = this[i];
  93. if (String.Compare(s.Name, value, true) == 0)
  94. return i;
  95. }
  96. return -1;
  97. }
  98. /// <summary>
  99. /// Determines whether an element is in the collection.
  100. /// </summary>
  101. /// <param name="value">The object to locate in the collection.</param>
  102. /// <returns><b>true</b> if object is found in the collection; otherwise, <b>false</b>.</returns>
  103. public bool Contains(Style value)
  104. {
  105. return IndexOf(value) != -1;
  106. }
  107. /// <summary>
  108. /// Determines whether a style with specified name is in the collection.
  109. /// </summary>
  110. /// <param name="value">The style name to locate in the collection.</param>
  111. /// <returns><b>true</b> if object is found in the collection; otherwise, <b>false</b>.</returns>
  112. public bool Contains(string value)
  113. {
  114. return IndexOf(value) != -1;
  115. }
  116. /// <inheritdoc/>
  117. public void Serialize(FRWriter writer)
  118. {
  119. writer.ItemName = "Styles";
  120. if (!String.IsNullOrEmpty(Name))
  121. writer.WriteStr("Name", Name);
  122. foreach (Style s in this)
  123. {
  124. writer.Write(s);
  125. }
  126. }
  127. /// <inheritdoc/>
  128. public void Deserialize(FRReader reader)
  129. {
  130. Clear();
  131. Name = "";
  132. reader.ReadProperties(this);
  133. while (reader.NextItem())
  134. {
  135. Style s = new Style();
  136. reader.Read(s);
  137. Add(s);
  138. }
  139. }
  140. /// <summary>
  141. /// Saves the collection to a stream.
  142. /// </summary>
  143. /// <param name="stream">Stream to save to.</param>
  144. public void Save(Stream stream)
  145. {
  146. using (FRWriter writer = new FRWriter())
  147. {
  148. writer.Write(this);
  149. writer.Save(stream);
  150. }
  151. }
  152. /// <summary>
  153. /// Saves the collection to a file.
  154. /// </summary>
  155. /// <param name="fileName">The name of the file.</param>
  156. public void Save(string fileName)
  157. {
  158. using (FileStream f = new FileStream(fileName, FileMode.Create))
  159. {
  160. Save(f);
  161. }
  162. }
  163. /// <summary>
  164. /// Loads the collection from a stream.
  165. /// </summary>
  166. /// <param name="stream">Stream to load from.</param>
  167. public void Load(Stream stream)
  168. {
  169. using (FRReader reader = new FRReader(null))
  170. {
  171. reader.Load(stream);
  172. reader.Read(this);
  173. }
  174. }
  175. /// <summary>
  176. /// Loads the collection from a file.
  177. /// </summary>
  178. /// <param name="fileName">The name of the file.</param>
  179. public void Load(string fileName)
  180. {
  181. using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
  182. {
  183. Load(f);
  184. }
  185. }
  186. /// <summary>
  187. /// Creates exact copy of this collection.
  188. /// </summary>
  189. /// <returns>The copy of this collection.</returns>
  190. public StyleCollection Clone()
  191. {
  192. StyleCollection result = new StyleCollection();
  193. foreach (Style s in this)
  194. {
  195. result.Add(s.Clone());
  196. }
  197. return result;
  198. }
  199. /// <summary>
  200. /// Initializes a new instance of the <see cref="StyleCollection"/> class with default settings.
  201. /// </summary>
  202. public StyleCollection()
  203. {
  204. name = "";
  205. }
  206. }
  207. }