Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2636 was 2620, checked in by swagner, 15 years ago

Implemented INotifyPropertyChanged in all observable collections (#819)

File size: 4.7 KB
RevLine 
[2618]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.ObjectModel;
[2620]26using System.ComponentModel;
[2618]27using System.Linq;
28using System.Text;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Collections {
32  [Serializable]
33  public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
34    [Storable]
35    private IObservableCollection<T> collection;
36
37    #region Properties
38    public int Count {
39      get { return collection.Count; }
40    }
41    bool ICollection<T>.IsReadOnly {
42      get { return true; }
43    }
44    #endregion
45
46    #region Constructors
47    public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
48      if (collection == null) throw new ArgumentNullException();
49      this.collection = collection;
50      collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
51      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
52      collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
[2620]53      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
[2618]54    }
55    #endregion
56
57    #region Access
58    public bool Contains(T item) {
59      return collection.Contains(item);
60    }
61    #endregion
62
63    #region Manipulation
64    void ICollection<T>.Add(T item) {
65      throw new NotSupportedException();
66    }
67
68    bool ICollection<T>.Remove(T item) {
69      throw new NotSupportedException();
70    }
71
72    void ICollection<T>.Clear() {
73      throw new NotSupportedException();
74    }
75    #endregion
76
77    #region Conversion
78    public void CopyTo(T[] array, int arrayIndex) {
79      collection.CopyTo(array, arrayIndex);
80    }
81    #endregion
82
83    #region Enumeration
84    public IEnumerator<T> GetEnumerator() {
85      return ((IEnumerable<T>)collection).GetEnumerator();
86    }
87    IEnumerator IEnumerable.GetEnumerator() {
88      return ((IEnumerable)collection).GetEnumerator();
89    }
90    #endregion
91
92    #region Events
93    [field: NonSerialized]
94    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
95    protected virtual void OnItemsAdded(IEnumerable<T> items) {
96      if (ItemsAdded != null)
97        ItemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
98    }
99
100    [field: NonSerialized]
101    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
102    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
103      if (ItemsRemoved != null)
104        ItemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
105    }
106
107    [field: NonSerialized]
108    public event CollectionItemsChangedEventHandler<T> CollectionReset;
109    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
110      if (CollectionReset != null)
111        CollectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
112    }
113
[2620]114    [field: NonSerialized]
115    public event PropertyChangedEventHandler PropertyChanged;
116    protected virtual void OnPropertyChanged(string propertyName) {
117      if (PropertyChanged != null)
118        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
119    }
120
[2618]121    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
122      OnItemsAdded(e.Items);
123    }
124    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
125      OnItemsRemoved(e.Items);
126    }
127    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
128      OnCollectionReset(e.Items, e.OldItems);
129    }
[2620]130    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
131      if (e.PropertyName.Equals("Count"))
132        OnPropertyChanged(e.PropertyName);
133    }
[2618]134    #endregion
135  }
136}
Note: See TracBrowser for help on using the repository browser.