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