[13368] | 1 | #region License Information
|
---|
[2618] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 {
|
---|
[14711] | 29 | [StorableType("5C3B9621-71C1-40F0-8E1C-31B9AD039E2B")]
|
---|
[2618] | 30 | [Serializable]
|
---|
| 31 | public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
|
---|
[3560] | 32 | [Storable]
|
---|
[3390] | 33 | protected IObservableCollection<T> collection;
|
---|
[2618] | 34 |
|
---|
| 35 | #region Properties
|
---|
| 36 | public int Count {
|
---|
| 37 | get { return collection.Count; }
|
---|
| 38 | }
|
---|
| 39 | bool ICollection<T>.IsReadOnly {
|
---|
| 40 | get { return true; }
|
---|
| 41 | }
|
---|
| 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | #region Constructors
|
---|
[3019] | 45 | protected ReadOnlyObservableCollection() { }
|
---|
[2618] | 46 | public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
|
---|
| 47 | if (collection == null) throw new ArgumentNullException();
|
---|
| 48 | this.collection = collection;
|
---|
[3002] | 49 | RegisterEvents();
|
---|
| 50 | }
|
---|
[3560] | 51 | [StorableConstructor]
|
---|
| 52 | protected ReadOnlyObservableCollection(bool deserializing) { }
|
---|
[4722] | 53 |
|
---|
| 54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 55 | private void AfterDeserialization() {
|
---|
| 56 | RegisterEvents();
|
---|
| 57 | }
|
---|
[2618] | 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region Access
|
---|
| 61 | public bool Contains(T item) {
|
---|
| 62 | return collection.Contains(item);
|
---|
| 63 | }
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | #region Manipulation
|
---|
| 67 | void ICollection<T>.Add(T item) {
|
---|
| 68 | throw new NotSupportedException();
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | bool ICollection<T>.Remove(T item) {
|
---|
| 72 | throw new NotSupportedException();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | void ICollection<T>.Clear() {
|
---|
| 76 | throw new NotSupportedException();
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | #region Conversion
|
---|
| 81 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
| 82 | collection.CopyTo(array, arrayIndex);
|
---|
| 83 | }
|
---|
| 84 | #endregion
|
---|
| 85 |
|
---|
| 86 | #region Enumeration
|
---|
| 87 | public IEnumerator<T> GetEnumerator() {
|
---|
| 88 | return ((IEnumerable<T>)collection).GetEnumerator();
|
---|
| 89 | }
|
---|
| 90 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
| 91 | return ((IEnumerable)collection).GetEnumerator();
|
---|
| 92 | }
|
---|
| 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region Events
|
---|
[3019] | 96 | protected void RegisterEvents() {
|
---|
| 97 | collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
|
---|
| 98 | collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
|
---|
| 99 | collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
|
---|
| 100 | collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[3317] | 103 |
|
---|
[2618] | 104 | [field: NonSerialized]
|
---|
| 105 | public event CollectionItemsChangedEventHandler<T> ItemsAdded;
|
---|
| 106 | protected virtual void OnItemsAdded(IEnumerable<T> items) {
|
---|
[3317] | 107 | CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
|
---|
| 108 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2618] | 109 | }
|
---|
| 110 |
|
---|
| 111 | [field: NonSerialized]
|
---|
| 112 | public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
|
---|
| 113 | protected virtual void OnItemsRemoved(IEnumerable<T> items) {
|
---|
[3317] | 114 | CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
|
---|
| 115 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
[2618] | 116 | }
|
---|
| 117 |
|
---|
| 118 | [field: NonSerialized]
|
---|
| 119 | public event CollectionItemsChangedEventHandler<T> CollectionReset;
|
---|
| 120 | protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
[3317] | 121 | CollectionItemsChangedEventHandler<T> handler = CollectionReset;
|
---|
| 122 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
[2618] | 123 | }
|
---|
| 124 |
|
---|
[2620] | 125 | [field: NonSerialized]
|
---|
| 126 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
| 127 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
[3317] | 128 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
| 129 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
[2620] | 130 | }
|
---|
| 131 |
|
---|
[2618] | 132 | private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 133 | OnItemsAdded(e.Items);
|
---|
| 134 | }
|
---|
| 135 | private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 136 | OnItemsRemoved(e.Items);
|
---|
| 137 | }
|
---|
| 138 | private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 139 | OnCollectionReset(e.Items, e.OldItems);
|
---|
| 140 | }
|
---|
[2620] | 141 | private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
| 142 | if (e.PropertyName.Equals("Count"))
|
---|
| 143 | OnPropertyChanged(e.PropertyName);
|
---|
| 144 | }
|
---|
[2618] | 145 | #endregion
|
---|
| 146 | }
|
---|
| 147 | }
|
---|