1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
25 | using System.ComponentModel;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Collections {
|
---|
30 | [Serializable]
|
---|
31 | [StorableClass]
|
---|
32 | public class ReadOnlyObservableDictionary<TKey, TValue> : IObservableDictionary<TKey, TValue> {
|
---|
33 | [Storable]
|
---|
34 | private IObservableDictionary<TKey, TValue> dict;
|
---|
35 |
|
---|
36 | #region Properties
|
---|
37 | public bool ReadOnlyView {
|
---|
38 | get { return true; }
|
---|
39 | set { }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public ICollection<TKey> Keys {
|
---|
43 | get { return dict.Keys; }
|
---|
44 | }
|
---|
45 | public ICollection<TValue> Values {
|
---|
46 | get { return dict.Values; }
|
---|
47 | }
|
---|
48 | public int Count {
|
---|
49 | get { return dict.Count; }
|
---|
50 | }
|
---|
51 | bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
|
---|
52 | get { return true; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public TValue this[TKey key] {
|
---|
56 | get { return dict[key]; }
|
---|
57 | }
|
---|
58 | TValue IDictionary<TKey, TValue>.this[TKey key] {
|
---|
59 | get { return dict[key]; }
|
---|
60 | set { throw new NotSupportedException(); }
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Constructors
|
---|
65 | protected ReadOnlyObservableDictionary() { }
|
---|
66 | public ReadOnlyObservableDictionary(IObservableDictionary<TKey, TValue> dictionary) {
|
---|
67 | if (dictionary == null) throw new ArgumentNullException();
|
---|
68 | dict = dictionary;
|
---|
69 | RegisterEvents();
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | #region Access
|
---|
74 | public bool ContainsKey(TKey key) {
|
---|
75 | return dict.ContainsKey(key);
|
---|
76 | }
|
---|
77 | bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) {
|
---|
78 | return dict.Contains(item);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public bool TryGetValue(TKey key, out TValue value) {
|
---|
82 | return dict.TryGetValue(key, out value);
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | #region Manipulation
|
---|
87 | void IDictionary<TKey, TValue>.Add(TKey key, TValue value) {
|
---|
88 | throw new NotSupportedException();
|
---|
89 | }
|
---|
90 | void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) {
|
---|
91 | throw new NotSupportedException();
|
---|
92 | }
|
---|
93 |
|
---|
94 | bool IDictionary<TKey, TValue>.Remove(TKey key) {
|
---|
95 | throw new NotSupportedException();
|
---|
96 | }
|
---|
97 | bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) {
|
---|
98 | throw new NotSupportedException();
|
---|
99 | }
|
---|
100 |
|
---|
101 | void ICollection<KeyValuePair<TKey, TValue>>.Clear() {
|
---|
102 | throw new NotSupportedException();
|
---|
103 | }
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | #region Conversion
|
---|
107 | void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
|
---|
108 | ((ICollection<KeyValuePair<TKey, TValue>>)dict).CopyTo(array, arrayIndex);
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Enumeration
|
---|
113 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
|
---|
114 | return dict.GetEnumerator();
|
---|
115 | }
|
---|
116 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
117 | return ((IEnumerable)dict).GetEnumerator();
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | #region Events
|
---|
122 | [StorableHook(HookType.AfterDeserialization)]
|
---|
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 |
|
---|
131 | event EventHandler IContent.ReadOnlyViewChanged {
|
---|
132 | add { }
|
---|
133 | remove { }
|
---|
134 | }
|
---|
135 |
|
---|
136 | [field: NonSerialized]
|
---|
137 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsAdded;
|
---|
138 | protected virtual void OnItemsAdded(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
139 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsAdded;
|
---|
140 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
141 | }
|
---|
142 |
|
---|
143 | [field: NonSerialized]
|
---|
144 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsRemoved;
|
---|
145 | protected virtual void OnItemsRemoved(IEnumerable<KeyValuePair<TKey, TValue>> items) {
|
---|
146 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsRemoved;
|
---|
147 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items));
|
---|
148 | }
|
---|
149 |
|
---|
150 | [field: NonSerialized]
|
---|
151 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> ItemsReplaced;
|
---|
152 | protected virtual void OnItemsReplaced(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
153 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = ItemsReplaced;
|
---|
154 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
155 | }
|
---|
156 |
|
---|
157 | [field: NonSerialized]
|
---|
158 | public event CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> CollectionReset;
|
---|
159 | protected virtual void OnCollectionReset(IEnumerable<KeyValuePair<TKey, TValue>> items, IEnumerable<KeyValuePair<TKey, TValue>> oldItems) {
|
---|
160 | CollectionItemsChangedEventHandler<KeyValuePair<TKey, TValue>> handler = CollectionReset;
|
---|
161 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>>(items, oldItems));
|
---|
162 | }
|
---|
163 |
|
---|
164 | [field: NonSerialized]
|
---|
165 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
166 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
167 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
168 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void dict_ItemsAdded(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
172 | OnItemsAdded(e.Items);
|
---|
173 | }
|
---|
174 | private void dict_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
175 | OnItemsRemoved(e.Items);
|
---|
176 | }
|
---|
177 | private void dict_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
178 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
179 | }
|
---|
180 | private void dict_CollectionReset(object sender, CollectionItemsChangedEventArgs<KeyValuePair<TKey, TValue>> e) {
|
---|
181 | OnCollectionReset(e.Items, e.OldItems);
|
---|
182 | }
|
---|
183 | private void dict_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
184 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Keys") || e.PropertyName.Equals("Values") || e.PropertyName.Equals("Count"))
|
---|
185 | OnPropertyChanged(e.PropertyName);
|
---|
186 | }
|
---|
187 | #endregion
|
---|
188 | }
|
---|
189 | }
|
---|