Browse Source

Move ModelCollectionChangedEvent to its own file.

pull/59/merge
Daniel Grunwald 12 years ago
parent
commit
bfab500c73
  1. 38
      src/Main/Base/Project/Dom/IModelCollection.cs
  2. 52
      src/Main/Base/Project/Dom/ModelCollectionChangedEvent.cs
  3. 1
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

38
src/Main/Base/Project/Dom/IModelCollection.cs

@ -7,44 +7,6 @@ using System.Linq;
namespace ICSharpCode.SharpDevelop.Dom namespace ICSharpCode.SharpDevelop.Dom
{ {
/// <summary>
/// Event handler for the <see cref="IModelCollection{T}.CollectionChanged"/> event.
/// </summary>
/// <remarks>
/// We don't use the classic 'EventArgs' model for this event, because a EventArgs-class couldn't be covariant.
/// </remarks>
public delegate void ModelCollectionChangedEventHandler<in T>(IReadOnlyCollection<T> removedItems, IReadOnlyCollection<T> addedItems);
public class ModelCollectionChangedEvent<T>
{
List<ModelCollectionChangedEventHandler<T>> _handlers = new List<ModelCollectionChangedEventHandler<T>>();
public void AddHandler(ModelCollectionChangedEventHandler<T> handler)
{
if (handler != null)
_handlers.Add(handler);
}
public void RemoveHandler(ModelCollectionChangedEventHandler<T> handler)
{
_handlers.Remove(handler);
}
public void Fire(IReadOnlyCollection<T> removedItems, IReadOnlyCollection<T> addedItems)
{
foreach (var handler in _handlers.ToArray()) {
handler(removedItems, addedItems);
}
}
public bool ContainsHandlers
{
get {
return _handlers.Count > 0;
}
}
}
/// <summary> /// <summary>
/// A read-only collection that provides change notifications. /// A read-only collection that provides change notifications.
/// </summary> /// </summary>

52
src/Main/Base/Project/Dom/ModelCollectionChangedEvent.cs

@ -0,0 +1,52 @@
// 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
{
/// <summary>
/// Event handler for the <see cref="IModelCollection{T}.CollectionChanged"/> event.
/// </summary>
/// <remarks>
/// We don't use the classic 'EventArgs' model for this event, because a EventArgs-class couldn't be covariant.
/// </remarks>
public delegate void ModelCollectionChangedEventHandler<in T>(IReadOnlyCollection<T> removedItems, IReadOnlyCollection<T> addedItems);
/// <summary>
/// Helper class for <see cref="IModelCollection.CollectionChanged"/> implementations.
/// This is necessary because <c>Delegate.Combine</c> does not work with
/// co-/contravariant delegates.
/// </summary>
public class ModelCollectionChangedEvent<T>
{
List<ModelCollectionChangedEventHandler<T>> _handlers = new List<ModelCollectionChangedEventHandler<T>>();
public void AddHandler(ModelCollectionChangedEventHandler<T> handler)
{
if (handler != null)
_handlers.Add(handler);
}
public void RemoveHandler(ModelCollectionChangedEventHandler<T> handler)
{
_handlers.Remove(handler);
}
public void Fire(IReadOnlyCollection<T> removedItems, IReadOnlyCollection<T> addedItems)
{
foreach (var handler in _handlers.ToArray()) {
handler(removedItems, addedItems);
}
}
public bool ContainsHandlers {
get {
return _handlers.Count > 0;
}
}
}
}

1
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -111,6 +111,7 @@
<Compile Include="Dom\IModelCollection.cs" /> <Compile Include="Dom\IModelCollection.cs" />
<Compile Include="Dom\ITreeNodeFactory.cs" /> <Compile Include="Dom\ITreeNodeFactory.cs" />
<Compile Include="Dom\KeyedModelCollection.cs" /> <Compile Include="Dom\KeyedModelCollection.cs" />
<Compile Include="Dom\ModelCollectionChangedEvent.cs" />
<Compile Include="Dom\ModelCollectionLinq.cs" /> <Compile Include="Dom\ModelCollectionLinq.cs" />
<Compile Include="Dom\ImmutableModelCollection.cs" /> <Compile Include="Dom\ImmutableModelCollection.cs" />
<Compile Include="Dom\ModelCollectionTreeNode.cs" /> <Compile Include="Dom\ModelCollectionTreeNode.cs" />

Loading…
Cancel
Save