Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3396 was 3390, checked in by swagner, 15 years ago

Refactored HeuristicLab.Collections (#977)

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