Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4502 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
RevLine 
[2618]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2618]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;
[2620]25using System.ComponentModel;
[3560]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2618]27
28namespace HeuristicLab.Collections {
[3560]29  [StorableClass]
[2618]30  [Serializable]
31  public class ReadOnlyObservableList<T> : IObservableList<T> {
[3560]32    [Storable]
[3390]33    protected IObservableList<T> list;
[2618]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
[3019]53    protected ReadOnlyObservableList() { }
[2618]54    public ReadOnlyObservableList(IObservableList<T> list) {
55      if (list == null) throw new ArgumentNullException();
56      this.list = list;
[3002]57      RegisterEvents();
58    }
[3560]59    [StorableConstructor]
60    protected ReadOnlyObservableList(bool deserializing) { }
[2618]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
[2623]95    public void CopyTo(T[] array, int arrayIndex) {
[2618]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
[3560]110    [StorableHook(HookType.AfterDeserialization)]
[3019]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
[2618]123    [field: NonSerialized]
124    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
125    protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
[3317]126      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsAdded;
127      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
[2618]128    }
129
130    [field: NonSerialized]
[2623]131    private event CollectionItemsChangedEventHandler<T> itemsAdded;
[2832]132    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
[2623]133      add { itemsAdded += value; }
134      remove { itemsAdded -= value; }
135    }
136    private void OnItemsAdded(IEnumerable<T> items) {
[3317]137      CollectionItemsChangedEventHandler<T> handler = itemsAdded;
138      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
[2623]139    }
140
141    [field: NonSerialized]
[2618]142    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
143    protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
[3317]144      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsRemoved;
145      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
[2618]146    }
147
148    [field: NonSerialized]
[2623]149    private event CollectionItemsChangedEventHandler<T> itemsRemoved;
[2832]150    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
[2623]151      add { itemsRemoved += value; }
152      remove { itemsRemoved -= value; }
153    }
154    private void OnItemsRemoved(IEnumerable<T> items) {
[3317]155      CollectionItemsChangedEventHandler<T> handler = itemsRemoved;
156      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
[2623]157    }
158
159    [field: NonSerialized]
[2618]160    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
161    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
[3317]162      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
163      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
[2618]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) {
[3317]169      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
170      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
[2618]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) {
[3317]176      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
177      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
[2618]178    }
179
[2620]180    [field: NonSerialized]
[2623]181    private event CollectionItemsChangedEventHandler<T> collectionReset;
[2832]182    event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
[2623]183      add { collectionReset += value; }
184      remove { collectionReset -= value; }
185    }
186    private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
[3317]187      CollectionItemsChangedEventHandler<T> handler = collectionReset;
188      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
[2623]189    }
190
191    [field: NonSerialized]
[2620]192    public event PropertyChangedEventHandler PropertyChanged;
193    protected virtual void OnPropertyChanged(string propertyName) {
[3317]194      PropertyChangedEventHandler handler = PropertyChanged;
195      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
[2620]196    }
197
[2618]198    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
199      OnItemsAdded(e.Items);
200    }
[2623]201    private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
202      OnItemsAdded(e.Items);
203    }
[2618]204    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
205      OnItemsRemoved(e.Items);
206    }
[2623]207    private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
208      OnItemsRemoved(e.Items);
209    }
[2618]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    }
[2623]219    private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
220      OnCollectionReset(e.Items, e.OldItems);
221    }
[2620]222    private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
223      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
224        OnPropertyChanged(e.PropertyName);
225    }
[2618]226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.