#region MigraDoc - Creating Documents on the Fly // // Authors: // Stefan Lange // Klaus Potzesny // David Stephensen // // Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://www.migradoc.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using System.Collections; using System.Collections.Generic; using MigraDoc.DocumentObjectModel.Visitors; namespace MigraDoc.DocumentObjectModel { /// /// Base class of all collections of the MigraDoc Document Object Model. /// public abstract class DocumentObjectCollection : DocumentObject, IList, IVisitable { /// /// Initializes a new instance of the DocumentObjectCollection class. /// public DocumentObjectCollection() { _elements = new List(); } /// /// Initializes a new instance of the DocumentObjectCollection class with the specified parent. /// public DocumentObjectCollection(DocumentObject parent) : base(parent) { _elements = new List(); } /// /// Gets the first value in the Collection, if there is any, otherwise null. /// public DocumentObject First { get { return Count > 0 ? this[0] : null; } } /// /// Creates a deep copy of this object. /// public new DocumentObjectCollection Clone() { return (DocumentObjectCollection)DeepCopy(); } /// /// Implements the deep copy of the object. /// protected override object DeepCopy() { DocumentObjectCollection coll = (DocumentObjectCollection)base.DeepCopy(); int count = Count; coll._elements = new List(count); for (int index = 0; index < count; ++index) { DocumentObject doc = this[index]; if (doc != null) { doc = doc.Clone() as DocumentObject; doc._parent = coll; } coll._elements.Add(doc); } return coll; } /// /// Copies the entire collection to a compatible one-dimensional System.Array, /// starting at the specified index of the target array. /// public void CopyTo(Array array, int index) { throw new NotImplementedException("TODO"); //this .elements.CopyTo(array, index); } /// /// Gets a value indicating whether the Collection is read-only. /// bool IList.IsReadOnly { get { return false; } } /// /// Gets a value indicating whether the Collection has a fixed size. /// bool IList.IsFixedSize { get { return false; } } /// /// Gets a value indicating whether access to the Collection is synchronized. /// bool ICollection.IsSynchronized { get { return false; } } /// /// Gets an object that can be used to synchronize access to the collection. /// object ICollection.SyncRoot { get { return null; } } /// /// Gets the number of elements actually contained in the collection. /// public int Count { get { return _elements.Count; } } /// /// Removes all elements from the collection. /// public void Clear() { ((IList)this).Clear(); } /// /// Inserts an object at the specified index. /// public virtual void InsertObject(int index, DocumentObject val) { SetParent(val); ((IList)this).Insert(index, val); // Call ResetCachedValues for all objects moved by the Insert operation. int count = ((IList)this).Count; for (int idx = index + 1; idx < count; ++idx) { DocumentObject obj = (DocumentObject)((IList)this)[idx]; obj.ResetCachedValues(); } } /// /// Determines the index of a specific item in the collection. /// public int IndexOf(DocumentObject val) { return ((IList)this).IndexOf(val); } /// /// Gets or sets the element at the specified index. /// public virtual DocumentObject this[int index] { get { return _elements[index] as DocumentObject; } set { SetParent(value); _elements[index] = value; } } /// /// Gets the last element or null, if no such element exists. /// public DocumentObject LastObject { get { int count = _elements.Count; if (count > 0) return (DocumentObject)_elements[count - 1]; return null; } } /// /// Removes the element at the specified index. /// public void RemoveObjectAt(int index) { ((IList)this).RemoveAt(index); // Call ResetCachedValues for all objects moved by the RemoveAt operation. int count = ((IList)this).Count; for (int idx = index; idx < count; ++idx) { DocumentObject obj = (DocumentObject)((IList)this)[idx]; obj.ResetCachedValues(); } } /// /// Inserts the object into the collection and sets its parent. /// public virtual void Add(DocumentObject value) { SetParent(value); _elements.Add(value); } /// /// Determines whether this instance is null. /// public override bool IsNull() { if (!Meta.IsNull(this)) return false; if (_elements == null) return true; foreach (DocumentObject docObject in _elements) { if (docObject != null && !docObject.IsNull()) return false; } return true; } /// /// Allows the visitor object to visit the document object and its child objects. /// void IVisitable.AcceptVisitor(DocumentObjectVisitor visitor, bool visitChildren) { visitor.VisitDocumentObjectCollection(this); foreach (DocumentObject docobj in this) { IVisitable visitable = docobj as IVisitable; if (visitable != null) visitable.AcceptVisitor(visitor, visitChildren); } } /// /// Returns an enumerator that can iterate through this collection. /// public IEnumerator GetEnumerator() { return _elements.GetEnumerator(); } List _elements; #region IList Members /// /// Gets or sets the element at the specified index. /// object IList.this[int index] { get { return _elements[index]; } set { _elements[index] = value; } } /// /// Removes the item at the specified index from the Collection. /// void IList.RemoveAt(int index) { _elements.RemoveAt(index); } /// /// Inserts an object at the specified index. /// void IList.Insert(int index, object value) { _elements.Insert(index, value); } /// /// Removes the first occurrence of the specific object. /// void IList.Remove(object value) { _elements.Remove(value); } /// /// Determines whether an element exists. /// bool IList.Contains(object value) { return _elements.Contains(value); } /// /// Determines the index of a specific item in the Collection. /// int IList.IndexOf(object value) { return _elements.IndexOf(value); } /// /// Adds an item to the Collection. /// int IList.Add(object value) { _elements.Add(value); return _elements.Count - 1; } /// /// Removes all items from the Collection. /// void IList.Clear() { _elements.Clear(); } #endregion } }