1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation
7 using System.Activities.Presentation.Model;
8 using System.Collections.Generic;
9 using System.Collections.ObjectModel;
12 class DictionaryItemsCollection<TKey, TValue> : Collection<ModelItemKeyValuePair<TKey, TValue>>, IItemsCollection
14 IDictionary<TKey, TValue> dictionary;
15 public bool ShouldUpdateDictionary { get; set; }
16 public ModelItemDictionaryImpl ModelDictionary { get; set; }
18 public DictionaryItemsCollection(object dictionary)
20 this.ShouldUpdateDictionary = true;
21 this.dictionary = dictionary as IDictionary<TKey, TValue>;
22 Fx.Assert(this.dictionary != null, "dictionary should be instantiated");
23 foreach (KeyValuePair<TKey, TValue> kvpair in this.dictionary)
25 ModelItemKeyValuePair<TKey, TValue> item = new ModelItemKeyValuePair<TKey, TValue>(kvpair.Key, kvpair.Value);
26 item.collection = this;
27 base.InsertItem(this.Count, item);
31 internal void PostUpdateKey()
33 this.UpdateDictionary();
36 internal void PreUpdateKey(TKey oldKey, TKey newKey)
38 this.dictionary.Remove(oldKey);
39 if (this.dictionary.ContainsKey(newKey))
41 this.UpdateDictionary();
42 throw FxTrace.Exception.AsError(new ArgumentException(SR.DuplicateKey));
44 if (this.ModelDictionary != null)
46 this.ModelDictionary.UpdateKey(oldKey, newKey);
50 internal void UpdateValue(TKey key, TValue value)
52 if (ShouldUpdateDictionary)
54 this.dictionary[key] = value;
55 if (this.ModelDictionary != null)
57 this.ModelDictionary.UpdateValue(key, value);
62 protected override void ClearItems()
64 if (ShouldUpdateDictionary)
66 this.dictionary.Clear();
71 protected override void InsertItem(int index, ModelItemKeyValuePair<TKey, TValue> item)
75 throw FxTrace.Exception.AsError(new ArgumentNullException("item"));
78 if (ShouldUpdateDictionary && this.dictionary.ContainsKey(item.Key))
80 throw FxTrace.Exception.AsError(new ArgumentException(SR.DuplicateKey));
83 item.collection = this;
84 base.InsertItem(index, item);
86 this.UpdateDictionary();
89 protected override void RemoveItem(int index)
91 ModelItemKeyValuePair<TKey, TValue> item = this[index];
92 Fx.Assert(item != null, "Item should not be null.");
93 if (ShouldUpdateDictionary)
95 this.dictionary.Remove(item.Key);
98 base.RemoveItem(index);
101 protected override void SetItem(int index, ModelItemKeyValuePair<TKey, TValue> item)
105 throw FxTrace.Exception.AsError(new ArgumentNullException("item"));
107 item.collection = this;
108 ModelItemKeyValuePair<TKey, TValue> oldItem = this[index];
109 Fx.Assert(oldItem != null, "Item should not be null.");
110 this.PreUpdateKey(oldItem.Key, item.Key);
111 base.SetItem(index, item);
112 this.PostUpdateKey();
115 void UpdateDictionary()
117 if (ShouldUpdateDictionary)
119 // Make sure the order of KVPairs in the dictionary is the same as the order of items in the collection
120 this.dictionary.Clear();
121 foreach (ModelItemKeyValuePair<TKey, TValue> item in this)
123 this.dictionary.Add(new KeyValuePair<TKey, TValue>(item.Key, item.Value));