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