Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4023 was 3560, checked in by swagner, 15 years ago

Fixed persistence exceptions by restoring the reference on HeuristicLab.Persistence in HeuristicLab.Collections (#977)

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