comparison FrmArmyTree.cs @ 45:1c74b51abac1

Re #137: Remove use of old collections * Replace hashtables with dictionaries in FrmArmyTree Also: * Correct some delegate method definitions * Rename a private "remove" method so that name doesn't only differ by capitalisation
author IBBoard <dev@ibboard.co.uk>
date Wed, 09 Sep 2009 19:56:24 +0000
parents 1486ccd744dc
children 93b3d23147ba
comparison
equal deleted inserted replaced
44:1486ccd744dc 45:1c74b51abac1
2 // 2 //
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. 3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
4 4
5 using System; 5 using System;
6 using System.Drawing; 6 using System.Drawing;
7 using System.Collections; 7 using System.Collections.Generic;
8 using System.ComponentModel; 8 using System.ComponentModel;
9 using System.Windows.Forms; 9 using System.Windows.Forms;
10 using IBBoard.Commands; 10 using IBBoard.Commands;
11 using IBBoard.WarFoundry.API; 11 using IBBoard.WarFoundry.API;
12 using IBBoard.WarFoundry.API.Commands; 12 using IBBoard.WarFoundry.API.Commands;
23 private System.Windows.Forms.TreeView treeView; 23 private System.Windows.Forms.TreeView treeView;
24 /// <summary> 24 /// <summary>
25 /// Required designer variable. 25 /// Required designer variable.
26 /// </summary> 26 /// </summary>
27 private System.ComponentModel.Container components = null; 27 private System.ComponentModel.Container components = null;
28 private Hashtable htNodes; 28 private Dictionary<string, TreeNode> htNodes;
29 private System.Windows.Forms.ContextMenu contextMenu; 29 private System.Windows.Forms.ContextMenu contextMenu;
30 private System.Windows.Forms.MenuItem miDelete; 30 private System.Windows.Forms.MenuItem miDelete;
31 private System.Windows.Forms.MenuItem miEdit; 31 private System.Windows.Forms.MenuItem miEdit;
32 private Hashtable htUnitWindows; 32 private Dictionary<string, FrmUnit> htUnitWindows;
33 33
34 private ObjectAddDelegate UnitAddedMethod; 34 private ObjectAddDelegate UnitAddedMethod;
35 private ObjectRemoveDelegate UnitRemovedMethod; 35 private ObjectRemoveDelegate UnitRemovedMethod;
36 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod; 36 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
37 private CommandStack commandStack; 37 private CommandStack commandStack;
43 UnitAddedMethod = new ObjectAddDelegate(AddUnit); 43 UnitAddedMethod = new ObjectAddDelegate(AddUnit);
44 UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit); 44 UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit);
45 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName); 45 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
46 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName); 46 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
47 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged); 47 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
48 htNodes = new Hashtable(); 48 htNodes = new Dictionary<string, TreeNode>();
49 htUnitWindows = new Hashtable(); 49 htUnitWindows = new Dictionary<string, FrmUnit>();
50 this.Name = "ArmyTree"; 50 this.Name = "ArmyTree";
51 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged); 51 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
52 } 52 }
53 53
54 public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack) 54 public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack)
137 137
138 private void ClearArmy() 138 private void ClearArmy()
139 { 139 {
140 htNodes.Clear(); 140 htNodes.Clear();
141 treeView.Nodes.Clear(); 141 treeView.Nodes.Clear();
142 ICollection unitFormsCollection = htUnitWindows.Values; 142 FrmUnit[] unitForms = DictionaryUtils.ToArray(htUnitWindows);
143 FrmUnit[] unitForms = new FrmUnit[unitFormsCollection.Count];
144 unitFormsCollection.CopyTo(unitForms, 0);
145 143
146 foreach (FrmUnit window in unitForms) 144 foreach (FrmUnit window in unitForms)
147 { 145 {
148 window.Close(); 146 window.Close();
149 } 147 }
218 newArmy.UnitRemoved += UnitRemovedMethod; 216 newArmy.UnitRemoved += UnitRemovedMethod;
219 } 217 }
220 218
221 SetArmy(newArmy); 219 SetArmy(newArmy);
222 } 220 }
223 221
224 private void AddUnit(object obj) 222 private void AddUnit(WarFoundryObject obj)
225 { 223 {
226 if (obj is Unit) 224 if (obj is Unit)
227 { 225 {
228 Unit unit = (Unit)obj; 226 Unit unit = (Unit)obj;
229 ArmyCategory cat = unit.Category; 227 ArmyCategory cat = unit.Category;
230 TreeNode parent = (TreeNode)htNodes[cat.ID]; 228 TreeNode parent = htNodes[cat.ID];
231 TreeNode unitNode = createTreeNode(unit); 229 TreeNode unitNode = createTreeNode(unit);
232 parent.Nodes.Add(unitNode); 230 parent.Nodes.Add(unitNode);
233 parent.Expand(); //make sure it's expanded 231 parent.Expand(); //make sure it's expanded
234 } 232 }
235 } 233 }
236 234
237 private void RemoveUnit(object obj) 235 private void RemoveUnit(WarFoundryObject obj)
238 { 236 {
239 if (obj is Unit) 237 if (obj is Unit)
240 { 238 {
241 Unit unit = (Unit)obj; 239 Unit unit = (Unit)obj;
242 removeUnit(unit); 240 RemoveUnitFromTree(unit);
243 } 241 }
244 } 242 }
245 243
246 private void removeUnit(Unit unit) 244 private void RemoveUnitFromTree(Unit unit)
247 { 245 {
248 TreeNode unitNode = (TreeNode)htNodes[unit.ID]; 246 TreeNode unitNode = htNodes[unit.ID];
249 unit.NameChanged-= UnitNameChangedMethod; 247 unit.NameChanged-= UnitNameChangedMethod;
250 248
251 if (unitNode!=null) 249 if (unitNode!=null)
252 { 250 {
253 unitNode.Remove(); 251 unitNode.Remove();
339 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue) 337 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
340 { 338 {
341 if (obj is Unit) 339 if (obj is Unit)
342 { 340 {
343 Unit unit = (Unit)obj; 341 Unit unit = (Unit)obj;
344 TreeNode node = (TreeNode)htNodes[unit.ID]; 342 TreeNode node = htNodes[unit.ID];
345 343
346 if (node!=null) 344 if (node!=null)
347 { 345 {
348 node.Text = unit.Name; 346 node.Text = unit.Name;
349 } 347 }
373 } 371 }
374 } 372 }
375 373
376 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue) 374 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
377 { 375 {
378 TreeNode node = (TreeNode)htNodes[obj.ID]; 376 TreeNode node = htNodes[obj.ID];
379 377
380 if (node!=null) 378 if (node!=null)
381 { 379 {
382 node.Text = obj.Name; 380 node.Text = obj.Name;
383 } 381 }