Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3164 was 3019, checked in by swagner, 15 years ago

Restructured persistence code of read-only observable collections (#548)

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