Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3368 was 3368, checked in by swagner, 14 years ago

Adaptions according to the new IContent interface (#969).

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