[2745] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2745] | 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;
|
---|
[3560] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2745] | 27 |
|
---|
| 28 | namespace HeuristicLab.Collections {
|
---|
[3560] | 29 | [StorableClass]
|
---|
[2745] | 30 | [Serializable]
|
---|
| 31 | public class ReadOnlyObservableArray<T> : IObservableArray<T> {
|
---|
[3560] | 32 | [Storable]
|
---|
[3390] | 33 | protected IObservableArray<T> array;
|
---|
[2745] | 34 |
|
---|
| 35 | #region Properties
|
---|
| 36 | public int Length {
|
---|
| 37 | get { return array.Length; }
|
---|
| 38 | }
|
---|
| 39 | int ICollection<T>.Count {
|
---|
| 40 | get { return array.Count; }
|
---|
| 41 | }
|
---|
| 42 | bool ICollection<T>.IsReadOnly {
|
---|
| 43 | get { return true; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public T this[int index] {
|
---|
| 47 | get { return array[index]; }
|
---|
| 48 | }
|
---|
| 49 | T IList<T>.this[int index] {
|
---|
| 50 | get { return array[index]; }
|
---|
| 51 | set { throw new NotSupportedException(); }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | #region Constructors
|
---|
[3019] | 56 | protected ReadOnlyObservableArray() { }
|
---|
[2745] | 57 | public ReadOnlyObservableArray(IObservableArray<T> array) {
|
---|
| 58 | if (array == null) throw new ArgumentNullException();
|
---|
| 59 | this.array = array;
|
---|
[3002] | 60 | RegisterEvents();
|
---|
| 61 | }
|
---|
[3560] | 62 | [StorableConstructor]
|
---|
| 63 | protected ReadOnlyObservableArray(bool deserializing) { }
|
---|
[4722] | 64 |
|
---|
| 65 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 66 | private void AfterDeserialization() {
|
---|
| 67 | RegisterEvents();
|
---|
| 68 | }
|
---|
[2745] | 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | #region Access
|
---|
| 72 | public bool Contains(T item) {
|
---|
| 73 | return array.Contains(item);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public int IndexOf(T item) {
|
---|
| 77 | return array.IndexOf(item);
|
---|
| 78 | }
|
---|
| 79 | #endregion
|
---|
| 80 |
|
---|
| 81 | #region Manipulation
|
---|
| 82 | void ICollection<T>.Add(T item) {
|
---|
| 83 | throw new NotSupportedException();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void IList<T>.Insert(int index, T item) {
|
---|
| 87 | throw new NotSupportedException();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | bool ICollection<T>.Remove(T item) {
|
---|
| 91 | throw new NotSupportedException();
|
---|
| 92 | }
|
---|
| 93 | void IList<T>.RemoveAt(int index) {
|
---|
| 94 | throw new NotSupportedException();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void ICollection<T>.Clear() {
|
---|
| 98 | throw new NotSupportedException();
|
---|
| 99 | }
|
---|
[5928] | 100 |
|
---|
| 101 | void IObservableArray<T>.Reverse() {
|
---|
| 102 | throw new NotSupportedException();
|
---|
| 103 | }
|
---|
| 104 | void IObservableArray<T>.Reverse(int index, int count) {
|
---|
| 105 | throw new NotSupportedException();
|
---|
| 106 | }
|
---|
[2745] | 107 | #endregion
|
---|
| 108 |
|
---|
| 109 | #region Conversion
|
---|
| 110 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
| 111 | this.array.CopyTo(array, arrayIndex);
|
---|
| 112 | }
|
---|
| 113 | #endregion
|
---|
| 114 |
|
---|
| 115 | #region Enumeration
|
---|
| 116 | public IEnumerator<T> GetEnumerator() {
|
---|
| 117 | return array.GetEnumerator();
|
---|
| 118 | }
|
---|
| 119 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 120 | return array.GetEnumerator();
|
---|
| 121 | }
|
---|
| 122 | #endregion
|
---|
| 123 |
|
---|
| 124 | #region Events
|
---|
[3019] | 125 | protected void RegisterEvents() {
|
---|
| 126 | array.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsReplaced);
|
---|
| 127 | array.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_ItemsMoved);
|
---|
| 128 | array.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(array_CollectionReset);
|
---|
| 129 | array.PropertyChanged += new PropertyChangedEventHandler(array_PropertyChanged);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[2745] | 132 | [field: NonSerialized]
|
---|
| 133 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsReplaced;
|
---|
| 134 | protected virtual void OnItemsReplaced(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
[3317] | 135 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsReplaced;
|
---|
| 136 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2745] | 137 | }
|
---|
| 138 |
|
---|
| 139 | [field: NonSerialized]
|
---|
| 140 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> ItemsMoved;
|
---|
| 141 | protected virtual void OnItemsMoved(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
[3317] | 142 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = ItemsMoved;
|
---|
| 143 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2745] | 144 | }
|
---|
| 145 |
|
---|
| 146 | [field: NonSerialized]
|
---|
| 147 | public event CollectionItemsChangedEventHandler<IndexedItem<T>> CollectionReset;
|
---|
| 148 | protected virtual void OnCollectionReset(IEnumerable<IndexedItem<T>> items, IEnumerable<IndexedItem<T>> oldItems) {
|
---|
[3317] | 149 | CollectionItemsChangedEventHandler<IndexedItem<T>> handler = CollectionReset;
|
---|
| 150 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<IndexedItem<T>>(items, oldItems));
|
---|
[2745] | 151 | }
|
---|
| 152 |
|
---|
| 153 | [field: NonSerialized]
|
---|
| 154 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 155 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 156 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 157 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2745] | 158 | }
|
---|
| 159 |
|
---|
| 160 | private void array_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 161 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
| 162 | }
|
---|
| 163 | private void array_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 164 | OnItemsMoved(e.Items, e.OldItems);
|
---|
| 165 | }
|
---|
| 166 | private void array_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
| 167 | OnCollectionReset(e.Items, e.OldItems);
|
---|
| 168 | }
|
---|
| 169 | private void array_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 170 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Length"))
|
---|
| 171 | OnPropertyChanged(e.PropertyName);
|
---|
| 172 | }
|
---|
| 173 | #endregion
|
---|
| 174 | }
|
---|
| 175 | }
|
---|