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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.ComponentModel;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Collections {
|
---|
29 | [Serializable]
|
---|
30 | [StorableClass(StorableClassType.MarkedOnly)]
|
---|
31 | public class ReadOnlyObservableList<T> : IObservableList<T> {
|
---|
32 | [Storable]
|
---|
33 | private IObservableList<T> list;
|
---|
34 |
|
---|
35 | #region persistence
|
---|
36 | private ReadOnlyObservableList() { }
|
---|
37 |
|
---|
38 | [StorableHook(HookType.AfterDeserialization)]
|
---|
39 | private void PostDeserizlationHook() {
|
---|
40 | RegisterEvents();
|
---|
41 | }
|
---|
42 | #endregion
|
---|
43 |
|
---|
44 | #region Properties
|
---|
45 | public int Count {
|
---|
46 | get { return ((ICollection<T>)list).Count; }
|
---|
47 | }
|
---|
48 | bool ICollection<T>.IsReadOnly {
|
---|
49 | get { return true; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public T this[int index] {
|
---|
53 | get { return list[index]; }
|
---|
54 | }
|
---|
55 | T IList<T>.this[int index] {
|
---|
56 | get { return list[index]; }
|
---|
57 | set { throw new NotSupportedException(); }
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | #region Constructors
|
---|
62 | public ReadOnlyObservableList(IObservableList<T> list) {
|
---|
63 | if (list == null) throw new ArgumentNullException();
|
---|
64 | this.list = list;
|
---|
65 | RegisterEvents();
|
---|
66 | }
|
---|
67 |
|
---|
68 | private void RegisterEvents() {
|
---|
69 | list.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsAdded);
|
---|
70 | ((IObservableCollection<T>)list).ItemsAdded += new CollectionItemsChangedEventHandler<T>(list_ItemsAdded);
|
---|
71 | list.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsRemoved);
|
---|
72 | ((IObservableCollection<T>)list).ItemsRemoved += new CollectionItemsChangedEventHandler<T>(list_ItemsRemoved);
|
---|
73 | list.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsReplaced);
|
---|
74 | list.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_ItemsMoved);
|
---|
75 | list.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(list_CollectionReset);
|
---|
76 | ((IObservableCollection<T>)list).CollectionReset += new CollectionItemsChangedEventHandler<T>(list_CollectionReset);
|
---|
77 | list.PropertyChanged += new PropertyChangedEventHandler(list_PropertyChanged);
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | #region Access
|
---|
82 | public bool Contains(T item) {
|
---|
83 | return list.Contains(item);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public int IndexOf(T item) {
|
---|
87 | return list.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 | list.CopyTo(array, arrayIndex);
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 |
|
---|
118 | #region Enumeration
|
---|
119 | public IEnumerator<T> GetEnumerator() {
|
---|
120 | return ((ICollection<T>)list).GetEnumerator();
|
---|
121 | }
|
---|
122 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
123 | return ((IEnumerable)list).GetEnumerator();
|
---|
124 | }
|
---|
125 | #endregion
|
---|
126 |
|
---|
127 | #region Events
|
---|
128 | [field: NonSerialized]
|
---|
129 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
|
---|
130 | protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
|
---|
131 | if (ItemsAdded != null)
|
---|
132 | ItemsAdded(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
133 | }
|
---|
134 |
|
---|
135 | [field: NonSerialized]
|
---|
136 | private event CollectionItemsChangedEventHandler<T> itemsAdded;
|
---|
137 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
|
---|
138 | add { itemsAdded += value; }
|
---|
139 | remove { itemsAdded -= value; }
|
---|
140 | }
|
---|
141 | private void OnItemsAdded(IEnumerable<T> items) {
|
---|
142 | if (itemsAdded != null)
|
---|
143 | itemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
144 | }
|
---|
145 |
|
---|
146 | [field: NonSerialized]
|
---|
147 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
|
---|
148 | protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
|
---|
149 | if (ItemsRemoved != null)
|
---|
150 | ItemsRemoved(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
151 | }
|
---|
152 |
|
---|
153 | [field: NonSerialized]
|
---|
154 | private event CollectionItemsChangedEventHandler<T> itemsRemoved;
|
---|
155 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
|
---|
156 | add { itemsRemoved += value; }
|
---|
157 | remove { itemsRemoved -= value; }
|
---|
158 | }
|
---|
159 | private void OnItemsRemoved(IEnumerable<T> items) {
|
---|
160 | if (itemsRemoved != null)
|
---|
161 | itemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
162 | }
|
---|
163 |
|
---|
164 | [field: NonSerialized]
|
---|
165 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
|
---|
166 | protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
167 | if (ItemsReplaced != null)
|
---|
168 | ItemsReplaced(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
169 | }
|
---|
170 |
|
---|
171 | [field: NonSerialized]
|
---|
172 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
|
---|
173 | protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
174 | if (ItemsMoved != null)
|
---|
175 | ItemsMoved(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
176 | }
|
---|
177 |
|
---|
178 | [field: NonSerialized]
|
---|
179 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
|
---|
180 | protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
181 | if (CollectionReset != null)
|
---|
182 | CollectionReset(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
183 | }
|
---|
184 |
|
---|
185 | [field: NonSerialized]
|
---|
186 | private event CollectionItemsChangedEventHandler<T> collectionReset;
|
---|
187 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
|
---|
188 | add { collectionReset += value; }
|
---|
189 | remove { collectionReset -= value; }
|
---|
190 | }
|
---|
191 | private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
192 | if (collectionReset != null)
|
---|
193 | collectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
194 | }
|
---|
195 |
|
---|
196 | [field: NonSerialized]
|
---|
197 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
198 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
199 | if (PropertyChanged != null)
|
---|
200 | PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
204 | OnItemsAdded(e.Items);
|
---|
205 | }
|
---|
206 | private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
207 | OnItemsAdded(e.Items);
|
---|
208 | }
|
---|
209 | private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
210 | OnItemsRemoved(e.Items);
|
---|
211 | }
|
---|
212 | private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
213 | OnItemsRemoved(e.Items);
|
---|
214 | }
|
---|
215 | private void list_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
216 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
217 | }
|
---|
218 | private void list_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
219 | OnItemsMoved(e.Items, e.OldItems);
|
---|
220 | }
|
---|
221 | private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
222 | OnCollectionReset(e.Items, e.OldItems);
|
---|
223 | }
|
---|
224 | private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
225 | OnCollectionReset(e.Items, e.OldItems);
|
---|
226 | }
|
---|
227 | private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
228 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
|
---|
229 | OnPropertyChanged(e.PropertyName);
|
---|
230 | }
|
---|
231 | #endregion
|
---|
232 | }
|
---|
233 | }
|
---|