[2618] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2618] | 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;
|
---|
[2620] | 25 | using System.ComponentModel;
|
---|
[3560] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2618] | 27 |
|
---|
| 28 | namespace HeuristicLab.Collections {
|
---|
[3560] | 29 | [StorableClass]
|
---|
[2618] | 30 | [Serializable]
|
---|
| 31 | public class ReadOnlyObservableList<T> : IObservableList<T> {
|
---|
[3560] | 32 | [Storable]
|
---|
[3390] | 33 | protected IObservableList<T> list;
|
---|
[2618] | 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
|
---|
[3019] | 53 | protected ReadOnlyObservableList() { }
|
---|
[2618] | 54 | public ReadOnlyObservableList(IObservableList<T> list) {
|
---|
| 55 | if (list == null) throw new ArgumentNullException();
|
---|
| 56 | this.list = list;
|
---|
[3002] | 57 | RegisterEvents();
|
---|
| 58 | }
|
---|
[3560] | 59 | [StorableConstructor]
|
---|
| 60 | protected ReadOnlyObservableList(bool deserializing) { }
|
---|
[4722] | 61 |
|
---|
| 62 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 63 | private void AfterDeserialization() {
|
---|
| 64 | RegisterEvents();
|
---|
| 65 | }
|
---|
[2618] | 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
|
---|
[2623] | 100 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
[2618] | 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
|
---|
[3019] | 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 |
|
---|
[2618] | 127 | [field: NonSerialized]
|
---|
| 128 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsAdded;
|
---|
| 129 | protected virtual void OnItemsAdded(IEnumerable<IndexedItem<T>> items) {
|
---|
[3317] | 130 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsAdded;
|
---|
| 131 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
[2618] | 132 | }
|
---|
| 133 |
|
---|
| 134 | [field: NonSerialized]
|
---|
[2623] | 135 | private event CollectionItemsChangedEventHandler<T> itemsAdded;
|
---|
[2832] | 136 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsAdded {
|
---|
[2623] | 137 | add { itemsAdded += value; }
|
---|
| 138 | remove { itemsAdded -= value; }
|
---|
| 139 | }
|
---|
| 140 | private void OnItemsAdded(IEnumerable<T> items) {
|
---|
[3317] | 141 | CollectionItemsChangedEventHandler<T> handler = itemsAdded;
|
---|
| 142 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2623] | 143 | }
|
---|
| 144 |
|
---|
| 145 | [field: NonSerialized]
|
---|
[2618] | 146 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsRemoved;
|
---|
| 147 | protected virtual void OnItemsRemoved(IEnumerable<IndexedItem<T>> items) {
|
---|
[3317] | 148 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsRemoved;
|
---|
| 149 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items));
|
---|
[2618] | 150 | }
|
---|
| 151 |
|
---|
| 152 | [field: NonSerialized]
|
---|
[2623] | 153 | private event CollectionItemsChangedEventHandler<T> itemsRemoved;
|
---|
[2832] | 154 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.ItemsRemoved {
|
---|
[2623] | 155 | add { itemsRemoved += value; }
|
---|
| 156 | remove { itemsRemoved -= value; }
|
---|
| 157 | }
|
---|
| 158 | private void OnItemsRemoved(IEnumerable<T> items) {
|
---|
[3317] | 159 | CollectionItemsChangedEventHandler<T> handler = itemsRemoved;
|
---|
| 160 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2623] | 161 | }
|
---|
| 162 |
|
---|
| 163 | [field: NonSerialized]
|
---|
[2618] | 164 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
|
---|
| 165 | protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
[3317] | 166 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
|
---|
| 167 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2618] | 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) {
|
---|
[3317] | 173 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
|
---|
| 174 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2618] | 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) {
|
---|
[3317] | 180 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
|
---|
| 181 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2618] | 182 | }
|
---|
| 183 |
|
---|
[2620] | 184 | [field: NonSerialized]
|
---|
[2623] | 185 | private event CollectionItemsChangedEventHandler<T> collectionReset;
|
---|
[2832] | 186 | event CollectionItemsChangedEventHandler<T> INotifyObservableCollectionItemsChanged<T>.CollectionReset {
|
---|
[2623] | 187 | add { collectionReset += value; }
|
---|
| 188 | remove { collectionReset -= value; }
|
---|
| 189 | }
|
---|
| 190 | private void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
[3317] | 191 | CollectionItemsChangedEventHandler<T> handler = collectionReset;
|
---|
| 192 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
[2623] | 193 | }
|
---|
| 194 |
|
---|
| 195 | [field: NonSerialized]
|
---|
[2620] | 196 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 197 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 198 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 199 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 200 | }
|
---|
| 201 |
|
---|
[2618] | 202 | private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 203 | OnItemsAdded(e.Items);
|
---|
| 204 | }
|
---|
[2623] | 205 | private void list_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 206 | OnItemsAdded(e.Items);
|
---|
| 207 | }
|
---|
[2618] | 208 | private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 209 | OnItemsRemoved(e.Items);
|
---|
| 210 | }
|
---|
[2623] | 211 | private void list_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 212 | OnItemsRemoved(e.Items);
|
---|
| 213 | }
|
---|
[2618] | 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 | }
|
---|
[2623] | 223 | private void list_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 224 | OnCollectionReset(e.Items, e.OldItems);
|
---|
| 225 | }
|
---|
[2620] | 226 | private void list_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 227 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
|
---|
| 228 | OnPropertyChanged(e.PropertyName);
|
---|
| 229 | }
|
---|
[2618] | 230 | #endregion
|
---|
| 231 | }
|
---|
| 232 | }
|
---|