Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3555 was 3390, checked in by swagner, 15 years ago

Refactored HeuristicLab.Collections (#977)

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