Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3317 was 3317, checked in by swagner, 14 years ago

Implemented ReadOnlyView property for items (#969).

File size: 6.0 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  [StorableClass]
31  public class ReadOnlyObservableArray<T> : IObservableArray<T> {
32    [Storable]
33    private IObservableArray<T> array;
34
35    #region Properties
36    public bool ReadOnlyView {
37      get { return true; }
38      set { throw new NotSupportedException(); }
39    }
40
41    public int Length {
42      get { return array.Length; }
43    }
44    int ICollection<T>.Count {
45      get { return array.Count; }
46    }
47    bool ICollection<T>.IsReadOnly {
48      get { return true; }
49    }
50
51    public T this[int index] {
52      get { return array[index]; }
53    }
54    T IList<T>.this[int index] {
55      get { return array[index]; }
56      set { throw new NotSupportedException(); }
57    }
58    #endregion
59
60    #region Constructors
61    protected ReadOnlyObservableArray() { }
62    public ReadOnlyObservableArray(IObservableArray<T> array) {
63      if (array == null) throw new ArgumentNullException();
64      this.array = array;
65      RegisterEvents();
66    }
67    #endregion
68
69    #region Access
70    public bool Contains(T item) {
71      return array.Contains(item);
72    }
73
74    public int IndexOf(T item) {
75      return array.IndexOf(item);
76    }
77    #endregion
78
79    #region Manipulation
80    void ICollection<T>.Add(T item) {
81      throw new NotSupportedException();
82    }
83
84    void IList<T>.Insert(int index, T item) {
85      throw new NotSupportedException();
86    }
87
88    bool ICollection<T>.Remove(T item) {
89      throw new NotSupportedException();
90    }
91    void IList<T>.RemoveAt(int index) {
92      throw new NotSupportedException();
93    }
94
95    void ICollection<T>.Clear() {
96      throw new NotSupportedException();
97    }
98    #endregion
99
100    #region Conversion
101    public void CopyTo(T[] array, int arrayIndex) {
102      this.array.CopyTo(array, arrayIndex);
103    }
104    #endregion
105
106    #region Enumeration
107    public IEnumerator<T> GetEnumerator() {
108      return array.GetEnumerator();
109    }
110    IEnumerator IEnumerable.GetEnumerator() {
111      return array.GetEnumerator();
112    }
113    #endregion
114
115    #region Events
116    [StorableHook(HookType.AfterDeserialization)]
117    protected void RegisterEvents() {
118      array.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsReplaced);
119      array.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsMoved);
120      array.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_CollectionReset);
121      array.PropertyChanged += new PropertyChangedEventHandler(array_PropertyChanged);
122    }
123
124    event EventHandler IObservableArray<T>.ReadOnlyViewChanged {
125      add { }
126      remove { }
127    }
128
129    [field: NonSerialized]
130    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
131    protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
132      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
133      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
134    }
135
136    [field: NonSerialized]
137    public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
138    protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
139      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
140      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
141    }
142
143    [field: NonSerialized]
144    public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
145    protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
146      CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
147      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
148    }
149
150    [field: NonSerialized]
151    public event PropertyChangedEventHandler PropertyChanged;
152    protected virtual void OnPropertyChanged(string propertyName) {
153      PropertyChangedEventHandler handler = PropertyChanged;
154      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
155    }
156
157    private void array_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
158      OnItemsReplaced(e.Items, e.OldItems);
159    }
160    private void array_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
161      OnItemsMoved(e.Items, e.OldItems);
162    }
163    private void array_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
164      OnCollectionReset(e.Items, e.OldItems);
165    }
166    private void array_PropertyChanged(object sender, PropertyChangedEventArgs e) {
167      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Length"))
168        OnPropertyChanged(e.PropertyName);
169    }
170    #endregion
171  }
172}
Note: See TracBrowser for help on using the repository browser.