using System; using System.Text; using System.Collections; using System.ComponentModel; namespace DevComponents.DotNetBar { /// /// Represents collection of CrumbBarItem buttons. /// public class CrumbBarItemsCollection : CollectionBase { #region Private Variables private CrumbBar _Parent = null; #endregion #region Internal Implementation /// /// Initializes a new instance of the CrumbBarItemsCollection class. /// /// public CrumbBarItemsCollection(CrumbBar parent) { _Parent = parent; } /// /// Gets or sets the node this collection is associated with. /// [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public CrumbBar Parent { get { return _Parent; } } /// /// Sets the node collection belongs to. /// /// CrumbBarItem that is parent of this collection. internal void SetParent(CrumbBar parent) { _Parent = parent; } /// /// Adds new object to the collection. /// /// Object to add. /// Index of newly added object. public int Add(CrumbBarItem ch) { return List.Add(ch); } /// /// Returns reference to the object in collection based on it's index. /// public CrumbBarItem this[int index] { get { return (CrumbBarItem)(List[index]); } set { List[index] = value; } } /// /// Returns reference to the object in collection based on it's name. /// public CrumbBarItem this[string name] { get { return GetByName(name); } set { int index = GetIndexByName(name); if (index == -1) throw new ArgumentException("name cannot be found in this collection"); List[index] = value; } } /// /// Inserts new object into the collection. /// /// Position of the object. /// Object to insert. public void Insert(int index, CrumbBarItem value) { List.Insert(index, value); } /// /// Returns index of the object inside of the collection. /// /// Reference to the object. /// Index of the object. public int IndexOf(CrumbBarItem value) { return List.IndexOf(value); } /// /// Returns whether collection contains specified object. /// /// Object to look for. /// true if object is part of the collection, otherwise false. public bool Contains(CrumbBarItem value) { return List.Contains(value); } /// /// Removes specified object from the collection. /// /// public void Remove(CrumbBarItem value) { List.Remove(value); } protected override void OnSet(int index, object oldValue, object newValue) { CrumbBarItem item = (CrumbBarItem)oldValue; item.SetOwner(null); item = (CrumbBarItem)newValue; item.SetOwner(_Parent); base.OnSet(index, oldValue, newValue); } protected override void OnRemoveComplete(int index, object value) { CrumbBarItem item = (CrumbBarItem)value; item.SetOwner(null); if (item.IsSelected) _Parent.SetSelectedItem(null, eEventSource.Code); base.OnRemoveComplete(index, value); } protected override void OnInsertComplete(int index, object value) { CrumbBarItem item = (CrumbBarItem)value; item.SetOwner(_Parent); item.ContainerControl = _Parent; item.Style = eDotNetBarStyle.Office2007; base.OnInsertComplete(index, value); } /// /// Copies collection into the specified array. /// /// Array to copy collection to. /// Starting index. public void CopyTo(CrumbBarItem[] array, int index) { List.CopyTo(array, index); } /// /// Copies contained items to the CrumbBarItem array. /// /// Array to copy to. internal void CopyTo(CrumbBarItem[] array) { List.CopyTo(array, 0); } protected override void OnClear() { foreach (CrumbBarItem item in List) { item.SetOwner(null); } base.OnClear(); } protected override void OnClearComplete() { if (_Parent != null) _Parent.OnItemsCleared(); base.OnClearComplete(); } private CrumbBarItem GetByName(string name) { foreach (CrumbBarItem d in this.List) { if (d.Name == name) return d; } return null; } private int GetIndexByName(string name) { for (int i = 0; i < this.List.Count; i++) { CrumbBarItem item = this[i]; if (item.Name == name) return i; } return -1; } #endregion } }