Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3932 was 3560, checked in by swagner, 15 years ago

Fixed persistence exceptions by restoring the reference on HeuristicLab.Persistence in HeuristicLab.Collections (#977)

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