#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Collections { [Serializable] [StorableClass] public class ReadOnlyObservableDictionary : IObservableDictionary { [Storable] private IObservableDictionary dict; #region Properties public bool ReadOnlyView { get { return true; } set { throw new NotSupportedException(); } } public ICollection Keys { get { return dict.Keys; } } public ICollection Values { get { return dict.Values; } } public int Count { get { return dict.Count; } } bool ICollection>.IsReadOnly { get { return true; } } public TValue this[TKey key] { get { return dict[key]; } } TValue IDictionary.this[TKey key] { get { return dict[key]; } set { throw new NotSupportedException(); } } #endregion #region Constructors protected ReadOnlyObservableDictionary() { } public ReadOnlyObservableDictionary(IObservableDictionary dictionary) { if (dictionary == null) throw new ArgumentNullException(); dict = dictionary; RegisterEvents(); } #endregion #region Access public bool ContainsKey(TKey key) { return dict.ContainsKey(key); } bool ICollection>.Contains(KeyValuePair item) { return dict.Contains(item); } public bool TryGetValue(TKey key, out TValue value) { return dict.TryGetValue(key, out value); } #endregion #region Manipulation void IDictionary.Add(TKey key, TValue value) { throw new NotSupportedException(); } void ICollection>.Add(KeyValuePair item) { throw new NotSupportedException(); } bool IDictionary.Remove(TKey key) { throw new NotSupportedException(); } bool ICollection>.Remove(KeyValuePair item) { throw new NotSupportedException(); } void ICollection>.Clear() { throw new NotSupportedException(); } #endregion #region Conversion void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { ((ICollection>)dict).CopyTo(array, arrayIndex); } #endregion #region Enumeration public IEnumerator> GetEnumerator() { return dict.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)dict).GetEnumerator(); } #endregion #region Events [StorableHook(HookType.AfterDeserialization)] protected void RegisterEvents() { dict.ItemsAdded += new CollectionItemsChangedEventHandler>(dict_ItemsAdded); dict.ItemsRemoved += new CollectionItemsChangedEventHandler>(dict_ItemsRemoved); dict.ItemsReplaced += new CollectionItemsChangedEventHandler>(dict_ItemsReplaced); dict.CollectionReset += new CollectionItemsChangedEventHandler>(dict_CollectionReset); dict.PropertyChanged += new PropertyChangedEventHandler(dict_PropertyChanged); } event EventHandler IContent.ReadOnlyViewChanged { add { } remove { } } [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 dict_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { OnItemsAdded(e.Items); } private void dict_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) { OnItemsRemoved(e.Items); } private void dict_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) { OnItemsReplaced(e.Items, e.OldItems); } private void dict_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) { OnCollectionReset(e.Items, e.OldItems); } private void dict_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Keys") || e.PropertyName.Equals("Values") || e.PropertyName.Equals("Count")) OnPropertyChanged(e.PropertyName); } #endregion } }