[2323] | 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;
|
---|
[2458] | 25 | using System.Collections.Specialized;
|
---|
[2323] | 26 |
|
---|
| 27 | namespace HeuristicLab.Common {
|
---|
[2656] | 28 | [Obsolete("Use collections of the HeuristicLab.Collections plugin instead", false)]
|
---|
[2458] | 29 | public class ObservableCollection<T> : ICollection<T>, INotifyCollectionChanged {
|
---|
| 30 | protected List<T> list;
|
---|
[2323] | 31 |
|
---|
| 32 | public ObservableCollection() {
|
---|
| 33 | list = new List<T>();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | #region Events
|
---|
[2458] | 37 | public event NotifyCollectionChangedEventHandler CollectionChanged;
|
---|
| 38 |
|
---|
| 39 | protected void FireCollectionChanged(NotifyCollectionChangedEventArgs e) {
|
---|
| 40 | if (CollectionChanged != null) {
|
---|
| 41 | CollectionChanged(this, e);
|
---|
| 42 | }
|
---|
[2323] | 43 | }
|
---|
| 44 |
|
---|
[2458] | 45 | protected virtual void FireCollectionCleared() {
|
---|
| 46 | NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
|
---|
| 47 | FireCollectionChanged( e);
|
---|
[2323] | 48 | }
|
---|
[2458] | 49 |
|
---|
| 50 | protected virtual void FireItemsAdded(IEnumerable<T> addedItems, int index) {
|
---|
| 51 | NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, addedItems.ToList(), index);
|
---|
| 52 | FireCollectionChanged(e);
|
---|
[2323] | 53 | }
|
---|
| 54 |
|
---|
[2458] | 55 | protected virtual void FireItemsRemoved(IEnumerable<T> removedItems) {
|
---|
| 56 | NotifyCollectionChangedEventArgs e = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItems.ToList());
|
---|
| 57 | FireCollectionChanged(e);
|
---|
[2323] | 58 | }
|
---|
| 59 | #endregion
|
---|
| 60 |
|
---|
| 61 | #region ICollection<T> Members
|
---|
| 62 | public void Add(T item) {
|
---|
[2458] | 63 | int index = this.list.Count - 1;
|
---|
[2323] | 64 | this.list.Add(item);
|
---|
[2458] | 65 | FireItemsAdded(new List<T> { item }, index);
|
---|
[2323] | 66 | }
|
---|
| 67 |
|
---|
| 68 | public void AddRange(IEnumerable<T> items) {
|
---|
[2458] | 69 | int index = this.list.Count - 1;
|
---|
[2323] | 70 | this.list.AddRange(items);
|
---|
[2458] | 71 | FireItemsAdded(items, index);
|
---|
[2323] | 72 | }
|
---|
| 73 |
|
---|
| 74 | public void Clear() {
|
---|
[2515] | 75 | IEnumerable<T> temp = this.list.ToArray<T>();
|
---|
[2323] | 76 | this.list.Clear();
|
---|
[2470] | 77 | FireItemsRemoved(temp);
|
---|
[2323] | 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
|
---|
[2458] | 129 |
|
---|
| 130 |
|
---|
[2323] | 131 | }
|
---|
| 132 | }
|
---|