#region License Information /* HeuristicLab * Copyright (C) 2002-2008 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.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Collections { [Serializable] public class ReadOnlyObservableCollection : IObservableCollection { [Storable] private IObservableCollection collection; #region Properties public int Count { get { return collection.Count; } } bool ICollection.IsReadOnly { get { return true; } } #endregion #region Constructors public ReadOnlyObservableCollection(IObservableCollection collection) { if (collection == null) throw new ArgumentNullException(); this.collection = collection; collection.ItemsAdded += new CollectionItemsChangedEventHandler(collection_ItemsAdded); collection.ItemsRemoved += new CollectionItemsChangedEventHandler(collection_ItemsRemoved); collection.CollectionReset += new CollectionItemsChangedEventHandler(collection_CollectionReset); collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged); } #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 [field: NonSerialized] public event CollectionItemsChangedEventHandler ItemsAdded; protected virtual void OnItemsAdded(IEnumerable items) { if (ItemsAdded != null) ItemsAdded(this, new CollectionItemsChangedEventArgs(items)); } [field: NonSerialized] public event CollectionItemsChangedEventHandler ItemsRemoved; protected virtual void OnItemsRemoved(IEnumerable items) { if (ItemsRemoved != null) ItemsRemoved(this, new CollectionItemsChangedEventArgs(items)); } [field: NonSerialized] public event CollectionItemsChangedEventHandler CollectionReset; protected virtual void OnCollectionReset(IEnumerable items, IEnumerable oldItems) { if (CollectionReset != null) CollectionReset(this, new CollectionItemsChangedEventArgs(items, oldItems)); } [field: NonSerialized] public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(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 } }