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