comparison FrmArmyTree.cs @ 0:7dd160dacb60

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 42cf06b8f897
comparison
equal deleted inserted replaced
-1:000000000000 0:7dd160dacb60
1 using System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using IBBoard.Commands;
7 using IBBoard.WarFoundry.API;
8 using IBBoard.WarFoundry.API.Commands;
9 using IBBoard.Windows.Forms;
10
11 namespace IBBoard.WarFoundry
12 {
13 /// <summary>
14 /// Summary description for FrmArmyTree.
15 /// </summary>
16 public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm
17 {
18 private System.Windows.Forms.TreeView treeView;
19 /// <summary>
20 /// Required designer variable.
21 /// </summary>
22 private System.ComponentModel.Container components = null;
23 private Hashtable htNodes;
24 private System.Windows.Forms.ContextMenu contextMenu;
25 private System.Windows.Forms.MenuItem miDelete;
26 private System.Windows.Forms.MenuItem miEdit;
27 private Hashtable htUnitWindows;
28
29 private ObjectAddDelegate UnitAddedMethod;
30 private ObjectRemoveDelegate UnitRemovedMethod;
31 private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
32 private CommandStack commandStack;
33
34 public FrmArmyTree(CommandStack cmdStack)
35 {
36 commandStack = cmdStack;
37 InitializeComponent();
38 UnitAddedMethod = new ObjectAddDelegate(AddUnit);
39 UnitRemovedMethod = new ObjectRemoveDelegate(RemoveUnit);
40 UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
41 ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
42 TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
43 htNodes = new Hashtable();
44 htUnitWindows = new Hashtable();
45 this.Name = "ArmyTree";
46 WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
47 }
48
49 public FrmArmyTree(Army army, CommandStack cmdStack) : this(cmdStack)
50 {
51 SetArmy(army);
52 }
53
54 /// <summary>
55 /// Clean up any resources being used.
56 /// </summary>
57 protected override void Dispose( bool disposing )
58 {
59 if( disposing )
60 {
61 if(components != null)
62 {
63 components.Dispose();
64 }
65 }
66 base.Dispose( disposing );
67 }
68
69 #region Windows Form Designer generated code
70 /// <summary>
71 /// Required method for Designer support - do not modify
72 /// the contents of this method with the code editor.
73 /// </summary>
74 private void InitializeComponent()
75 {
76 this.treeView = new System.Windows.Forms.TreeView();
77 this.contextMenu = new System.Windows.Forms.ContextMenu();
78 this.miEdit = new System.Windows.Forms.MenuItem();
79 this.miDelete = new System.Windows.Forms.MenuItem();
80 this.SuspendLayout();
81 //
82 // treeView
83 //
84 this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
85 | System.Windows.Forms.AnchorStyles.Left)
86 | System.Windows.Forms.AnchorStyles.Right)));
87 this.treeView.ContextMenu = this.contextMenu;
88 this.treeView.FullRowSelect = true;
89 this.treeView.ImageIndex = -1;
90 this.treeView.Location = new System.Drawing.Point(0, 0);
91 this.treeView.Name = "treeView";
92 this.treeView.SelectedImageIndex = -1;
93 this.treeView.Size = new System.Drawing.Size(240, 278);
94 this.treeView.TabIndex = 0;
95 this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);
96 this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick);
97 //
98 // contextMenu
99 //
100 this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
101 this.miEdit,
102 this.miDelete});
103 this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup);
104 //
105 // miEdit
106 //
107 this.miEdit.Index = 0;
108 this.miEdit.Text = "&Edit unit";
109 this.miEdit.Click += new System.EventHandler(this.miEdit_Click);
110 //
111 // miDelete
112 //
113 this.miDelete.Index = 1;
114 this.miDelete.Text = "&Delete unit";
115 this.miDelete.Click += new System.EventHandler(this.miDelete_Click);
116 //
117 // FrmArmyTree
118 //
119 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
120 this.ClientSize = new System.Drawing.Size(240, 277);
121 this.ControlBox = false;
122 this.Controls.Add(this.treeView);
123 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
124 this.MaximizeBox = false;
125 this.MinimizeBox = false;
126 this.Name = "FrmArmyTree";
127 this.Text = "FrmArmyTree";
128 this.ResumeLayout(false);
129
130 }
131 #endregion
132
133 private void ClearArmy()
134 {
135 htNodes.Clear();
136 treeView.Nodes.Clear();
137
138 ArrayList list = new ArrayList(htUnitWindows.Values);
139
140 foreach (FrmUnit window in list)
141 {
142 window.Close();
143 }
144
145 htUnitWindows.Clear();
146 }
147
148 private void SetArmy(Army army)
149 {
150 if (army!=null)
151 {
152 ArmyCategory[] cats = army.Categories;
153 TreeNode[] catNodes = new TreeNode[cats.Length];
154 Unit[] units;
155
156 for (int i = 0; i<cats.Length; i++)
157 {
158 units = army.GetUnits(cats[i]);
159 cats[i].NameChanged += TreeNameChangedMethod;
160 TreeNode[] unitNodes = new TreeNode[units.Length];
161 TreeNode temp;
162
163 for (int j = 0; j<units.Length; j++)
164 {
165 unitNodes[j] = createTreeNode(units[j]);
166 }
167
168 temp = new TreeNode(cats[i].Name, unitNodes);
169 temp.Tag = cats[i];
170 catNodes[i] = temp;
171 htNodes[cats[i].ID] = temp;
172 }
173
174 TreeNode root = new TreeNode(army.Name, catNodes);
175 root.Tag = army;
176 treeView.Nodes.Add(root);
177 root.ExpandAll();
178 }
179 }
180
181 private TreeNode createTreeNode(Unit unit)
182 {
183 TreeNode temp = new TreeNode(unit.Name);
184 temp.Tag = unit;
185 unit.NameChanged+= UnitNameChangedMethod;
186 htNodes[unit.ID] = temp;
187 return temp;
188 }
189
190 /*private void FrmArmyTree_Move(object sender, System.EventArgs e)
191 {
192 if (ParentForm is FrmMain)
193 {
194 FrmMain main = (FrmMain)ParentForm;
195 main.Invoke(new MethodInvoker(main.MdiChildMoved));
196 }
197 }*/
198
199 public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
200 {
201 if (oldArmy != null)
202 {
203 oldArmy.UnitAdded -= UnitAddedMethod;
204 oldArmy.UnitRemoved -= UnitRemovedMethod;
205 }
206
207 if (newArmy != null)
208 {
209 newArmy.UnitAdded += UnitAddedMethod;
210 newArmy.UnitRemoved += UnitRemovedMethod;
211 SetArmy(newArmy);
212 }
213 else
214 {
215 ClearArmy();
216 }
217 }
218
219 private void AddUnit(object obj)
220 {
221 if (obj is Unit)
222 {
223 Unit unit = (Unit)obj;
224 ArmyCategory cat = unit.Category;
225 TreeNode parent = (TreeNode)htNodes[cat.ID];
226 TreeNode unitNode = createTreeNode(unit);
227 parent.Nodes.Add(unitNode);
228 parent.Expand(); //make sure it's expanded
229 }
230 }
231
232 private void RemoveUnit(object obj)
233 {
234 if (obj is Unit)
235 {
236 Unit unit = (Unit)obj;
237 removeUnit(unit);
238 }
239 }
240
241 private void removeUnit(Unit unit)
242 {
243 TreeNode unitNode = (TreeNode)htNodes[unit.ID];
244 unit.NameChanged-= UnitNameChangedMethod;
245
246 if (unitNode!=null)
247 {
248 unitNode.Remove();
249 htNodes.Remove(unit.ID);
250 }
251 }
252
253
254 private void contextMenu_Popup(object sender, System.EventArgs e)
255 {
256 TreeNode node = treeView.SelectedNode;
257 if (node!=null && node.Tag is Unit)
258 {
259 foreach(MenuItem item in contextMenu.MenuItems)
260 {
261 item.Visible = true;
262 }
263 }
264 else
265 {
266 foreach(MenuItem item in contextMenu.MenuItems)
267 {
268 item.Visible = false;
269 }
270 }
271 }
272
273 private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
274 {
275 if (e.Button == MouseButtons.Right)
276 {
277 TreeNode tn = treeView.GetNodeAt(e.X, e.Y);
278
279 if (tn!=null)
280 {
281 treeView.SelectedNode = tn;
282 }
283 else
284 {
285 treeView.SelectedNode = null;
286 }
287 }
288 }
289
290 private void miDelete_Click(object sender, System.EventArgs e)
291 {
292 TreeNode selected = treeView.SelectedNode;
293
294 if (selected.Tag!=null && selected.Tag is Unit)
295 {
296 Unit unit = (Unit)selected.Tag;
297 commandStack.Execute(new RemoveUnitCommand(unit));
298 }
299 }
300
301 private void miEdit_Click(object sender, System.EventArgs e)
302 {
303 editUnit();
304 }
305
306 private void treeView_DoubleClick(object sender, System.EventArgs e)
307 {
308 editUnit();
309 }
310
311 private void editUnit()
312 {
313 TreeNode selected = treeView.SelectedNode;
314
315 if (selected.Tag!=null && selected.Tag is Unit)
316 {
317 Unit unit = (Unit)selected.Tag;
318
319 if (htUnitWindows.ContainsKey(unit.ID))
320 {
321 ((FrmUnit)htUnitWindows[unit.ID]).Focus();
322 }
323 else
324 {
325 FrmUnit unitForm = new FrmUnit(unit, commandStack);
326 htUnitWindows.Add(unit.ID, unitForm);
327 unitForm.MdiParent = this.MdiParent;
328 unitForm.Closing+=new CancelEventHandler(unitForm_Closing);
329 unitForm.Show();
330 }
331 }
332 }
333
334 private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
335 {
336 if (obj is Unit)
337 {
338 Unit unit = (Unit)obj;
339 TreeNode node = (TreeNode)htNodes[unit.ID];
340
341 if (node!=null)
342 {
343 node.Text = unit.Name;
344 }
345 }
346 }
347
348 private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue)
349 {
350 if (obj is Army)
351 {
352 Army army = (Army)obj;
353 TreeNode node = treeView.Nodes[0];
354
355 if (node!=null)
356 {
357 node.Text = army.Name;
358 }
359 }
360 }
361
362 private void unitForm_Closing(object sender, CancelEventArgs e)
363 {
364 if (sender is FrmUnit)
365 {
366 FrmUnit unitForm = (FrmUnit)sender;
367 htUnitWindows.Remove(unitForm.Unit.ID);
368 }
369 }
370
371 private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
372 {
373 TreeNode node = (TreeNode)htNodes[obj.ID];
374
375 if (node!=null)
376 {
377 node.Text = obj.Name;
378 }
379 }
380 }
381 }