Add observable collections
This commit is contained in:
@@ -120,7 +120,10 @@
|
||||
<Compile Include="Primitives\Cache.cs" />
|
||||
<Compile Include="Primitives\Cached.cs" />
|
||||
<Compile Include="Primitives\DisposableAction.cs" />
|
||||
<Compile Include="Primitives\IObservableCollection.cs" />
|
||||
<Compile Include="Primitives\Lazy.cs" />
|
||||
<Compile Include="Primitives\ObservableCollection.cs" />
|
||||
<Compile Include="Primitives\ObservableDictionary.cs" />
|
||||
<Compile Include="Primitives\Pair.cs" />
|
||||
<Compile Include="Primitives\PriorityQueue.cs" />
|
||||
<Compile Include="Primitives\Set.cs" />
|
||||
|
||||
25
OpenRA.FileFormats/Primitives/IObservableCollection.cs
Normal file
25
OpenRA.FileFormats/Primitives/IObservableCollection.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace OpenRA.FileFormats.Primitives
|
||||
{
|
||||
public interface IObservableCollection
|
||||
{
|
||||
event Action<object> OnAdd;
|
||||
event Action<object> OnRemove;
|
||||
event Action<int> OnRemoveAt;
|
||||
event Action<object, object> OnSet;
|
||||
event Action OnRefresh;
|
||||
IEnumerable ObservedItems { get; }
|
||||
}
|
||||
}
|
||||
59
OpenRA.FileFormats/Primitives/ObservableCollection.cs
Normal file
59
OpenRA.FileFormats/Primitives/ObservableCollection.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace OpenRA.FileFormats.Primitives
|
||||
{
|
||||
public class ObservableCollection<T> : Collection<T>, IObservableCollection
|
||||
{
|
||||
public event Action<object> OnAdd = k => { };
|
||||
public event Action<object> OnRemove = k => { };
|
||||
public event Action<int> OnRemoveAt = i => { };
|
||||
public event Action<object, object> OnSet = (o, n) => { };
|
||||
public event Action OnRefresh = () => { };
|
||||
|
||||
public ObservableCollection() : base() { }
|
||||
public ObservableCollection(IList<T> list) : base(list) { }
|
||||
|
||||
protected override void SetItem(int index, T item)
|
||||
{
|
||||
var old = this[index];
|
||||
base.SetItem(index, item);
|
||||
OnSet(old, item);
|
||||
}
|
||||
|
||||
protected override void InsertItem(int index, T item)
|
||||
{
|
||||
base.InsertItem(index, item);
|
||||
OnAdd(item);
|
||||
}
|
||||
|
||||
protected override void ClearItems()
|
||||
{
|
||||
base.ClearItems();
|
||||
OnRefresh();
|
||||
}
|
||||
|
||||
protected override void RemoveItem(int index)
|
||||
{
|
||||
base.RemoveItem(index);
|
||||
OnRemoveAt(index);
|
||||
}
|
||||
|
||||
public IEnumerable ObservedItems
|
||||
{
|
||||
get { return base.Items; }
|
||||
}
|
||||
}
|
||||
}
|
||||
137
OpenRA.FileFormats/Primitives/ObservableDictionary.cs
Normal file
137
OpenRA.FileFormats/Primitives/ObservableDictionary.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRA.FileFormats.Primitives
|
||||
{
|
||||
public class ObservableSortedDictionary<TKey, TValue> : ObservableDictionary<TKey, TValue>
|
||||
{
|
||||
public ObservableSortedDictionary(IComparer<TKey> comparer)
|
||||
{
|
||||
InnerDict = new SortedDictionary<TKey, TValue>(comparer);
|
||||
}
|
||||
|
||||
public override void Add(TKey key, TValue value)
|
||||
{
|
||||
InnerDict.Add(key, value);
|
||||
FireOnRefresh();
|
||||
}
|
||||
}
|
||||
|
||||
public class ObservableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IObservableCollection
|
||||
{
|
||||
protected IDictionary<TKey, TValue> InnerDict;
|
||||
|
||||
public event Action<object> OnAdd = k => { };
|
||||
public event Action<object> OnRemove = k => { };
|
||||
public event Action<int> OnRemoveAt = i => { };
|
||||
public event Action<object, object> OnSet = (o, n) => { };
|
||||
public event Action OnRefresh = () => { };
|
||||
|
||||
protected void FireOnRefresh()
|
||||
{
|
||||
OnRefresh();
|
||||
}
|
||||
|
||||
protected ObservableDictionary() { }
|
||||
|
||||
public ObservableDictionary(IEqualityComparer<TKey> comparer)
|
||||
{
|
||||
InnerDict = new Dictionary<TKey, TValue>(comparer);
|
||||
}
|
||||
|
||||
public virtual void Add(TKey key, TValue value)
|
||||
{
|
||||
InnerDict.Add(key, value);
|
||||
OnAdd(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
var found = InnerDict.Remove(key);
|
||||
if (found)
|
||||
OnRemove(key);
|
||||
return found;
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return InnerDict.ContainsKey(key);
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys { get { return InnerDict.Keys; } }
|
||||
public ICollection<TValue> Values { get { return InnerDict.Values; } }
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return InnerDict.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get { return InnerDict[key]; }
|
||||
set { InnerDict[key] = value; }
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
InnerDict.Clear();
|
||||
OnRefresh();
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return InnerDict.Count; }
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
Add(item.Key, item.Value);
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return InnerDict.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
InnerDict.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool IsReadOnly
|
||||
{
|
||||
get { return InnerDict.IsReadOnly; }
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return Remove(item.Key);
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return InnerDict.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return InnerDict.GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerable ObservedItems
|
||||
{
|
||||
get { return InnerDict.Keys; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user