Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ObservableList.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using System.ComponentModel;
7 using System.Linq;
8 
9 namespace SiliconStudio.Presentation.Collections
10 {
11  public class ObservableList<T> : IList<T>, IObservableCollection<T>, IReadOnlyObservableCollection<T>
12  {
13  private readonly List<T> list;
14 
15  public ObservableList()
16  {
17  list = new List<T>();
18  }
19 
20  public ObservableList(IEnumerable<T> collection)
21  {
22  list = new List<T>(collection);
23  }
24 
25  public T this[int index]
26  {
27  get
28  {
29  return list[index];
30  }
31  set
32  {
33  var oldItem = list[index];
34  list[index] = value;
35  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, value, oldItem, index);
36  OnCollectionChanged(arg);
37  }
38  }
39 
40  public int Count { get { return list.Count; } }
41 
42  public bool IsReadOnly { get { return false; } }
43 
44  public event PropertyChangedEventHandler PropertyChanged;
45 
46  public event NotifyCollectionChangedEventHandler CollectionChanged;
47 
48  public IList ToIList()
49  {
50  return new NonGenericObservableListWrapper<T>(this);
51  }
52 
53  public IEnumerator<T> GetEnumerator()
54  {
55  return list.GetEnumerator();
56  }
57 
58  IEnumerator IEnumerable.GetEnumerator()
59  {
60  return GetEnumerator();
61  }
62 
63  public void Add(T item)
64  {
65  Insert(Count, item);
66  }
67 
68  public void AddRange(IEnumerable<T> items)
69  {
70  var itemList = items.ToList();
71  if (itemList.Count > 0)
72  {
73  list.AddRange(itemList);
74 
75  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, itemList, Count - itemList.Count);
76  OnCollectionChanged(arg);
77  }
78  }
79 
80  public void Clear()
81  {
82  list.Clear();
83  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
84  OnCollectionChanged(arg);
85  }
86 
87  public bool Contains(T item)
88  {
89  return list.Contains(item);
90  }
91 
92  public void CopyTo(T[] array, int arrayIndex)
93  {
94  list.CopyTo(array, arrayIndex);
95  }
96 
97  public bool Remove(T item)
98  {
99  int index = list.IndexOf(item);
100  if (index != -1)
101  {
102  RemoveAt(index);
103  }
104  return index != -1;
105  }
106 
107  public void RemoveRange(int index, int count)
108  {
109  var oldItems = list.Skip(index).Take(count).ToList();
110  list.RemoveRange(index, count);
111  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, oldItems, index);
112  OnCollectionChanged(arg);
113  }
114 
115  public int IndexOf(T item)
116  {
117  return list.IndexOf(item);
118  }
119 
120  public void Insert(int index, T item)
121  {
122  list.Insert(index, item);
123  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index);
124  OnCollectionChanged(arg);
125  }
126 
127  public void RemoveAt(int index)
128  {
129  var item = list[index];
130  list.RemoveAt(index);
131  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index);
132  OnCollectionChanged(arg);
133  }
134 
135  public void Reset()
136  {
137  var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
138  OnCollectionChanged(arg);
139  }
140 
141  /// <inheritdoc/>
142  public override string ToString()
143  {
144  return string.Format("{{ObservableList}} Count = {0}", Count);
145  }
146 
147  protected void OnCollectionChanged(NotifyCollectionChangedEventArgs arg)
148  {
149  var handler = CollectionChanged;
150  if (handler != null)
151  {
152  handler(this, arg);
153  }
154 
155  switch (arg.Action)
156  {
157  case NotifyCollectionChangedAction.Add:
158  case NotifyCollectionChangedAction.Remove:
159  case NotifyCollectionChangedAction.Reset:
160  OnPropertyChanged(new PropertyChangedEventArgs("Count"));
161  break;
162  }
163  }
164 
166  {
167  var handler = PropertyChanged;
168  if (handler != null)
169  {
170  handler(this, arg);
171  }
172  }
173  }
174 }
_In_ size_t count
Definition: DirectXTexP.h:174
A class that wraps an instance of the ObservableList{T} class and implement the IList interface...
NotifyCollectionChangedEventHandler CollectionChanged
void OnCollectionChanged(NotifyCollectionChangedEventArgs arg)