Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Mainform refactoring/HeuristicLab.Common/3.2/ObservableCollection.cs @ 2424

Last change on this file since 2424 was 2424, checked in by mkommend, 15 years ago

changed ObservableCollection to implement INotifyCollectionChanged
and added ObservableList in branch mainform refactoring (ticket #779)

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using System.Collections.Specialized;
26
27namespace HeuristicLab.Common {
28  public class ObservableCollection<T> : ICollection<T>, INotifyCollectionChanged {
29    protected List<T> list;
30
31    public ObservableCollection() {
32      list = new List<T>();
33    }
34
35    #region Events
36    public event NotifyCollectionChangedEventHandler CollectionChanged;
37
38    protected void FireCollectionChanged(NotifyCollectionChangedEventArgs e) {
39      if (CollectionChanged != null) {
40        CollectionChanged(this, e);
41      }
42    }
43
44    protected virtual void FireCollectionCleared() {
45        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
46        FireCollectionChanged( e);
47    }
48
49    protected virtual void FireItemsAdded(IEnumerable<T> addedItems, int index) {
50        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, addedItems.ToList(), index);
51        FireCollectionChanged(e);
52    }
53
54    protected virtual void FireItemsRemoved(IEnumerable<T> removedItems) {
55        NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems.ToList());
56        FireCollectionChanged(e);
57    }
58    #endregion
59
60    #region ICollection<T> Members
61    public void Add(T item) {
62      int index = this.list.Count - 1;
63      this.list.Add(item);
64      FireItemsAdded(new List<T> { item }, index);
65    }
66
67    public void AddRange(IEnumerable<T> items) {
68      int index = this.list.Count - 1;
69      this.list.AddRange(items);
70      FireItemsAdded(items, index);
71    }
72
73    public void Clear() {
74      this.list.Clear();
75      FireCollectionCleared();
76    }
77
78    public bool Contains(T item) {
79      return this.list.Contains(item);
80    }
81
82    public void CopyTo(T[] array, int arrayIndex) {
83      this.list.CopyTo(array, arrayIndex);
84    }
85
86    public int Count {
87      get { return this.list.Count; }
88    }
89
90    public bool IsReadOnly {
91      get { return false; }
92    }
93
94    public bool Remove(T item) {
95      bool ret = this.list.Remove(item);
96      if (ret)
97        FireItemsRemoved(new List<T> { item });
98      return ret;
99    }
100
101    public void RemoveRange(IEnumerable<T> items) {
102      bool ret = false;
103      foreach (T item in items)
104        ret |= list.Remove(item);
105      if (ret)
106        FireItemsRemoved(items);
107    }
108
109    #endregion
110
111    #region IEnumerable<T> Members
112
113    public IEnumerator<T> GetEnumerator() {
114      return list.GetEnumerator();
115    }
116
117    #endregion
118
119    #region IEnumerable Members
120
121    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
122      return list.GetEnumerator();
123    }
124
125    #endregion
126
127
128  }
129}
Note: See TracBrowser for help on using the repository browser.