Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Collections/3.3/ReadOnlyObservableCollection.cs @ 2790

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
  • added additional plugins HeuristicLab.Evolutionary, HeuristicLab.Permutation, HeuristicLab.Selection, and HeuristicLab.Routing.TSP
File size: 4.6 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  public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
31    [Storable]
32    private IObservableCollection<T> collection;
33
34    #region Properties
35    public int Count {
36      get { return collection.Count; }
37    }
38    bool ICollection<T>.IsReadOnly {
39      get { return true; }
40    }
41    #endregion
42
43    #region Constructors
44    public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
45      if (collection == null) throw new ArgumentNullException();
46      this.collection = collection;
47      collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
48      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
49      collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
50      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
51    }
52    #endregion
53
54    #region Access
55    public bool Contains(T item) {
56      return collection.Contains(item);
57    }
58    #endregion
59
60    #region Manipulation
61    void ICollection<T>.Add(T item) {
62      throw new NotSupportedException();
63    }
64
65    bool ICollection<T>.Remove(T item) {
66      throw new NotSupportedException();
67    }
68
69    void ICollection<T>.Clear() {
70      throw new NotSupportedException();
71    }
72    #endregion
73
74    #region Conversion
75    public void CopyTo(T[] array, int arrayIndex) {
76      collection.CopyTo(array, arrayIndex);
77    }
78    #endregion
79
80    #region Enumeration
81    public IEnumerator<T> GetEnumerator() {
82      return ((IEnumerable<T>)collection).GetEnumerator();
83    }
84    IEnumerator IEnumerable.GetEnumerator() {
85      return ((IEnumerable)collection).GetEnumerator();
86    }
87    #endregion
88
89    #region Events
90    [field: NonSerialized]
91    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
92    protected virtual void OnItemsAdded(IEnumerable<T> items) {
93      if (ItemsAdded != null)
94        ItemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
95    }
96
97    [field: NonSerialized]
98    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
99    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
100      if (ItemsRemoved != null)
101        ItemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
102    }
103
104    [field: NonSerialized]
105    public event CollectionItemsChangedEventHandler<T> CollectionReset;
106    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
107      if (CollectionReset != null)
108        CollectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
109    }
110
111    [field: NonSerialized]
112    public event PropertyChangedEventHandler PropertyChanged;
113    protected virtual void OnPropertyChanged(string propertyName) {
114      if (PropertyChanged != null)
115        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
116    }
117
118    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
119      OnItemsAdded(e.Items);
120    }
121    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
122      OnItemsRemoved(e.Items);
123    }
124    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
125      OnCollectionReset(e.Items, e.OldItems);
126    }
127    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
128      if (e.PropertyName.Equals("Count"))
129        OnPropertyChanged(e.PropertyName);
130    }
131    #endregion
132  }
133}
Note: See TracBrowser for help on using the repository browser.