Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedCollection.cs @ 3021

Last change on this file since 3021 was 3019, checked in by swagner, 15 years ago

Restructured persistence code of read-only observable collections (#548)

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