1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Collections {
|
---|
29 | [StorableType("a8b79b27-3f40-4bbe-a59f-9c54c669f111")]
|
---|
30 | [Serializable]
|
---|
31 | public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
|
---|
32 | [Storable]
|
---|
33 | protected IObservableCollection<T> collection;
|
---|
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
|
---|
45 | protected ReadOnlyObservableCollection() { }
|
---|
46 | public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
|
---|
47 | if (collection == null) throw new ArgumentNullException();
|
---|
48 | this.collection = collection;
|
---|
49 | RegisterEvents();
|
---|
50 | }
|
---|
51 | [StorableConstructor]
|
---|
52 | protected ReadOnlyObservableCollection(StorableConstructorFlag deserializing) { }
|
---|
53 |
|
---|
54 | [StorableHook(HookType.AfterDeserialization)]
|
---|
55 | private void AfterDeserialization() {
|
---|
56 | RegisterEvents();
|
---|
57 | }
|
---|
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
|
---|
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 |
|
---|
103 |
|
---|
104 | [field: NonSerialized]
|
---|
105 | public event CollectionItemsChangedEventHandler<T> ItemsAdded;
|
---|
106 | protected virtual void OnItemsAdded(IEnumerable<T> items) {
|
---|
107 | CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
|
---|
108 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
109 | }
|
---|
110 |
|
---|
111 | [field: NonSerialized]
|
---|
112 | public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
|
---|
113 | protected virtual void OnItemsRemoved(IEnumerable<T> items) {
|
---|
114 | CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
|
---|
115 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
116 | }
|
---|
117 |
|
---|
118 | [field: NonSerialized]
|
---|
119 | public event CollectionItemsChangedEventHandler<T> CollectionReset;
|
---|
120 | protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
121 | CollectionItemsChangedEventHandler<T> handler = CollectionReset;
|
---|
122 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
123 | }
|
---|
124 |
|
---|
125 | [field: NonSerialized]
|
---|
126 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
127 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
128 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
129 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
130 | }
|
---|
131 |
|
---|
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 | }
|
---|
141 | private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
142 | if (e.PropertyName.Equals("Count"))
|
---|
143 | OnPropertyChanged(e.PropertyName);
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 | }
|
---|
147 | }
|
---|