1 //------------------------------------------------------------------------------
2 // <copyright file="ModelItemCollection.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Activities.Presentation.Model {
9 using System.Activities.Presentation.Internal.Properties;
10 using System.Activities.Presentation;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Collections.Specialized;
16 using System.Globalization;
20 /// ModelItemCollection derives from ModelItem and implements
21 /// support for a collection of items.
23 /// ModelItemCollection defines a static attached property called
24 /// Item. This property is returned from the Properties
25 /// enumeration of the collection, in addition to any properties
26 /// defined on the collection. The Item property represents all
27 /// the items in the collection and is defined as type
28 /// IEnumerable of ModelItem. All items in the collection have
29 /// their Source property set to this property. The property
\92s metadata
30 /// marks it non browsable and non-serializable. The Item property is a
31 /// "pseudo" property because it is not actually set on the model. The
32 /// value it points to is the ModelItemCollection itself.
34 public abstract class ModelItemCollection : ModelItem, IList<ModelItem>, IList, INotifyCollectionChanged {
37 /// Creates a new ModelItemCollection.
39 protected ModelItemCollection() { }
42 /// Returns the item at the given index. Sets the item at the
43 /// given index to the given value.
45 /// <param name="index">The zero-based index into the collection.</param>
46 /// <returns></returns>
47 /// <exception cref="ArgumentNullException">if value is null.</exception>
48 /// <exception cref="IndexOutOfRangeException">if index is less than 0 or greater than or equal to count.</exception>
49 public abstract ModelItem this[int index] { get; set; }
52 /// Returns the count of items in the collection.
54 public abstract int Count { get; }
57 /// Returns true if the collection is a fixed size.
58 /// The default implementation returns true if the
59 /// collection is read only.
61 protected virtual bool IsFixedSize {
62 get { return IsReadOnly; }
66 /// Returns true if the collection cannot be modified.
68 public abstract bool IsReadOnly { get; }
71 /// Protected access to ICollection.IsSynchronized.
73 protected virtual bool IsSynchronized {
78 /// Protected access to the SyncRoot object used to synchronize
79 /// this collection. The default value returns "this".
81 protected virtual object SyncRoot {
86 /// This event is raised when the contents of this collection change.
88 public abstract event NotifyCollectionChangedEventHandler CollectionChanged;
91 /// Adds the item to the collection.
93 /// <param name="item"></param>
94 /// <exception cref="ArgumentNullException">if item is null.</exception>
95 /// <exception cref="InvalidOperationException">if the collection is read only.</exception>
96 public abstract void Add(ModelItem item);
99 /// Adds the given value to the collection. This will create an item
100 /// for the value. It returns the newly created item.
102 /// <param name="value"></param>
103 /// <returns>an item representing the value</returns>
104 /// <exception cref="ArgumentNullException">if value is null.</exception>
105 /// <exception cref="InvalidOperationException">if the collection is read only.</exception>
106 public abstract ModelItem Add(object value);
109 /// Clears the contents of the collection.
111 /// <exception cref="InvalidOperationException">if the collection is read only.</exception>
112 public abstract void Clear();
115 /// Returns true if the collection contains the given item.
117 /// <param name="item"></param>
118 /// <returns></returns>
119 /// <exception cref="ArgumentNullException">if item is null.</exception>
120 public abstract bool Contains(ModelItem item);
123 /// Returns true if the collection contains the given value.
125 /// <param name="value"></param>
126 /// <returns></returns>
127 /// <exception cref="ArgumentNullException">if value is null.</exception>
128 public abstract bool Contains(object value);
131 // Helper method that verifies that objects can be upcast to
134 private static ModelItem ConvertType(object value) {
136 return (ModelItem)value;
138 catch (InvalidCastException) {
139 throw FxTrace.Exception.AsError(new ArgumentException(
140 string.Format(CultureInfo.CurrentCulture,
141 Resources.Error_ArgIncorrectType,
142 "value", typeof(ModelItem).FullName)));
148 /// Copies the contents of the collection into the given array.
150 /// <exception cref="ArgumentNullException">if array is null.</exception>
151 /// <exception cref="IndexOutOfRangeException">
152 /// if arrayIndex is outside the bounds of the items array or if there is
153 /// insuffient space in the array to hold the collection.
155 public abstract void CopyTo(ModelItem[] array, int arrayIndex);
158 /// Returns an enumerator for the items in the collection.
160 /// <returns></returns>
161 public abstract IEnumerator<ModelItem> GetEnumerator();
164 /// Returns the index of the given item or -1 if the item does not exist.
166 /// <param name="item"></param>
167 /// <returns></returns>
168 /// <exception cref="ArgumentNullException">if item is null.</exception>
169 public abstract int IndexOf(ModelItem item);
172 /// Inserts the item at the given location. To
173 /// move an item, use Move. If index is == Count this will insert the item
174 /// at the end. If it is zero it will insert at the beginning.
176 /// <param name="index"></param>
177 /// <param name="item"></param>
178 /// <exception cref="ArgumentNullException">if item is null.</exception>
179 /// <exception cref="IndexOutOfRangeException">if index is less than 0 or greater than count.</exception>
180 public abstract void Insert(int index, ModelItem item);
183 /// Inserts the item at the given location. To
184 /// move an item, use Move. If index is == Count this will insert the item
185 /// at the end. If it is zero it will insert at the beginning.
187 /// <param name="index"></param>
188 /// <param name="value"></param>
189 /// <returns>an item representing the value</returns>
190 /// <exception cref="ArgumentNullException">if value is null.</exception>
191 /// <exception cref="IndexOutOfRangeException">if index is less than 0 or greater than count.</exception>
192 public abstract ModelItem Insert(int index, object value);
195 /// Moves the item at fromIndex to toIndex. The value for toIndex is
196 /// always where you want the item to be according to how the collection
197 /// currently sits. This means that if you are moving an item to a higher
198 /// index you don
\92t have to account for the fact that the indexes will
199 /// shuffle when the item is removed from its current location.
201 /// <param name="fromIndex">
202 /// The index of the item to move.
204 /// <param name="toIndex">
205 /// The index to move it to.
207 /// <exception cref="IndexOutOfRangeException">
208 /// if fromIndex or toIndex is less than zero or greater than or
211 public abstract void Move(int fromIndex, int toIndex);
214 /// Removes the item from the collection. This does nothing if the
215 /// item does not exist in the collection.
217 /// <param name="item"></param>
218 /// <exception cref="ArgumentNullException">if item is null.</exception>
219 public abstract bool Remove(ModelItem item);
222 /// Removes the value from the collection. This does nothing if the
223 /// value does not exist in the collection.
225 /// <param name="value"></param>
226 /// <exception cref="ArgumentNullException">if value is null.</exception>
227 public abstract bool Remove(object value);
230 /// Removes the item at the given index.
232 /// <param name="index"></param>
233 /// <exception cref="IndexOutOfRangeException">if index is less than 0 or greater than or equal to count.</exception>
234 public abstract void RemoveAt(int index);
237 /// This property is returned from the Properties enumeration of
238 /// the collection, in addition to any properties defined on the
239 /// collection. The Item property represents all the items in
240 /// the collection and is defined as type IEnumerable of ModelItem.
241 /// All items in the collection have their Source property set to
242 /// this property. The property
\92s metadata marks it non browsable
243 /// and non-serializable. The Item property is a "pseudo"property
244 /// because it is not actually set on the model. The value it points
245 /// to is the ModelItemCollection itself.
247 public static readonly DependencyProperty ItemProperty = DependencyProperty.RegisterAttachedReadOnly(
249 typeof(IEnumerable<ModelItem>),
250 typeof(ModelItemCollection), null).DependencyProperty;
252 #region IList Members
255 /// IList Implementation maps back to public API.
257 int IList.Add(object value) {
263 /// IList Implementation maps back to public API.
270 /// IList Implementation maps back to public API.
272 bool IList.Contains(object value) {
273 return Contains(value);
277 /// IList Implementation maps back to public API.
279 int IList.IndexOf(object value) {
280 return IndexOf(ConvertType(value));
284 /// IList Implementation maps back to public API.
286 void IList.Insert(int index, object value) {
287 Insert(index, value);
291 /// IList Implementation maps back to public API.
293 bool IList.IsFixedSize {
294 get { return IsFixedSize; }
298 /// IList Implementation maps back to public API.
300 bool IList.IsReadOnly {
301 get { return IsReadOnly; }
305 /// IList Implementation maps back to public API.
307 void IList.Remove(object value) {
312 /// IList Implementation maps back to public API.
314 void IList.RemoveAt(int index) {
319 /// IList Implementation maps back to public API.
321 object IList.this[int index] {
322 get { return this[index]; }
323 set { this[index] = ConvertType(value); }
328 #region ICollection Members
331 /// ICollection Implementation maps back to public API.
333 void ICollection.CopyTo(Array array, int index) {
334 for (int idx = 0; idx < Count; idx++) {
335 array.SetValue(this[idx], idx + index);
340 /// ICollection Implementation maps back to public API.
342 int ICollection.Count {
343 get { return Count; }
347 /// ICollection Implementation maps back to public API.
349 bool ICollection.IsSynchronized {
350 get { return IsSynchronized; }
354 /// ICollection Implementation maps back to public API.
356 object ICollection.SyncRoot {
357 get { return SyncRoot; }
362 #region IEnumerable Members
365 /// IEnumerable Implementation maps back to public API.
367 IEnumerator IEnumerable.GetEnumerator() {
368 foreach (object o in this) {