Free cookie consent management tool by TermsFeed Policy Generator

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

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

Make StorableClass attribute compulsory for StorableSerializer to work, add named property StorableClassType to choose between Empty and MarkedOnly, later other options will be added. (#548)

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