Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableList.cs @ 3653

Last change on this file since 3653 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: 9.3 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.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    #endregion
62
63    #region Access
64    public bool Contains(T item) {
65      return list.Contains(item);
66    }
67
68    public int IndexOf(T item) {
69      return list.IndexOf(item);
70    }
71    #endregion
72
73    #region Manipulation
74    void ICollection<T>.Add(T item) {
75      throw new NotSupportedException();
76    }
77
78    void IList<T>.Insert(int index, T item) {
79      throw new NotSupportedException();
80    }
81
82    bool ICollection<T>.Remove(T item) {
83      throw new NotSupportedException();
84    }
85    void IList<T>.RemoveAt(int index) {
86      throw new NotSupportedException();
87    }
88
89    void ICollection<T>.Clear() {
90      throw new NotSupportedException();
91    }
92    #endregion
93
94    #region Conversion
95    public void CopyTo(T[] array, int arrayIndex) {
96      list.CopyTo(array, arrayIndex);
97    }
98    #endregion
99
100    #region Enumeration
101    public IEnumerator<T> GetEnumerator() {
102      return ((ICollection<T>)list).GetEnumerator();
103    }
104    IEnumerator IEnumerable.GetEnumerator() {
105      return ((IEnumerable)list).GetEnumerator();
106    }
107    #endregion
108
109    #region Events
110    [StorableHook(HookType.AfterDeserialization)]
111    protected void RegisterEvents() {
112      list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
113      ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
114      list.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsRemoved);
115      ((IObservableCollection<T>)list).ItemsRemoved += new CollectionItemsChangedEventHandler<T>(list_ItemsRemoved);
116      list.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsReplaced);
117      list.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsMoved);
118      list.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_CollectionReset);
119      ((IObservableCollection<T>)list).CollectionReset += new CollectionItemsChangedEventHandler<T>(list_CollectionReset);
120      list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
121    }
122
123    [field: NonSerialized]
124    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
125    protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
126      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsAdded;
127      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
128    }
129
130    [field: NonSerialized]
131    private event CollectionItemsChangedEventHandler<T> itemsAdded;
132    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
133      add { itemsAdded += value; }
134      remove { itemsAdded -= value; }
135    }
136    private void OnItemsAdded(IEnumerable<T> items) {
137      CollectionItemsChangedEventHandler<T> handler = itemsAdded;
138      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
139    }
140
141    [field: NonSerialized]
142    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
143    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
144      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsRemoved;
145      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
146    }
147
148    [field: NonSerialized]
149    private event CollectionItemsChangedEventHandler<T> itemsRemoved;
150    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
151      add { itemsRemoved += value; }
152      remove { itemsRemoved -= value; }
153    }
154    private void OnItemsRemoved(IEnumerable<T> items) {
155      CollectionItemsChangedEventHandler<T> handler = itemsRemoved;
156      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
157    }
158
159    [field: NonSerialized]
160    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
161    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
162      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
163      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
164    }
165
166    [field: NonSerialized]
167    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
168    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
169      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
170      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
171    }
172
173    [field: NonSerialized]
174    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
175    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
176      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
177      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
178    }
179
180    [field: NonSerialized]
181    private event CollectionItemsChangedEventHandler<T> collectionReset;
182    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
183      add { collectionReset += value; }
184      remove { collectionReset -= value; }
185    }
186    private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
187      CollectionItemsChangedEventHandler<T> handler = collectionReset;
188      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
189    }
190
191    [field: NonSerialized]
192    public event PropertyChangedEventHandler PropertyChanged;
193    protected virtual void OnPropertyChanged(string propertyName) {
194      PropertyChangedEventHandler handler = PropertyChanged;
195      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
196    }
197
198    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
199      OnItemsAdded(e.Items);
200    }
201    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
202      OnItemsAdded(e.Items);
203    }
204    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
205      OnItemsRemoved(e.Items);
206    }
207    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
208      OnItemsRemoved(e.Items);
209    }
210    private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
211      OnItemsReplaced(e.Items, e.OldItems);
212    }
213    private void list_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
214      OnItemsMoved(e.Items, e.OldItems);
215    }
216    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
217      OnCollectionReset(e.Items, e.OldItems);
218    }
219    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
220      OnCollectionReset(e.Items, e.OldItems);
221    }
222    private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
223      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
224        OnPropertyChanged(e.PropertyName);
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.