StyleSheet.cs 6.9 KB

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