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
|
---|
21 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using System.Collections.Specialized;
|
---|
26 |
|
---|
27 | namespace 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 | IEnumerable<T> temp = this.list.AsEnumerable();
|
---|
75 | this.list.Clear();
|
---|
76 | FireItemsRemoved(temp);
|
---|
77 | FireCollectionCleared();
|
---|
78 | }
|
---|
79 |
|
---|
80 | public bool Contains(T item) {
|
---|
81 | return this.list.Contains(item);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void CopyTo(T[] array, int arrayIndex) {
|
---|
85 | this.list.CopyTo(array, arrayIndex);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public int Count {
|
---|
89 | get { return this.list.Count; }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public bool IsReadOnly {
|
---|
93 | get { return false; }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public bool Remove(T item) {
|
---|
97 | bool ret = this.list.Remove(item);
|
---|
98 | if (ret)
|
---|
99 | FireItemsRemoved(new List<T> { item });
|
---|
100 | return ret;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void RemoveRange(IEnumerable<T> items) {
|
---|
104 | bool ret = false;
|
---|
105 | foreach (T item in items)
|
---|
106 | ret |= list.Remove(item);
|
---|
107 | if (ret)
|
---|
108 | FireItemsRemoved(items);
|
---|
109 | }
|
---|
110 |
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region IEnumerable<T> Members
|
---|
114 |
|
---|
115 | public IEnumerator<T> GetEnumerator() {
|
---|
116 | return list.GetEnumerator();
|
---|
117 | }
|
---|
118 |
|
---|
119 | #endregion
|
---|
120 |
|
---|
121 | #region IEnumerable Members
|
---|
122 |
|
---|
123 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
---|
124 | return list.GetEnumerator();
|
---|
125 | }
|
---|
126 |
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 |
|
---|
130 | }
|
---|
131 | }
|
---|