Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2940 was 2832, checked in by swagner, 15 years ago

Moved CollectionItemsChanged events into own interfaces (#819)

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