Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3017 was 3017, checked in by epitzer, 14 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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