#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 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;
namespace HeuristicLab.Collections {
[StorableType("2a66af94-ed89-4d97-bd41-33f8365b99cf")]
[Serializable]
public class ReadOnlyObservableArray : IObservableArray {
[Storable]
protected IObservableArray array;
#region Properties
public int Length {
get { return array.Length; }
}
int ICollection.Count {
get { return array.Count; }
}
bool ICollection.IsReadOnly {
get { return true; }
}
public T this[int index] {
get { return array[index]; }
}
T IList.this[int index] {
get { return array[index]; }
set { throw new NotSupportedException(); }
}
#endregion
#region Constructors
protected ReadOnlyObservableArray() { }
public ReadOnlyObservableArray(IObservableArray array) {
if (array == null) throw new ArgumentNullException();
this.array = array;
RegisterEvents();
}
[StorableConstructor]
protected ReadOnlyObservableArray(StorableConstructorFlag deserializing) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterEvents();
}
#endregion
#region Access
public bool Contains(T item) {
return array.Contains(item);
}
public int IndexOf(T item) {
return array.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 IObservableArray.Reverse() {
throw new NotSupportedException();
}
void IObservableArray.Reverse(int index, int count) {
throw new NotSupportedException();
}
#endregion
#region Conversion
public void CopyTo(T[] array, int arrayIndex) {
this.array.CopyTo(array, arrayIndex);
}
#endregion
#region Enumeration
public IEnumerator GetEnumerator() {
return array.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return array.GetEnumerator();
}
#endregion
#region Events
protected void RegisterEvents() {
array.ItemsReplaced += new CollectionItemsChangedEventHandler>(array_ItemsReplaced);
array.ItemsMoved += new CollectionItemsChangedEventHandler>(array_ItemsMoved);
array.CollectionReset += new CollectionItemsChangedEventHandler>(array_CollectionReset);
array.PropertyChanged += new PropertyChangedEventHandler(array_PropertyChanged);
}
[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]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
private void array_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsReplaced(e.Items, e.OldItems);
}
private void array_ItemsMoved(object sender, CollectionItemsChangedEventArgs> e) {
OnItemsMoved(e.Items, e.OldItems);
}
private void array_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
OnCollectionReset(e.Items, e.OldItems);
}
private void array_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Length"))
OnPropertyChanged(e.PropertyName);
}
#endregion
}
}