[2618] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2618] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
[2620] | 25 | using System.ComponentModel;
|
---|
[3560] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2618] | 27 |
|
---|
| 28 | namespace HeuristicLab.Collections {
|
---|
[3560] | 29 | [StorableClass]
|
---|
[2618] | 30 | [Serializable]
|
---|
| 31 | public class ReadOnlyObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
|
---|
[3560] | 32 | [Storable]
|
---|
[3390] | 33 | protected IObservableDictionary<TKey, TValue> dict;
|
---|
[2618] | 34 |
|
---|
| 35 | #region Properties
|
---|
| 36 | public ICollection<TKey> Keys {
|
---|
| 37 | get { return dict.Keys; }
|
---|
| 38 | }
|
---|
| 39 | public ICollection<TValue> Values {
|
---|
| 40 | get { return dict.Values; }
|
---|
| 41 | }
|
---|
| 42 | public int Count {
|
---|
| 43 | get { return dict.Count; }
|
---|
| 44 | }
|
---|
| 45 | bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
|
---|
| 46 | get { return true; }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public TValue this[TKey key] {
|
---|
| 50 | get { return dict[key]; }
|
---|
| 51 | }
|
---|
| 52 | TValue IDictionary<TKey, TValue>.this[TKey key] {
|
---|
| 53 | get { return dict[key]; }
|
---|
| 54 | set { throw new NotSupportedException(); }
|
---|
| 55 | }
|
---|
| 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | #region Constructors
|
---|
[3019] | 59 | protected ReadOnlyObservableDictionary() { }
|
---|
[2618] | 60 | public ReadOnlyObservableDictionary(IObservableDictionary<TKey, TValue> dictionary) {
|
---|
| 61 | if (dictionary == null) throw new ArgumentNullException();
|
---|
| 62 | dict = dictionary;
|
---|
[3002] | 63 | RegisterEvents();
|
---|
| 64 | }
|
---|
[3560] | 65 | [StorableConstructor]
|
---|
| 66 | protected ReadOnlyObservableDictionary(bool deserializing) { }
|
---|
[4722] | 67 |
|
---|
| 68 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 69 | private void AfterDeserialization() {
|
---|
| 70 | RegisterEvents();
|
---|
| 71 | }
|
---|
[2618] | 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region Access
|
---|
| 75 | public bool ContainsKey(TKey key) {
|
---|
| 76 | return dict.ContainsKey(key);
|
---|
| 77 | }
|
---|
| 78 | bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
|
---|
| 79 | return dict.Contains(item);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public bool TryGetValue(TKey key, out TValue value) {
|
---|
| 83 | return dict.TryGetValue(key, out value);
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 |
|
---|
| 87 | #region Manipulation
|
---|
| 88 | void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
|
---|
| 89 | throw new NotSupportedException();
|
---|
| 90 | }
|
---|
| 91 | void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
|
---|
| 92 | throw new NotSupportedException();
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | bool IDictionary<TKey, TValue>.Remove(TKey key) {
|
---|
| 96 | throw new NotSupportedException();
|
---|
| 97 | }
|
---|
| 98 | bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
|
---|
| 99 | throw new NotSupportedException();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
|
---|
| 103 | throw new NotSupportedException();
|
---|
| 104 | }
|
---|
| 105 | #endregion
|
---|
| 106 |
|
---|
| 107 | #region Conversion
|
---|
| 108 | void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
---|
| 109 | ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
|
---|
| 110 | }
|
---|
| 111 | #endregion
|
---|
| 112 |
|
---|
| 113 | #region Enumeration
|
---|
| 114 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
---|
| 115 | return dict.GetEnumerator();
|
---|
| 116 | }
|
---|
| 117 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 118 | return ((IEnumerable)dict).GetEnumerator();
|
---|
| 119 | }
|
---|
| 120 | #endregion
|
---|
| 121 |
|
---|
| 122 | #region Events
|
---|
[3019] | 123 | protected void RegisterEvents() {
|
---|
| 124 | dict.ItemsAdded += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsAdded);
|
---|
| 125 | dict.ItemsRemoved += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsRemoved);
|
---|
| 126 | dict.ItemsReplaced += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_ItemsReplaced);
|
---|
| 127 | dict.CollectionReset += new CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>>(dict_CollectionReset);
|
---|
| 128 | dict.PropertyChanged += new PropertyChangedEventHandler(dict_PropertyChanged);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[2618] | 131 | [field: NonSerialized]
|
---|
| 132 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
|
---|
| 133 | protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
[3317] | 134 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsAdded;
|
---|
| 135 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
[2618] | 136 | }
|
---|
| 137 |
|
---|
| 138 | [field: NonSerialized]
|
---|
| 139 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
|
---|
| 140 | protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
[3317] | 141 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsRemoved;
|
---|
| 142 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
[2618] | 143 | }
|
---|
| 144 |
|
---|
| 145 | [field: NonSerialized]
|
---|
| 146 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
|
---|
| 147 | protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
[3317] | 148 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsReplaced;
|
---|
| 149 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
[2618] | 150 | }
|
---|
| 151 |
|
---|
| 152 | [field: NonSerialized]
|
---|
| 153 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
|
---|
| 154 | protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
[3317] | 155 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = CollectionReset;
|
---|
| 156 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
[2618] | 157 | }
|
---|
| 158 |
|
---|
[2620] | 159 | [field: NonSerialized]
|
---|
| 160 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 161 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 162 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 163 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 164 | }
|
---|
| 165 |
|
---|
[2618] | 166 | private void dict_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
| 167 | OnItemsAdded(e.Items);
|
---|
| 168 | }
|
---|
| 169 | private void dict_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
| 170 | OnItemsRemoved(e.Items);
|
---|
| 171 | }
|
---|
| 172 | private void dict_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
| 173 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
| 174 | }
|
---|
| 175 | private void dict_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
| 176 | OnCollectionReset(e.Items, e.OldItems);
|
---|
| 177 | }
|
---|
[2620] | 178 | private void dict_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 179 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Keys") || e.PropertyName.Equals("Values") || e.PropertyName.Equals("Count"))
|
---|
| 180 | OnPropertyChanged(e.PropertyName);
|
---|
| 181 | }
|
---|
[2618] | 182 | #endregion
|
---|
| 183 | }
|
---|
| 184 | }
|
---|