Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3021 was 3019, checked in by swagner, 15 years ago

Restructured persistence code of read-only observable collections (#548)

File size: 4.8 KB
RevLine 
[2618]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2618]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;
[2620]25using System.ComponentModel;
[2618]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Collections {
29  [Serializable]
[3017]30  [StorableClass]
[2618]31  public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
32    [Storable]
33    private IObservableCollection<T> collection;
34
35    #region Properties
36    public int Count {
37      get { return collection.Count; }
38    }
39    bool ICollection<T>.IsReadOnly {
40      get { return true; }
41    }
42    #endregion
43
44    #region Constructors
[3019]45    protected ReadOnlyObservableCollection() { }
[2618]46    public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
47      if (collection == null) throw new ArgumentNullException();
48      this.collection = collection;
[3002]49      RegisterEvents();
50    }
[2618]51    #endregion
52
53    #region Access
54    public bool Contains(T item) {
55      return collection.Contains(item);
56    }
57    #endregion
58
59    #region Manipulation
60    void ICollection<T>.Add(T item) {
61      throw new NotSupportedException();
62    }
63
64    bool ICollection<T>.Remove(T item) {
65      throw new NotSupportedException();
66    }
67
68    void ICollection<T>.Clear() {
69      throw new NotSupportedException();
70    }
71    #endregion
72
73    #region Conversion
74    public void CopyTo(T[] array, int arrayIndex) {
75      collection.CopyTo(array, arrayIndex);
76    }
77    #endregion
78
79    #region Enumeration
80    public IEnumerator<T> GetEnumerator() {
81      return ((IEnumerable<T>)collection).GetEnumerator();
82    }
83    IEnumerator IEnumerable.GetEnumerator() {
84      return ((IEnumerable)collection).GetEnumerator();
85    }
86    #endregion
87
88    #region Events
[3019]89    [StorableHook(HookType.AfterDeserialization)]
90    protected void RegisterEvents() {
91      collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
92      collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
93      collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
94      collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
95    }
96
[2618]97    [field: NonSerialized]
98    public event CollectionItemsChangedEventHandler<T> ItemsAdded;
99    protected virtual void OnItemsAdded(IEnumerable<T> items) {
100      if (ItemsAdded != null)
101        ItemsAdded(this, new CollectionItemsChangedEventArgs<T>(items));
102    }
103
104    [field: NonSerialized]
105    public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
106    protected virtual void OnItemsRemoved(IEnumerable<T> items) {
107      if (ItemsRemoved != null)
108        ItemsRemoved(this, new CollectionItemsChangedEventArgs<T>(items));
109    }
110
111    [field: NonSerialized]
112    public event CollectionItemsChangedEventHandler<T> CollectionReset;
113    protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
114      if (CollectionReset != null)
115        CollectionReset(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
116    }
117
[2620]118    [field: NonSerialized]
119    public event PropertyChangedEventHandler PropertyChanged;
120    protected virtual void OnPropertyChanged(string propertyName) {
121      if (PropertyChanged != null)
122        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
123    }
124
[2618]125    private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
126      OnItemsAdded(e.Items);
127    }
128    private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
129      OnItemsRemoved(e.Items);
130    }
131    private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
132      OnCollectionReset(e.Items, e.OldItems);
133    }
[2620]134    private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
135      if (e.PropertyName.Equals("Count"))
136        OnPropertyChanged(e.PropertyName);
137    }
[2618]138    #endregion
139  }
140}
Note: See TracBrowser for help on using the repository browser.