Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableCollection.cs @ 3561

Last change on this file since 3561 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: 5.1 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 ReadOnlyObservableCollection<T> : IObservableCollection<T> {
[3560]32    [Storable]
[3390]33    protected IObservableCollection<T> collection;
[2618]34
35    #region Properties
36    public int Count {
37      get { return collection.Count; }
38    }
39    bool ICollection<T>.IsReadOnly {
40      get { return true; }
41    }
42    #endregion
43
44    #region Constructors
[3019]45    protected ReadOnlyObservableCollection() { }
[2618]46    public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
47      if (collection == null) throw new ArgumentNullException();
48      this.collection = collection;
[3002]49      RegisterEvents();
50    }
[3560]51    [StorableConstructor]
52    protected ReadOnlyObservableCollection(bool deserializing) { }
[2618]53    #endregion
54
55    #region Access
56    public bool Contains(T item) {
57      return collection.Contains(item);
58    }
59    #endregion
60
61    #region Manipulation
62    void ICollection<T>.Add(T item) {
63      throw new NotSupportedException();
64    }
65
66    bool ICollection<T>.Remove(T item) {
67      throw new NotSupportedException();
68    }
69
70    void ICollection<T>.Clear() {
71      throw new NotSupportedException();
72    }
73    #endregion
74
75    #region Conversion
76    public void CopyTo(T[] array, int arrayIndex) {
77      collection.CopyTo(array, arrayIndex);
78    }
79    #endregion
80
81    #region Enumeration
82    public IEnumerator<T> GetEnumerator() {
83      return ((IEnumerable<T>)collection).GetEnumerator();
84    }
85    IEnumerator IEnumerable.GetEnumerator() {
86      return ((IEnumerable)collection).GetEnumerator();
87    }
88    #endregion
89
90    #region Events
[3560]91    [StorableHook(HookType.AfterDeserialization)]
[3019]92    protected void RegisterEvents() {
93      collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
94      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
95      collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
96      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
97    }
98
[3317]99
[2618]100    [field: NonSerialized]
101    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
102    protected virtual void OnItemsAdded(IEnumerable<T> items) {
[3317]103      CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
104      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
[2618]105    }
106
107    [field: NonSerialized]
108    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
109    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
[3317]110      CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
111      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
[2618]112    }
113
114    [field: NonSerialized]
115    public event CollectionItemsChangedEventHandler<T> CollectionReset;
116    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
[3317]117      CollectionItemsChangedEventHandler<T> handler = CollectionReset;
118      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
[2618]119    }
120
[2620]121    [field: NonSerialized]
122    public event PropertyChangedEventHandler PropertyChanged;
123    protected virtual void OnPropertyChanged(string propertyName) {
[3317]124      PropertyChangedEventHandler handler = PropertyChanged;
125      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
[2620]126    }
127
[2618]128    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
129      OnItemsAdded(e.Items);
130    }
131    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
132      OnItemsRemoved(e.Items);
133    }
134    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
135      OnCollectionReset(e.Items, e.OldItems);
136    }
[2620]137    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
138      if (e.PropertyName.Equals("Count"))
139        OnPropertyChanged(e.PropertyName);
140    }
[2618]141    #endregion
142  }
143}
Note: See TracBrowser for help on using the repository browser.