Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableKeyedCollection.cs @ 3397

Last change on this file since 3397 was 3390, checked in by swagner, 15 years ago

Refactored HeuristicLab.Collections (#977)

File size: 6.1 KB
Line 
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
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.ComponentModel;
26
27namespace HeuristicLab.Collections {
28  [Serializable]
29  public class ReadOnlyObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
30    protected IObservableKeyedCollection<TKey, TItem> collection;
31
32    #region Properties
33    public int Count {
34      get { return collection.Count; }
35    }
36    bool ICollection<TItem>.IsReadOnly {
37      get { return true; }
38    }
39
40    public TItem this[TKey key] {
41      get {
42        return collection[key];
43      }
44    }
45    #endregion
46
47    #region Constructors
48    protected ReadOnlyObservableKeyedCollection() { }
49    public ReadOnlyObservableKeyedCollection(IObservableKeyedCollection<TKey, TItem> collection) {
50      if (collection == null) throw new ArgumentNullException();
51      this.collection = collection;
52      RegisterEvents();
53    }
54    #endregion
55
56    #region Access
57    public bool ContainsKey(TKey key) {
58      return collection.ContainsKey(key);
59    }
60    public bool Contains(TItem item) {
61      return collection.Contains(item);
62    }
63
64    public bool TryGetValue(TKey key, out TItem item) {
65      return collection.TryGetValue(key, out item);
66    }
67    #endregion
68
69    #region Manipulation
70    void ICollection<TItem>.Add(TItem item) {
71      throw new NotSupportedException();
72    }
73
74    bool IObservableKeyedCollection<TKey, TItem>.Remove(TKey key) {
75      throw new NotSupportedException();
76    }
77    bool ICollection<TItem>.Remove(TItem item) {
78      throw new NotSupportedException();
79    }
80
81    void ICollection<TItem>.Clear() {
82      throw new NotSupportedException();
83    }
84    #endregion
85
86    #region Conversion
87    public void CopyTo(TItem[] array, int arrayIndex) {
88      collection.CopyTo(array, arrayIndex);
89    }
90    #endregion
91
92    #region Enumeration
93    public IEnumerator<TItem> GetEnumerator() {
94      return ((IEnumerable<TItem>)collection).GetEnumerator();
95    }
96    IEnumerator IEnumerable.GetEnumerator() {
97      return ((IEnumerable)collection).GetEnumerator();
98    }
99    #endregion
100
101    #region Events
102    protected void RegisterEvents() {
103      collection.ItemsAdded += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsAdded);
104      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsRemoved);
105      collection.ItemsReplaced += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsReplaced);
106      collection.CollectionReset += new CollectionItemsChangedEventHandler<TItem>(collection_CollectionReset);
107      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
108    }
109
110    [field: NonSerialized]
111    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
112    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
113      CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
114      if (handler != null) handler(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      CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
121      if (handler != null) handler(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      CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
128      if (handler != null) handler(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      CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
135      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
136    }
137
138    [field: NonSerialized]
139    public event PropertyChangedEventHandler PropertyChanged;
140    protected virtual void OnPropertyChanged(string propertyName) {
141      PropertyChangedEventHandler handler = PropertyChanged;
142      if (handler != null) handler(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}
Note: See TracBrowser for help on using the repository browser.