#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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 {
[StorableClass]
[Serializable]
public class ReadOnlyObservableList : IObservableList {
[Storable]
protected IObservableList list;
#region Properties
public int Count {
get { return ((ICollection)list).Count; }
}
bool ICollection.IsReadOnly {
get { return true; }
}
public T this[int index] {
get { return list[index]; }
}
T IList.this[int index] {
get { return list[index]; }
set { throw new NotSupportedException(); }
}
#endregion
#region Constructors
protected ReadOnlyObservableList() { }
public ReadOnlyObservableList(IObservableList list) {
if (list == null) throw new ArgumentNullException();
this.list = list;
RegisterEvents();
}
[StorableConstructor]
protected ReadOnlyObservableList(bool deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#endregion
#region Access
public bool Contains(T item) {
return list.Contains(item);
}
public int IndexOf(T item) {
return list.IndexOf(item);
}
#endregion
#region Manipulation
void ICollection.Add(T item) {
throw new NotSupportedException();
}
void IList.Insert(int index, T item) {
throw new NotSupportedException();
}
bool ICollection.Remove(T item) {
throw new NotSupportedException();
}
void IList.RemoveAt(int index) {
throw new NotSupportedException();
}
void ICollection.Clear() {
throw new NotSupportedException();
}
void IObservableList.Reverse() {
throw new NotSupportedException();
}
void IObservableList.Reverse(int index, int count) {
throw new NotSupportedException();
}
#endregion
#region Conversion
public void CopyTo(T[] array, int arrayIndex) {
list.CopyTo(array, arrayIndex);
}
#endregion
#region Enumeration
public IEnumerator GetEnumerator() {
return ((ICollection)list).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return ((IEnumerable)list).GetEnumerator();
}
#endregion
#region Events
protected void RegisterEvents() {
list.ItemsAdded += new CollectionItemsChangedEventHandler>(list_ItemsAdded);
((IObservableCollection)list).ItemsAdded += new CollectionItemsChangedEventHandler(list_ItemsAdded);
list.ItemsRemoved += new CollectionItemsChangedEventHandler>(list_ItemsRemoved);
((IObservableCollection)list).ItemsRemoved += new CollectionItemsChangedEventHandler(list_ItemsRemoved);
list.ItemsReplaced += new CollectionItemsChangedEventHandler>(list_ItemsReplaced);
list.ItemsMoved += new CollectionItemsChangedEventHandler>(list_ItemsMoved);
list.CollectionReset += new CollectionItemsChangedEventHandler>(list_CollectionReset);
((IObservableCollection)list).CollectionReset += new CollectionItemsChangedEventHandler(list_CollectionReset);
list.PropertyChanged += new PropertyChangedEventHandler(list_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]
private event CollectionItemsChangedEventHandler itemsAdded;
event CollectionItemsChangedEventHandler INotifyObservableCollectionItemsChanged.ItemsAdded {
add { itemsAdded += value; }
remove { itemsAdded -= value; }
}
private 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]
private event CollectionItemsChangedEventHandler itemsRemoved;
event CollectionItemsChangedEventHandler INotifyObservableCollectionItemsChanged.ItemsRemoved {
add { itemsRemoved += value; }
remove { itemsRemoved -= value; }
}
private 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> ItemsMoved;
protected virtual void OnItemsMoved(IEnumerable> items, IEnumerable> oldItems) {
CollectionItemsChangedEventHandler> handler = ItemsMoved;
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]
private event CollectionItemsChangedEventHandler collectionReset;
event CollectionItemsChangedEventHandler INotifyObservableCollectionItemsChanged.CollectionReset {
add { collectionReset += value; }
remove { collectionReset -= value; }
}
private 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 list_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsAdded(e.Items);
}
private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
OnItemsAdded(e.Items);
}
private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsRemoved(e.Items);
}
private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
OnItemsRemoved(e.Items);
}
private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsReplaced(e.Items, e.OldItems);
}
private void list_ItemsMoved(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsMoved(e.Items, e.OldItems);
}
private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
OnCollectionReset(e.Items, e.OldItems);
}
private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
OnCollectionReset(e.Items, e.OldItems);
}
private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
OnPropertyChanged(e.PropertyName);
}
#endregion
}
}