// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; using System.Linq; namespace ICSharpCode.SharpDevelop.Dom { /// /// Event handler for the event. /// /// /// We don't use the classic 'EventArgs' model for this event, because a EventArgs-class couldn't be covariant. /// public delegate void ModelCollectionChangedEventHandler(IReadOnlyCollection removedItems, IReadOnlyCollection addedItems); public class ModelCollectionChangedEvent { List> _handlers = new List>(); // public static ModelCollectionChangedEvent operator+(ModelCollectionChangedEvent eventObject, ModelCollectionChangedEventHandler handler) // { // eventObject._handlers.Add(handler); // return eventObject; // } public void AddHandler(ModelCollectionChangedEventHandler handler) { _handlers.Add(handler); } // public static ModelCollectionChangedEvent operator-(ModelCollectionChangedEvent eventObject, ModelCollectionChangedEventHandler handler) // { // eventObject._handlers.Remove(handler); // return eventObject; // } public void RemoveHandler(ModelCollectionChangedEventHandler handler) { _handlers.Remove(handler); } public void Fire(IReadOnlyCollection removedItems, IReadOnlyCollection addedItems) { foreach (var handler in _handlers) { if (handler != null) { handler(removedItems, addedItems); } } } public bool ContainsHandlers { get { return _handlers.Count > 0; } } } /// /// A read-only collection that provides change notifications. /// /// /// We don't use INotifyCollectionChanged because that has the annoying 'Reset' update, /// where it's impossible for to detect what kind of changes happened unless the event consumer /// maintains a copy of the list. /// Also, INotifyCollectionChanged isn't type-safe. /// public interface IModelCollection : IReadOnlyCollection { event ModelCollectionChangedEventHandler CollectionChanged; /// /// Creates an immutable snapshot of the collection. /// IReadOnlyCollection CreateSnapshot(); } /// /// A collection that provides change notifications. /// public interface IMutableModelCollection : IModelCollection, ICollection { /// /// Gets the number of items in the collection. /// /// /// Disambiguates between IReadOnlyCollection.Count and ICollection.Count /// new int Count { get; } /// /// Adds the specified items to the collection. /// void AddRange(IEnumerable items); /// /// Removes all items matching the specified precidate. /// int RemoveAll(Predicate predicate); /// /// Can be used to group several operations into a batch update. /// The event will not fire during the batch update; /// instead the event will be raised a single time after the batch update finishes. /// IDisposable BatchUpdate(); } }