2 using System.Collections.Generic;
4 namespace DirectOutput.General.Generic
10 public class NamedItemList<T> : ExtList<T> where T :
INamedItem
12 private Dictionary<string, T> _NameDict =
new Dictionary<string, T>();
18 public T
this[
string Name]
20 get {
return _NameDict[Name]; }
29 public bool Contains(
string Name)
31 return _NameDict.ContainsKey(Name);
38 public new void Sort()
40 Sort((T a, T b) => (a.Name.CompareTo(b.Name)));
44 #region Event handling
46 void NamedItemList_BeforeClear(
object sender, EventArgs e)
48 foreach (T Item
in this)
50 Item.BeforeNameChange -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
51 Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
56 void Item_AfterNameChanged(
object sender, NameChangeEventArgs e)
59 _NameDict.Remove(e.OldName);
61 _NameDict.Add(e.NewName, (T)sender);
65 void Item_BeforeNameChange(
object sender, NameChangeEventArgs e)
67 if (e.NewName != e.OldName)
69 if (_NameDict.ContainsKey(e.NewName))
71 throw new ArgumentException(
"Cant the the name of the INamedItem from {0} to {1}, because the new name does already exist in a colelction to which this object is assigned.".Build(e.OldName, e.NewName));
77 void NamedItemList_AfterRemove(
object sender, RemoveEventArgs<T> e)
79 if (_NameDict.ContainsKey(e.Item.Name))
81 _NameDict.Remove(e.Item.Name);
83 e.Item.BeforeNameChange -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
84 e.Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
88 void NamedItemList_AfterInsert(
object sender, InsertEventArgs<T> e)
90 _NameDict.Add(e.Item.Name, e.Item);
91 e.Item.BeforeNameChange +=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
92 e.Item.AfterNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
97 void NamedItemList_BeforeInsert(
object sender, InsertEventArgs<T> e)
99 if (_NameDict.ContainsKey(e.Item.Name))
101 throw new ArgumentException(
"Cant insert the INamedItem named {0}. The name does already exist in the collection.".Build(e.Item.Name));
105 void NamedItemList_AfterSet(
object sender, SetEventArgs<T> e)
107 if (_NameDict.ContainsKey(e.OldItem.Name))
109 _NameDict.Remove(e.OldItem.Name);
111 _NameDict.Add(e.NewItem.Name, e.NewItem);
114 e.OldItem.BeforeNameChange -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
115 e.OldItem.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
117 e.NewItem.BeforeNameChange +=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
118 e.NewItem.AfterNameChanged +=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
122 void NamedItemList_BeforeSet(
object sender, SetEventArgs<T> e)
124 if (!EqualityComparer<T>.Default.Equals(e.NewItem, e.OldItem))
126 if (!_NameDict.ContainsKey(e.NewItem.Name))
130 else if (_NameDict.ContainsKey(e.NewItem.Name) && EqualityComparer<T>.Default.Equals(e.OldItem, _NameDict[e.NewItem.Name]))
136 throw new ArgumentException(
"Cant set the INamedItem. The name {0} does already exist in the collection.".Build(e.NewItem.Name));
153 public NamedItemList()
155 this.BeforeInsert +=
new EventHandler<InsertEventArgs<T>>(NamedItemList_BeforeInsert);
156 this.AfterInsert +=
new EventHandler<InsertEventArgs<T>>(NamedItemList_AfterInsert);
157 this.BeforeSet +=
new EventHandler<SetEventArgs<T>>(NamedItemList_BeforeSet);
158 this.AfterSet +=
new EventHandler<SetEventArgs<T>>(NamedItemList_AfterSet);
159 this.AfterRemove +=
new EventHandler<RemoveEventArgs<T>>(NamedItemList_AfterRemove);
160 this.BeforeClear +=
new EventHandler<EventArgs>(NamedItemList_BeforeClear);
169 public NamedItemList(IEnumerable<T> EnumerableList)
172 AddRange(EnumerableList);
181 foreach (T Item
in this)
183 Item.BeforeNameChange -=
new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
184 Item.AfterNameChanged -=
new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);