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("d1e16c60-8c29-4e76-9dca-3e269ead513d")]
|
---|
30 | [Serializable]
|
---|
31 | public class ReadOnlyObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
|
---|
32 | [Storable]
|
---|
33 | protected IObservableKeyedCollection<TKey, TItem> collection;
|
---|
34 |
|
---|
35 | #region Properties
|
---|
36 | public int Count {
|
---|
37 | get { return collection.Count; }
|
---|
38 | }
|
---|
39 | bool ICollection<TItem>.IsReadOnly {
|
---|
40 | get { return true; }
|
---|
41 | }
|
---|
42 |
|
---|
43 | public TItem this[TKey key] {
|
---|
44 | get {
|
---|
45 | return collection[key];
|
---|
46 | }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | #region Constructors
|
---|
51 | protected ReadOnlyObservableKeyedCollection() { }
|
---|
52 | public ReadOnlyObservableKeyedCollection(IObservableKeyedCollection<TKey, TItem> collection) {
|
---|
53 | if (collection == null) throw new ArgumentNullException();
|
---|
54 | this.collection = collection;
|
---|
55 | RegisterEvents();
|
---|
56 | }
|
---|
57 | [StorableConstructor]
|
---|
58 | protected ReadOnlyObservableKeyedCollection(StorableConstructorFlag deserializing) { }
|
---|
59 |
|
---|
60 | [StorableHook(HookType.AfterDeserialization)]
|
---|
61 | private void AfterDeserialization() {
|
---|
62 | RegisterEvents();
|
---|
63 | }
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region Access
|
---|
67 | public bool ContainsKey(TKey key) {
|
---|
68 | return collection.ContainsKey(key);
|
---|
69 | }
|
---|
70 | public bool Contains(TItem item) {
|
---|
71 | return collection.Contains(item);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public bool TryGetValue(TKey key, out TItem item) {
|
---|
75 | return collection.TryGetValue(key, out item);
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 |
|
---|
79 | #region Manipulation
|
---|
80 | void ICollection<TItem>.Add(TItem item) {
|
---|
81 | throw new NotSupportedException();
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
|
---|
85 | throw new NotSupportedException();
|
---|
86 | }
|
---|
87 | bool ICollection<TItem>.Remove(TItem item) {
|
---|
88 | throw new NotSupportedException();
|
---|
89 | }
|
---|
90 |
|
---|
91 | void ICollection<TItem>.Clear() {
|
---|
92 | throw new NotSupportedException();
|
---|
93 | }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | #region Conversion
|
---|
97 | public void CopyTo(TItem[] array, int arrayIndex) {
|
---|
98 | collection.CopyTo(array, arrayIndex);
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region Enumeration
|
---|
103 | public IEnumerator<TItem> GetEnumerator() {
|
---|
104 | return ((IEnumerable<TItem>)collection).GetEnumerator();
|
---|
105 | }
|
---|
106 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
107 | return ((IEnumerable)collection).GetEnumerator();
|
---|
108 | }
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | #region Events
|
---|
112 | protected void RegisterEvents() {
|
---|
113 | collection.ItemsAdded += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsAdded);
|
---|
114 | collection.ItemsRemoved += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsRemoved);
|
---|
115 | collection.ItemsReplaced += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsReplaced);
|
---|
116 | collection.CollectionReset += new CollectionItemsChangedEventHandler<TItem>(collection_CollectionReset);
|
---|
117 | collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
|
---|
118 | }
|
---|
119 |
|
---|
120 | [field: NonSerialized]
|
---|
121 | public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
|
---|
122 | protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
|
---|
123 | CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
|
---|
124 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
125 | }
|
---|
126 |
|
---|
127 | [field: NonSerialized]
|
---|
128 | public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
|
---|
129 | protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
|
---|
130 | CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
|
---|
131 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
132 | }
|
---|
133 |
|
---|
134 | [field: NonSerialized]
|
---|
135 | public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
|
---|
136 | protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
137 | CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
|
---|
138 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
139 | }
|
---|
140 |
|
---|
141 | [field: NonSerialized]
|
---|
142 | public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
|
---|
143 | protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
144 | CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
|
---|
145 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
146 | }
|
---|
147 |
|
---|
148 | [field: NonSerialized]
|
---|
149 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
150 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
151 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
152 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
156 | OnItemsAdded(e.Items);
|
---|
157 | }
|
---|
158 | private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
159 | OnItemsRemoved(e.Items);
|
---|
160 | }
|
---|
161 | private void collection_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
162 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
163 | }
|
---|
164 | private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
165 | OnCollectionReset(e.Items, e.OldItems);
|
---|
166 | }
|
---|
167 | private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
168 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
|
---|
169 | OnPropertyChanged(e.PropertyName);
|
---|
170 | }
|
---|
171 | #endregion
|
---|
172 | }
|
---|
173 | }
|
---|