Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2916_IndexedDataTableSerialization/HeuristicLab.Collections/3.3/ReadOnlyObservableList.cs @ 16091

Last change on this file since 16091 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Collections {
29  [StorableClass]
30  [Serializable]
31  public class ReadOnlyObservableList<T> : IObservableList<T> {
32    [Storable]
33    protected IObservableList<T> list;
34
35    #region Properties
36    public int Count {
37      get { return ((ICollection<T>)list).Count; }
38    }
39    bool ICollection<T>.IsReadOnly {
40      get { return true; }
41    }
42
43    public T this[int index] {
44      get { return list[index]; }
45    }
46    T IList<T>.this[int index] {
47      get { return list[index]; }
48      set { throw new NotSupportedException(); }
49    }
50    #endregion
51
52    #region Constructors
53    protected ReadOnlyObservableList() { }
54    public ReadOnlyObservableList(IObservableList<T> list) {
55      if (list == null) throw new ArgumentNullException();
56      this.list = list;
57      RegisterEvents();
58    }
59    [StorableConstructor]
60    protected ReadOnlyObservableList(bool deserializing) { }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      RegisterEvents();
65    }
66    #endregion
67
68    #region Access
69    public bool Contains(T item) {
70      return list.Contains(item);
71    }
72
73    public int IndexOf(T item) {
74      return list.IndexOf(item);
75    }
76    #endregion
77
78    #region Manipulation
79    void ICollection<T>.Add(T item) {
80      throw new NotSupportedException();
81    }
82
83    void IList<T>.Insert(int index, T item) {
84      throw new NotSupportedException();
85    }
86
87    bool ICollection<T>.Remove(T item) {
88      throw new NotSupportedException();
89    }
90    void IList<T>.RemoveAt(int index) {
91      throw new NotSupportedException();
92    }
93
94    void ICollection<T>.Clear() {
95      throw new NotSupportedException();
96    }
97
98    void IObservableList<T>.Reverse() {
99      throw new NotSupportedException();
100    }
101    void IObservableList<T>.Reverse(int index, int count) {
102      throw new NotSupportedException();
103    }
104    #endregion
105
106    #region Conversion
107    public void CopyTo(T[] array, int arrayIndex) {
108      list.CopyTo(array, arrayIndex);
109    }
110    #endregion
111
112    #region Enumeration
113    public IEnumerator<T> GetEnumerator() {
114      return ((ICollection<T>)list).GetEnumerator();
115    }
116    IEnumerator IEnumerable.GetEnumerator() {
117      return ((IEnumerable)list).GetEnumerator();
118    }
119    #endregion
120
121    #region Events
122    private void RegisterEvents() {
123      list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
124      ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
125      list.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsRemoved);
126      ((IObservableCollection<T>)list).ItemsRemoved += new CollectionItemsChangedEventHandler<T>(list_ItemsRemoved);
127      list.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsReplaced);
128      list.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsMoved);
129      list.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_CollectionReset);
130      ((IObservableCollection<T>)list).CollectionReset += new CollectionItemsChangedEventHandler<T>(list_CollectionReset);
131      list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
132    }
133
134    [field: NonSerialized]
135    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
136    protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
137      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsAdded;
138      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
139    }
140
141    [field: NonSerialized]
142    private event CollectionItemsChangedEventHandler<T> itemsAdded;
143    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
144      add { itemsAdded += value; }
145      remove { itemsAdded -= value; }
146    }
147    private void OnItemsAdded(IEnumerable<T> items) {
148      CollectionItemsChangedEventHandler<T> handler = itemsAdded;
149      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
150    }
151
152    [field: NonSerialized]
153    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
154    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
155      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsRemoved;
156      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
157    }
158
159    [field: NonSerialized]
160    private event CollectionItemsChangedEventHandler<T> itemsRemoved;
161    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
162      add { itemsRemoved += value; }
163      remove { itemsRemoved -= value; }
164    }
165    private void OnItemsRemoved(IEnumerable<T> items) {
166      CollectionItemsChangedEventHandler<T> handler = itemsRemoved;
167      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
168    }
169
170    [field: NonSerialized]
171    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
172    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
173      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
174      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
175    }
176
177    [field: NonSerialized]
178    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
179    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
180      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
181      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
182    }
183
184    [field: NonSerialized]
185    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
186    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
187      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
188      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
189    }
190
191    [field: NonSerialized]
192    private event CollectionItemsChangedEventHandler<T> collectionReset;
193    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
194      add { collectionReset += value; }
195      remove { collectionReset -= value; }
196    }
197    private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
198      CollectionItemsChangedEventHandler<T> handler = collectionReset;
199      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
200    }
201
202    [field: NonSerialized]
203    public event PropertyChangedEventHandler PropertyChanged;
204    protected virtual void OnPropertyChanged(string propertyName) {
205      PropertyChangedEventHandler handler = PropertyChanged;
206      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
207    }
208
209    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
210      OnItemsAdded(e.Items);
211    }
212    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
213      OnItemsAdded(e.Items);
214    }
215    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
216      OnItemsRemoved(e.Items);
217    }
218    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
219      OnItemsRemoved(e.Items);
220    }
221    private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
222      OnItemsReplaced(e.Items, e.OldItems);
223    }
224    private void list_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
225      OnItemsMoved(e.Items, e.OldItems);
226    }
227    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
228      OnCollectionReset(e.Items, e.OldItems);
229    }
230    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
231      OnCollectionReset(e.Items, e.OldItems);
232    }
233    private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
234      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
235        OnPropertyChanged(e.PropertyName);
236    }
237    #endregion
238  }
239}
Note: See TracBrowser for help on using the repository browser.