Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3017 was 3017, checked in by epitzer, 14 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

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