Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored HeuristicLab.Collections (#977)

File size: 5.6 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 ReadOnlyObservableArray<T> : IObservableArray<T> {
30    protected IObservableArray<T> array;
31
32    #region Properties
33    public int Length {
34      get { return array.Length; }
35    }
36    int ICollection<T>.Count {
37      get { return array.Count; }
38    }
39    bool ICollection<T>.IsReadOnly {
40      get { return true; }
41    }
42
43    public T this[int index] {
44      get { return array[index]; }
45    }
46    T IList<T>.this[int index] {
47      get { return array[index]; }
48      set { throw new NotSupportedException(); }
49    }
50    #endregion
51
52    #region Constructors
53    protected ReadOnlyObservableArray() { }
54    public ReadOnlyObservableArray(IObservableArray<T> array) {
55      if (array == null) throw new ArgumentNullException();
56      this.array = array;
57      RegisterEvents();
58    }
59    #endregion
60
61    #region Access
62    public bool Contains(T item) {
63      return array.Contains(item);
64    }
65
66    public int IndexOf(T item) {
67      return array.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      this.array.CopyTo(array, arrayIndex);
95    }
96    #endregion
97
98    #region Enumeration
99    public IEnumerator<T> GetEnumerator() {
100      return array.GetEnumerator();
101    }
102    IEnumerator IEnumerable.GetEnumerator() {
103      return array.GetEnumerator();
104    }
105    #endregion
106
107    #region Events
108    protected void RegisterEvents() {
109      array.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsReplaced);
110      array.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsMoved);
111      array.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_CollectionReset);
112      array.PropertyChanged += new PropertyChangedEventHandler(array_PropertyChanged);
113    }
114
115    [field: NonSerialized]
116    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
117    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
118      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
119      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
120    }
121
122    [field: NonSerialized]
123    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
124    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
125      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
126      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
127    }
128
129    [field: NonSerialized]
130    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
131    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
132      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
133      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
134    }
135
136    [field: NonSerialized]
137    public event PropertyChangedEventHandler PropertyChanged;
138    protected virtual void OnPropertyChanged(string propertyName) {
139      PropertyChangedEventHandler handler = PropertyChanged;
140      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
141    }
142
143    private void array_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
144      OnItemsReplaced(e.Items, e.OldItems);
145    }
146    private void array_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
147      OnItemsMoved(e.Items, e.OldItems);
148    }
149    private void array_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
150      OnCollectionReset(e.Items, e.OldItems);
151    }
152    private void array_PropertyChanged(object sender, PropertyChangedEventArgs e) {
153      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Length"))
154        OnPropertyChanged(e.PropertyName);
155    }
156    #endregion
157  }
158}
Note: See TracBrowser for help on using the repository browser.