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