Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableDictionary.cs @ 3332

Last change on this file since 3332 was 3317, checked in by swagner, 15 years ago

Implemented ReadOnlyView property for items (#969).

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