#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("5C3B9621-71C1-40F0-8E1C-31B9AD039E2B")]
[Serializable]
public class ReadOnlyObservableCollection : IObservableCollection {
[Storable]
protected IObservableCollection collection;
#region Properties
public int Count {
get { return collection.Count; }
}
bool ICollection.IsReadOnly {
get { return true; }
}
#endregion
#region Constructors
protected ReadOnlyObservableCollection() { }
public ReadOnlyObservableCollection(IObservableCollection collection) {
if (collection == null) throw new ArgumentNullException();
this.collection = collection;
RegisterEvents();
}
[StorableConstructor]
protected ReadOnlyObservableCollection(bool deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#endregion
#region Access
public bool Contains(T item) {
return collection.Contains(item);
}
#endregion
#region Manipulation
void ICollection.Add(T item) {
throw new NotSupportedException();
}
bool ICollection.Remove(T item) {
throw new NotSupportedException();
}
void ICollection.Clear() {
throw new NotSupportedException();
}
#endregion
#region Conversion
public void CopyTo(T[] 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.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 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_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
OnCollectionReset(e.Items, e.OldItems);
}
private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName.Equals("Count"))
OnPropertyChanged(e.PropertyName);
}
#endregion
}
}