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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.ComponentModel;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Collections {
|
---|
29 | [Serializable]
|
---|
30 | [StorableClass]
|
---|
31 | public class ReadOnlyObservableCollection<T> : IObservableCollection<T> {
|
---|
32 | [Storable]
|
---|
33 | private IObservableCollection<T> 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<T>.IsReadOnly {
|
---|
45 | get { return true; }
|
---|
46 | }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region Constructors
|
---|
50 | protected ReadOnlyObservableCollection() { }
|
---|
51 | public ReadOnlyObservableCollection(IObservableCollection<T> collection) {
|
---|
52 | if (collection == null) throw new ArgumentNullException();
|
---|
53 | this.collection = collection;
|
---|
54 | RegisterEvents();
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region Access
|
---|
59 | public bool Contains(T item) {
|
---|
60 | return collection.Contains(item);
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Manipulation
|
---|
65 | void ICollection<T>.Add(T item) {
|
---|
66 | throw new NotSupportedException();
|
---|
67 | }
|
---|
68 |
|
---|
69 | bool ICollection<T>.Remove(T item) {
|
---|
70 | throw new NotSupportedException();
|
---|
71 | }
|
---|
72 |
|
---|
73 | void ICollection<T>.Clear() {
|
---|
74 | throw new NotSupportedException();
|
---|
75 | }
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | #region Conversion
|
---|
79 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
80 | collection.CopyTo(array, arrayIndex);
|
---|
81 | }
|
---|
82 | #endregion
|
---|
83 |
|
---|
84 | #region Enumeration
|
---|
85 | public IEnumerator<T> GetEnumerator() {
|
---|
86 | return ((IEnumerable<T>)collection).GetEnumerator();
|
---|
87 | }
|
---|
88 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
89 | return ((IEnumerable)collection).GetEnumerator();
|
---|
90 | }
|
---|
91 | #endregion
|
---|
92 |
|
---|
93 | #region Events
|
---|
94 | [StorableHook(HookType.AfterDeserialization)]
|
---|
95 | protected void RegisterEvents() {
|
---|
96 | collection.ItemsAdded += new CollectionItemsChangedEventHandler<T>(collection_ItemsAdded);
|
---|
97 | collection.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(collection_ItemsRemoved);
|
---|
98 | collection.CollectionReset += new CollectionItemsChangedEventHandler<T>(collection_CollectionReset);
|
---|
99 | collection.PropertyChanged += new PropertyChangedEventHandler(collection_PropertyChanged);
|
---|
100 | }
|
---|
101 |
|
---|
102 | event EventHandler IObservableCollection<T>.ReadOnlyViewChanged {
|
---|
103 | add { }
|
---|
104 | remove { }
|
---|
105 | }
|
---|
106 |
|
---|
107 | [field: NonSerialized]
|
---|
108 | public event CollectionItemsChangedEventHandler<T> ItemsAdded;
|
---|
109 | protected virtual void OnItemsAdded(IEnumerable<T> items) {
|
---|
110 | CollectionItemsChangedEventHandler<T> handler = ItemsAdded;
|
---|
111 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
112 | }
|
---|
113 |
|
---|
114 | [field: NonSerialized]
|
---|
115 | public event CollectionItemsChangedEventHandler<T> ItemsRemoved;
|
---|
116 | protected virtual void OnItemsRemoved(IEnumerable<T> items) {
|
---|
117 | CollectionItemsChangedEventHandler<T> handler = ItemsRemoved;
|
---|
118 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items));
|
---|
119 | }
|
---|
120 |
|
---|
121 | [field: NonSerialized]
|
---|
122 | public event CollectionItemsChangedEventHandler<T> CollectionReset;
|
---|
123 | protected virtual void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
124 | CollectionItemsChangedEventHandler<T> handler = CollectionReset;
|
---|
125 | if (handler != null) handler(this, new CollectionItemsChangedEventArgs<T>(items, oldItems));
|
---|
126 | }
|
---|
127 |
|
---|
128 | [field: NonSerialized]
|
---|
129 | public event PropertyChangedEventHandler PropertyChanged;
|
---|
130 | protected virtual void OnPropertyChanged(string propertyName) {
|
---|
131 | PropertyChangedEventHandler handler = PropertyChanged;
|
---|
132 | if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void collection_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
136 | OnItemsAdded(e.Items);
|
---|
137 | }
|
---|
138 | private void collection_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
139 | OnItemsRemoved(e.Items);
|
---|
140 | }
|
---|
141 | private void collection_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
142 | OnCollectionReset(e.Items, e.OldItems);
|
---|
143 | }
|
---|
144 | private void collection_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
145 | if (e.PropertyName.Equals("Count"))
|
---|
146 | OnPropertyChanged(e.PropertyName);
|
---|
147 | }
|
---|
148 | #endregion
|
---|
149 | }
|
---|
150 | }
|
---|