#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Collections {
[StorableType("982065AC-FCD5-4FBE-A5B4-55C0EAB21C56")]
[Serializable]
public class ReadOnlyObservableKeyedCollection : IObservableKeyedCollection {
[Storable]
protected IObservableKeyedCollection collection;
#region Properties
public int Count {
get { return collection.Count; }
}
bool ICollection.IsReadOnly {
get { return true; }
}
public TItem this[TKey key] {
get {
return collection[key];
}
}
#endregion
#region Constructors
protected ReadOnlyObservableKeyedCollection() { }
public ReadOnlyObservableKeyedCollection(IObservableKeyedCollection collection) {
if (collection == null) throw new ArgumentNullException();
this.collection = collection;
RegisterEvents();
}
[StorableConstructor]
protected ReadOnlyObservableKeyedCollection(bool deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#endregion
#region Access
public bool ContainsKey(TKey key) {
return collection.ContainsKey(key);
}
public bool Contains(TItem item) {
return collection.Contains(item);
}
public bool TryGetValue(TKey key, out TItem item) {
return collection.TryGetValue(key, out item);
}
#endregion
#region Manipulation
void ICollection.Add(TItem item) {
throw new NotSupportedException();
}
bool IObservableKeyedCollection.Remove(TKey key) {
throw new NotSupportedException();
}
bool ICollection.Remove(TItem item) {
throw new NotSupportedException();
}
void ICollection.Clear() {
throw new NotSupportedException();
}
#endregion
#region Conversion
public void CopyTo(TItem[] array, int arrayIndex) {
collection.CopyTo(array, arrayIndex);
}
#endregion
#region Enumeration
public IEnumerator GetEnumerator() {
return ((IEnumerable)collection).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable)collection).GetEnumerator();
}
#endregion
#region Events
protected void RegisterEvents() {
collection.ItemsAdded += new CollectionItemsChangedEventHandler(collection_ItemsAdded);
collection.ItemsRemoved += new CollectionItemsChangedEventHandler(collection_ItemsRemoved);
collection.ItemsReplaced += new CollectionItemsChangedEventHandler(collection_ItemsReplaced);
collection.CollectionReset += new CollectionItemsChangedEventHandler(collection_CollectionReset);
collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler ItemsAdded;
protected virtual void OnItemsAdded(IEnumerable items) {
CollectionItemsChangedEventHandler handler = ItemsAdded;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler ItemsRemoved;
protected virtual void OnItemsRemoved(IEnumerable items) {
CollectionItemsChangedEventHandler handler = ItemsRemoved;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler ItemsReplaced;
protected virtual void OnItemsReplaced(IEnumerable items, IEnumerable oldItems) {
CollectionItemsChangedEventHandler handler = ItemsReplaced;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items, oldItems));
}
[field: NonSerialized]
public event CollectionItemsChangedEventHandler CollectionReset;
protected virtual void OnCollectionReset(IEnumerable items, IEnumerable oldItems) {
CollectionItemsChangedEventHandler handler = CollectionReset;
if (handler != null) handler(this, new CollectionItemsChangedEventArgs(items, oldItems));
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
OnItemsAdded(e.Items);
}
private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
OnItemsRemoved(e.Items);
}
private void collection_ItemsReplaced(object sender, CollectionItemsChangedEventArgs e) {
OnItemsReplaced(e.Items, e.OldItems);
}
private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
OnCollectionReset(e.Items, e.OldItems);
}
private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
OnPropertyChanged(e.PropertyName);
}
#endregion
}
}