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