Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3397 was 3390, checked in by swagner, 15 years ago

Refactored HeuristicLab.Collections (#977)

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