Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NonGenericObservableListWrapper.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;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Collections.Specialized;
7 using System.ComponentModel;
8 
9 namespace SiliconStudio.Presentation.Collections
10 {
11  /// <summary>
12  /// A class that wraps an instance of the <see cref="ObservableList{T}"/> class and implement the <see cref="IList"/> interface.
13  /// In some scenarii, <see cref="IList"/> does not support range changes on the collection (Especially when bound to a ListCollectionView).
14  /// This is why the <see cref="ObservableList{T}"/> class does not implement this interface directly. However this wrapper class can be used
15  /// when the <see cref="IList"/> interface is required.
16  /// </summary>
17  /// <typeparam name="T">The type of item contained in the <see cref="ObservableList{T}"/>.</typeparam>
18  public class NonGenericObservableListWrapper<T> : IList, IList<T>, INotifyPropertyChanged, INotifyCollectionChanged
19  {
20  private readonly ObservableList<T> list;
21  private readonly object syncRoot = new object();
22 
23  /// <summary>
24  /// Initializes a new instance of the <see cref="NonGenericObservableListWrapper{T}"/> class.
25  /// </summary>
26  /// <param name="list">The <see cref="ObservableList{T}"/> to wrap.</param>
28  {
29  this.list = list;
30  list.PropertyChanged += ListPropertyChanged;
31  list.CollectionChanged += ListCollectionChanged;
32  }
33 
34  /// <inheritdoc/>
35  public object this[int index] { get { return list[index]; } set { list[index] = (T)value; } }
36 
37  /// <inheritdoc/>
38  T IList<T>.this[int index] { get { return list[index]; } set { list[index] = value; } }
39 
40  /// <inheritdoc/>
41  public bool IsReadOnly { get { return list.IsReadOnly; } }
42 
43  /// <inheritdoc/>
44  public bool IsFixedSize { get { return false; } }
45 
46  /// <inheritdoc/>
47  public int Count { get { return list.Count; } }
48 
49  /// <inheritdoc/>
50  public object SyncRoot { get { return syncRoot; } }
51 
52  /// <inheritdoc/>
53  public bool IsSynchronized { get { return false; } }
54 
55  /// <inheritdoc/>
56  public event PropertyChangedEventHandler PropertyChanged;
57 
58  /// <inheritdoc/>
59  public event NotifyCollectionChangedEventHandler CollectionChanged;
60 
61  /// <inheritdoc/>
62  public IEnumerator GetEnumerator()
63  {
64  return list.GetEnumerator();
65  }
66 
67  /// <inheritdoc/>
68  IEnumerator<T> IEnumerable<T>.GetEnumerator()
69  {
70  return list.GetEnumerator();
71  }
72 
73  /// <inheritdoc/>
74  public void CopyTo(Array array, int index)
75  {
76  list.CopyTo((T[])array, index);
77  }
78 
79  /// <inheritdoc/>
80  public void CopyTo(T[] array, int arrayIndex)
81  {
82  list.CopyTo(array, arrayIndex);
83  }
84 
85  /// <inheritdoc/>
86  public int Add(object value)
87  {
88  list.Add((T)value);
89  return list.Count - 1;
90  }
91 
92  /// <inheritdoc/>
93  public void Add(T item)
94  {
95  list.Add(item);
96  }
97 
98  /// <inheritdoc/>
99  public bool Contains(object value)
100  {
101  return list.Contains((T)value);
102  }
103 
104  /// <inheritdoc/>
105  public bool Contains(T item)
106  {
107  return list.Contains(item);
108  }
109 
110  /// <inheritdoc/>
111  public void Clear()
112  {
113  list.Clear();
114  }
115 
116  /// <inheritdoc/>
117  public int IndexOf(object value)
118  {
119  return list.IndexOf((T)value);
120  }
121 
122  /// <inheritdoc/>
123  public int IndexOf(T item)
124  {
125  return list.IndexOf(item);
126  }
127 
128  /// <inheritdoc/>
129  public void Insert(int index, object value)
130  {
131  list.Insert(index, (T)value);
132  }
133 
134  /// <inheritdoc/>
135  public void Insert(int index, T item)
136  {
137  list.Insert(index, item);
138  }
139 
140  /// <inheritdoc/>
141  public void Remove(object value)
142  {
143  list.Remove((T)value);
144  }
145 
146  /// <inheritdoc/>
147  public bool Remove(T item)
148  {
149  return list.Remove(item);
150  }
151 
152  /// <inheritdoc/>
153  public void RemoveAt(int index)
154  {
155  list.RemoveAt(index);
156  }
157 
158  /// <inheritdoc/>
159  public override string ToString()
160  {
161  return string.Format("{{NonGenericObservableListWrapper}} Count = {0}", Count);
162  }
163 
164  private void ListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
165  {
166  var handler = CollectionChanged;
167  if (handler != null)
168  {
169  handler(this, e);
170  }
171  }
172 
173  private void ListPropertyChanged(object sender, PropertyChangedEventArgs e)
174  {
175  var handler = PropertyChanged;
176  if (handler != null)
177  {
178  handler(this, e);
179  }
180  }
181  }
182 }
NonGenericObservableListWrapper(ObservableList< T > list)
Initializes a new instance of the NonGenericObservableListWrapper{T} class.