Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11143 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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