Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3317 was 3317, checked in by swagner, 14 years ago

Implemented ReadOnlyView property for items (#969).

File size: 6.5 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;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Collections {
29  [Serializable]
30  [StorableClass]
31  public class ReadOnlyObservableKeyedCollection<TKey, TItem> : IObservableKeyedCollection<TKey, TItem> {
32    [Storable]
33    private IObservableKeyedCollection<TKey, TItem> collection;
34
35    #region Properties
36    public bool ReadOnlyView {
37      get { return true; }
38      set { throw new NotSupportedException(); }
39    }
40
41    public int Count {
42      get { return collection.Count; }
43    }
44    bool ICollection<TItem>.IsReadOnly {
45      get { return true; }
46    }
47
48    public TItem this[TKey key] {
49      get {
50        return collection[key];
51      }
52    }
53    #endregion
54
55    #region Constructors
56    protected ReadOnlyObservableKeyedCollection() { }
57    public ReadOnlyObservableKeyedCollection(IObservableKeyedCollection<TKey, TItem> collection) {
58      if (collection == null) throw new ArgumentNullException();
59      this.collection = collection;
60      RegisterEvents();
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    [StorableHook(HookType.AfterDeserialization)]
111    protected void RegisterEvents() {
112      collection.ItemsAdded += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsAdded);
113      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsRemoved);
114      collection.ItemsReplaced += new CollectionItemsChangedEventHandler<TItem>(collection_ItemsReplaced);
115      collection.CollectionReset += new CollectionItemsChangedEventHandler<TItem>(collection_CollectionReset);
116      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
117    }
118
119    event EventHandler IObservableCollection<TItem>.ReadOnlyViewChanged {
120      add { }
121      remove { }
122    }
123
124    [field: NonSerialized]
125    public event CollectionItemsChangedEventHandler<TItem> ItemsAdded;
126    protected virtual void OnItemsAdded(IEnumerable<TItem> items) {
127      CollectionItemsChangedEventHandler<TItem> handler = ItemsAdded;
128      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
129    }
130
131    [field: NonSerialized]
132    public event CollectionItemsChangedEventHandler<TItem> ItemsRemoved;
133    protected virtual void OnItemsRemoved(IEnumerable<TItem> items) {
134      CollectionItemsChangedEventHandler<TItem> handler = ItemsRemoved;
135      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items));
136    }
137
138    [field: NonSerialized]
139    public event CollectionItemsChangedEventHandler<TItem> ItemsReplaced;
140    protected virtual void OnItemsReplaced(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
141      CollectionItemsChangedEventHandler<TItem> handler = ItemsReplaced;
142      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
143    }
144
145    [field: NonSerialized]
146    public event CollectionItemsChangedEventHandler<TItem> CollectionReset;
147    protected virtual void OnCollectionReset(IEnumerable<TItem> items, IEnumerable<TItem> oldItems) {
148      CollectionItemsChangedEventHandler<TItem> handler = CollectionReset;
149      if (handler != null) handler(this, new CollectionItemsChangedEventArgs<TItem>(items, oldItems));
150    }
151
152    [field: NonSerialized]
153    public event PropertyChangedEventHandler PropertyChanged;
154    protected virtual void OnPropertyChanged(string propertyName) {
155      PropertyChangedEventHandler handler = PropertyChanged;
156      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
157    }
158
159    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<TItem> e) {
160      OnItemsAdded(e.Items);
161    }
162    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<TItem> e) {
163      OnItemsRemoved(e.Items);
164    }
165    private void collection_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<TItem> e) {
166      OnItemsReplaced(e.Items, e.OldItems);
167    }
168    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<TItem> e) {
169      OnCollectionReset(e.Items, e.OldItems);
170    }
171    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
172      if (e.PropertyName.Equals("Item[]") || e.PropertyName.Equals("Count"))
173        OnPropertyChanged(e.PropertyName);
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.