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