Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2636 was 2623, checked in by swagner, 15 years ago

Worked on HeuristicLab.Collections (#819)

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