Free cookie consent management tool by TermsFeed Policy Generator

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