using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.IO; using FastReport.Utils; namespace FastReport { /// /// Represents a collection of styles used in the . /// public class StyleCollection : CollectionBase, IFRSerializable { private string name; /// /// Gets or sets the name of the style. /// public string Name { get { return name; } set { name = value; } } /// /// Gets or sets the element at the specified index. /// /// Index of an element. /// The element at the specified index. public Style this[int index] { get { return List[index] as Style; } set { List[index] = value; } } /// /// Adds the specified elements to the end of this collection. /// /// public void AddRange(Style[] range) { foreach (Style s in range) { Add(s); } } /// /// Adds an object to the end of this collection. /// /// Object to add. /// Index of the added object. public int Add(Style value) { return List.Add(value); } /// /// Inserts an object into this collection at the specified index. /// /// The zero-based index at which value should be inserted. /// The object to insert. public void Insert(int index, Style value) { List.Insert(index, value); } /// /// Removes the specified object from the collection. /// /// Object to remove. public void Remove(Style value) { int i = IndexOf(value); if (i != -1) List.RemoveAt(i); } /// /// Returns the zero-based index of the first occurrence of an object. /// /// The object to locate in the collection. /// The zero-based index of the first occurrence of value within the entire collection, if found; /// otherwise, -1. public int IndexOf(Style value) { return List.IndexOf(value); } /// /// Returns the zero-based index of the first occurrence of a style with specified name. /// /// The name to locate in the collection. /// The zero-based index of the first occurrence of value within the entire collection, if found; /// otherwise, -1. public int IndexOf(string value) { for (int i = 0; i < Count; i++) { Style s = this[i]; if (String.Compare(s.Name, value, true) == 0) return i; } return -1; } /// /// Determines whether an element is in the collection. /// /// The object to locate in the collection. /// true if object is found in the collection; otherwise, false. public bool Contains(Style value) { return IndexOf(value) != -1; } /// /// Determines whether a style with specified name is in the collection. /// /// The style name to locate in the collection. /// true if object is found in the collection; otherwise, false. public bool Contains(string value) { return IndexOf(value) != -1; } /// public void Serialize(FRWriter writer) { writer.ItemName = "Styles"; if (!String.IsNullOrEmpty(Name)) writer.WriteStr("Name", Name); foreach (Style s in this) { writer.Write(s); } } /// public void Deserialize(FRReader reader) { Clear(); Name = ""; reader.ReadProperties(this); while (reader.NextItem()) { Style s = new Style(); reader.Read(s); Add(s); } } /// /// Saves the collection to a stream. /// /// Stream to save to. public void Save(Stream stream) { using (FRWriter writer = new FRWriter()) { writer.Write(this); writer.Save(stream); } } /// /// Saves the collection to a file. /// /// The name of the file. public void Save(string fileName) { using (FileStream f = new FileStream(fileName, FileMode.Create)) { Save(f); } } /// /// Loads the collection from a stream. /// /// Stream to load from. public void Load(Stream stream) { using (FRReader reader = new FRReader(null)) { reader.Load(stream); reader.Read(this); } } /// /// Loads the collection from a file. /// /// The name of the file. public void Load(string fileName) { using (FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { Load(f); } } /// /// Creates exact copy of this collection. /// /// The copy of this collection. public StyleCollection Clone() { StyleCollection result = new StyleCollection(); foreach (Style s in this) { result.Add(s.Clone()); } return result; } /// /// Initializes a new instance of the class with default settings. /// public StyleCollection() { name = ""; } } }