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 | [StorableClass]
|
---|
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(bool deserializing) { }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | #region Access
|
---|
62 | public bool ContainsKey(TKey key) {
|
---|
63 | return collection.ContainsKey(key);
|
---|
64 | }
|
---|
65 | public bool Contains(TItem item) {
|
---|
66 | return collection.Contains(item);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public bool TryGetValue(TKey key, out TItem item) {
|
---|
70 | return collection.TryGetValue(key, out item);
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Manipulation
|
---|
75 | void ICollection<TItem>.Add(TItem item) {
|
---|
76 | throw new NotSupportedException();
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
|
---|
80 | throw new NotSupportedException();
|
---|
81 | }
|
---|
82 | bool ICollection<TItem>.Remove(TItem item) {
|
---|
83 | throw new NotSupportedException();
|
---|
84 | }
|
---|
85 |
|
---|
86 | void ICollection<TItem>.Clear() {
|
---|
87 | throw new NotSupportedException();
|
---|
88 | }
|
---|
89 | #endregion
|
---|
90 |
|
---|
91 | #region Conversion
|
---|
92 | public void CopyTo(TItem[] array, int arrayIndex) {
|
---|
93 | collection.CopyTo(array, arrayIndex);
|
---|
94 | }
|
---|
95 | #endregion
|
---|
96 |
|
---|
97 | #region Enumeration
|
---|
98 | public IEnumerator<TItem> GetEnumerator() {
|
---|
99 | return ((IEnumerable<TItem>)collection).GetEnumerator();
|
---|
100 | }
|
---|
101 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
102 | return ((IEnumerable)collection).GetEnumerator();
|
---|
103 | }
|
---|
104 | #endregion
|
---|
105 |
|
---|
106 | #region Events
|
---|
107 | [StorableHook(HookType.AfterDeserialization)]
|
---|
108 | protected void RegisterEvents() {
|
---|
109 | collection.ItemsAdded += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsAdded);
|
---|
110 | collection.ItemsRemoved += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsRemoved);
|
---|
111 | collection.ItemsReplaced += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsReplaced);
|
---|
112 | collection.CollectionReset += new CollectionItemsChangedEventHandler<TItem>(collection_CollectionReset);
|
---|
113 | collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
|
---|
114 | }
|
---|
115 |
|
---|
116 | [field: NonSerialized]
|
---|
117 | public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
|
---|
118 | protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
|
---|
119 | CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
|
---|
120 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
121 | }
|
---|
122 |
|
---|
123 | [field: NonSerialized]
|
---|
124 | public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
|
---|
125 | protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
|
---|
126 | CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
|
---|
127 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
|
---|
128 | }
|
---|
129 |
|
---|
130 | [field: NonSerialized]
|
---|
131 | public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
|
---|
132 | protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
133 | CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
|
---|
134 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
135 | }
|
---|
136 |
|
---|
137 | [field: NonSerialized]
|
---|
138 | public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
|
---|
139 | protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
|
---|
140 | CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
|
---|
141 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
|
---|
142 | }
|
---|
143 |
|
---|
144 | [field: NonSerialized]
|
---|
145 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
146 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
147 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
148 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
149 | }
|
---|
150 |
|
---|
151 | private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
152 | OnItemsAdded(e.Items);
|
---|
153 | }
|
---|
154 | private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
155 | OnItemsRemoved(e.Items);
|
---|
156 | }
|
---|
157 | private void collection_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
158 | OnItemsReplaced(e.Items, e.OldItems);
|
---|
159 | }
|
---|
160 | private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<TItem> e) {
|
---|
161 | OnCollectionReset(e.Items, e.OldItems);
|
---|
162 | }
|
---|
163 | private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
164 | if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
|
---|
165 | OnPropertyChanged(e.PropertyName);
|
---|
166 | }
|
---|
167 | #endregion
|
---|
168 | }
|
---|
169 | }
|
---|