Free cookie consent management tool by TermsFeed Policy Generator

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

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

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