using System; using System.Collections.ObjectModel; namespace DevComponents.Instrumentation.Primitives { public class BaseCollection : Collection where T : class, ICloneable, new() { #region Events public event EventHandler CollectionChanged; #endregion #region Private variables private bool _IsRangeSet; #endregion #region AddRange /// /// Adds a range of items to the collection /// /// Array of items to add public void AddRange(T[] items) { try { _IsRangeSet = true; for (int i = 0; i < items.Length; i++) Add(items[i]); } finally { _IsRangeSet = false; OnCollectionChanged(); } } #endregion #region RemoveItem /// /// Processes list RemoveItem calls /// /// Index to remove protected override void RemoveItem(int index) { base.RemoveItem(index); OnCollectionChanged(); } #endregion #region InsertItem /// /// Processes list InsertItem calls /// /// Index to add /// Text to add protected override void InsertItem(int index, T item) { if (item != null) { base.InsertItem(index, item); OnCollectionChanged(); } } #endregion #region SetItem /// /// Processes list SetItem calls (e.g. replace) /// /// Index to replace /// Text to replace protected override void SetItem(int index, T newItem) { base.SetItem(index, newItem); OnCollectionChanged(); } #endregion #region ClearItems /// /// Processes list Clear calls (e.g. remove all) /// protected override void ClearItems() { if (Count > 0) base.ClearItems(); OnCollectionChanged(); } #endregion #region OnCollectionChanged private void OnCollectionChanged() { if (CollectionChanged != null) { if (_IsRangeSet == false) CollectionChanged(this, EventArgs.Empty); } } #endregion #region ICloneable Members public virtual object Clone() { BaseCollection copy = new BaseCollection(); CopyToItem(copy); return (copy); } #endregion #region CopyToItem internal void CopyToItem(BaseCollection copy) { foreach (T item in this) copy.Add((T)item.Clone()); } #endregion } public class GenericCollection : BaseCollection where T : GaugeItem, new() { #region Name indexer public T this[string name] { get { foreach (T item in Items) { if (item.Name != null && item.Name.Equals(name)) return (item); } return (null); } } #endregion } }