Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.2/ObservableCollection.cs @ 2656

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

Committing first results of the refactoring of HeuristicLab.Core and related plugins (#95)

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