WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
NamedItemList.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 
4 namespace DirectOutput.General.Generic
5 {
10  public class NamedItemList<T> : ExtList<T> where T : INamedItem
11  {
12  private Dictionary<string, T> _NameDict = new Dictionary<string, T>();
13 
18  public T this[string Name]
19  {
20  get { return _NameDict[Name]; }
21  }
22 
23 
29  public bool Contains(string Name)
30  {
31  return _NameDict.ContainsKey(Name);
32  }
33 
34 
38  public new void Sort()
39  {
40  Sort((T a, T b) => (a.Name.CompareTo(b.Name)));
41  }
42 
43 
44  #region Event handling
45 
46  void NamedItemList_BeforeClear(object sender, EventArgs e)
47  {
48  foreach (T Item in this)
49  {
50  Item.BeforeNameChanged -= new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
51  Item.AfterNameChanged -= new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
52  }
53  }
54 
55 
56  void Item_AfterNameChanged(object sender, NameChangeEventArgs e)
57  {
58 
59  _NameDict.Remove(e.OldName);
60 
61  _NameDict.Add(e.NewName, (T)sender);
62 
63  }
64 
65  void Item_BeforeNameChange(object sender, NameChangeEventArgs e)
66  {
67  if (e.NewName != e.OldName)
68  {
69  if (_NameDict.ContainsKey(e.NewName))
70  {
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));
72  }
73  }
74  }
75 
76 
77  void NamedItemList_AfterRemove(object sender, RemoveEventArgs<T> e)
78  {
79  if (_NameDict.ContainsKey(e.Item.Name))
80  {
81  _NameDict.Remove(e.Item.Name);
82  }
83  e.Item.BeforeNameChanged -= new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
84  e.Item.AfterNameChanged -= new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
85  }
86 
87 
88  void NamedItemList_AfterInsert(object sender, InsertEventArgs<T> e)
89  {
90  _NameDict.Add(e.Item.Name, e.Item);
91  e.Item.BeforeNameChanged += new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
92  e.Item.AfterNameChanged += new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
93  }
94 
95 
96 
97  void NamedItemList_BeforeInsert(object sender, InsertEventArgs<T> e)
98  {
99  if (_NameDict.ContainsKey(e.Item.Name))
100  {
101  throw new ArgumentException("Cant insert the INamedItem named {0}. The name does already exist in the collection.".Build(e.Item.Name));
102  }
103  }
104 
105  void NamedItemList_AfterSet(object sender, SetEventArgs<T> e)
106  {
107  if (_NameDict.ContainsKey(e.OldItem.Name))
108  {
109  _NameDict.Remove(e.OldItem.Name);
110  };
111  _NameDict.Add(e.NewItem.Name, e.NewItem);
112 
113 
114  e.OldItem.BeforeNameChanged -= new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
115  e.OldItem.AfterNameChanged -= new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
116 
117  e.NewItem.BeforeNameChanged += new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
118  e.NewItem.AfterNameChanged += new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
119 
120  }
121 
122  void NamedItemList_BeforeSet(object sender, SetEventArgs<T> e)
123  {
124  if (!EqualityComparer<T>.Default.Equals(e.NewItem, e.OldItem))
125  {
126  if (!_NameDict.ContainsKey(e.NewItem.Name))
127  {
128  //Name of the new item does not exist.
129  }
130  else if (_NameDict.ContainsKey(e.NewItem.Name) && EqualityComparer<T>.Default.Equals(e.OldItem, _NameDict[e.NewItem.Name]))
131  {
132  //Name does exist, but its the name of the old item.
133  }
134  else
135  {
136  throw new ArgumentException("Cant set the INamedItem. The name {0} does already exist in the collection.".Build(e.NewItem.Name));
137  }
138 
139  }
140  }
141 
142 
143 
144 
145 
146  #endregion
147 
148  #region Constructors
149 
153  public NamedItemList()
154  {
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);
161  }
162 
163 
164 
169  public NamedItemList(IEnumerable<T> EnumerableList)
170  : this()
171  {
172  AddRange(EnumerableList);
173  }
174  #endregion
175 
179  ~NamedItemList()
180  {
181  foreach (T Item in this)
182  {
183  Item.BeforeNameChanged -= new EventHandler<NameChangeEventArgs>(Item_BeforeNameChange);
184  Item.AfterNameChanged -= new EventHandler<NameChangeEventArgs>(Item_AfterNameChanged);
185  }
186  }
187 
188  }
189 }
bool Contains(string Name)
Checks if a INamedItem object with the specified name exists in the list.
Interface for items which can be added the the NamedItemList.
Definition: INamedItem.cs:8
new void Sort()
Sorts the NamedItemList by the Name of the items.
NamedItemList(IEnumerable< T > EnumerableList)
Initializes a new instance of the NamedItemList class.
A list of uniquely named items which can be referenced by their name.
Extended version of the generic List class supporting events for various actions on the list...
Definition: ExtList.cs:11
NamedItemList()
Initializes a new instance of the NamedItemList class.