changeset 159:964f946d3cac

Fixes #288: WarFoundry WinForms assumes that army tree will be open * Add a getter method that returns a new army tree if the old one was disposed or non exists * Unhook the army tree from the army changed events on dispose
author IBBoard <dev@ibboard.co.uk>
date Fri, 04 Jun 2010 19:31:02 +0000
parents 0ae6aff8c862
children cd35c56d8153
files FrmArmyTree.cs FrmMain.cs
diffstat 2 files changed, 482 insertions(+), 468 deletions(-) [+]
line diff
     1.1 --- a/FrmArmyTree.cs	Mon May 31 20:09:12 2010 +0000
     1.2 +++ b/FrmArmyTree.cs	Fri Jun 04 19:31:02 2010 +0000
     1.3 @@ -1,461 +1,462 @@
     1.4 -// This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard.
     1.5 -//
     1.6 -// 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.
     1.7 -
     1.8 -using System;
     1.9 -using System.Drawing;
    1.10 -using System.Collections.Generic;
    1.11 -using System.ComponentModel;
    1.12 -using System.Windows.Forms;
    1.13 -using IBBoard.Commands;
    1.14 -using IBBoard.Lang;
    1.15 -using IBBoard.Windows.Forms.I18N;
    1.16 -using IBBoard.Windows.Forms;
    1.17 -using IBBoard.WarFoundry.API;
    1.18 -using IBBoard.WarFoundry.API.Commands;
    1.19 -using IBBoard.WarFoundry.API.Objects;
    1.20 -
    1.21 -namespace IBBoard.WarFoundry.GUI.WinForms
    1.22 -{
    1.23 -	/// <summary>
    1.24 -	/// Summary description for FrmArmyTree.
    1.25 -	/// </summary>
    1.26 -	public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm
    1.27 -	{
    1.28 -		private System.Windows.Forms.TreeView treeView;
    1.29 -		private IContainer components;
    1.30 -		private Dictionary<string, TreeNode> nodes;
    1.31 -		private System.Windows.Forms.ContextMenu contextMenu;
    1.32 -		private IBBMenuItem miDeleteUnit;
    1.33 -		private IBBMenuItem miEditUnit;
    1.34 -		
    1.35 -		private ObjectAddDelegate UnitAddedMethod;
    1.36 -		private ObjectRemoveDelegate UnitRemovedMethod;
    1.37 -		private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
    1.38 -		private ToolBar toolBar;
    1.39 -		private IBBToolBarButton bttnEdit;
    1.40 -		private IBBToolBarButton bttnDelete;
    1.41 -		private ImageList imageList;
    1.42 -		private CommandStack commandStack;
    1.43 -
    1.44 -		public FrmArmyTree(CommandStack cmdStack)
    1.45 -		{
    1.46 -			commandStack = cmdStack;
    1.47 -			InitializeComponent();
    1.48 -			UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
    1.49 -			UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
    1.50 -			UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
    1.51 -			ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
    1.52 -			TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
    1.53 -			nodes = new Dictionary<string, TreeNode>();
    1.54 -			this.Name = "ArmyTree";			
    1.55 -			WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
    1.56 -			TranslateForm();
    1.57 -			Translation.TranslationChanged += new MethodInvoker(TranslateForm);
    1.58 -		}
    1.59 -
    1.60 -		private void TranslateForm()
    1.61 -		{
    1.62 -			ControlTranslator.TranslateControl(this);
    1.63 -
    1.64 -			foreach (Component comp in components.Components)
    1.65 -			{
    1.66 -				ControlTranslator.TranslateComponent(comp);
    1.67 -			}
    1.68 -
    1.69 -			foreach (IBBMenuItem mi in contextMenu.MenuItems)
    1.70 -			{
    1.71 -				ControlTranslator.TranslateComponent(mi);
    1.72 -			}
    1.73 -		}
    1.74 -
    1.75 -		/// <summary>
    1.76 -		/// Clean up any resources being used.
    1.77 -		/// </summary>
    1.78 -		protected override void Dispose( bool disposing )
    1.79 -		{
    1.80 -			if( disposing )
    1.81 -			{
    1.82 -				if(components != null)
    1.83 -				{
    1.84 -					components.Dispose();
    1.85 -				}
    1.86 -			}
    1.87 -			base.Dispose( disposing );
    1.88 -			Translation.TranslationChanged -= new MethodInvoker(TranslateForm);
    1.89 -		}
    1.90 -
    1.91 -		#region Windows Form Designer generated code
    1.92 -		/// <summary>
    1.93 -		/// Required method for Designer support - do not modify
    1.94 -		/// the contents of this method with the code editor.
    1.95 -		/// </summary>
    1.96 -		private void InitializeComponent()
    1.97 -		{
    1.98 -			this.components = new System.ComponentModel.Container();
    1.99 -			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmArmyTree));
   1.100 -			this.contextMenu = new System.Windows.Forms.ContextMenu();
   1.101 -			this.miEditUnit = new IBBoard.Windows.Forms.IBBMenuItem();
   1.102 -			this.miDeleteUnit = new IBBoard.Windows.Forms.IBBMenuItem();
   1.103 -			this.imageList = new System.Windows.Forms.ImageList(this.components);
   1.104 -			this.toolBar = new System.Windows.Forms.ToolBar();
   1.105 -			this.bttnEdit = new IBBoard.Windows.Forms.IBBToolBarButton();
   1.106 -			this.bttnDelete = new IBBoard.Windows.Forms.IBBToolBarButton();
   1.107 -			this.treeView = new System.Windows.Forms.TreeView();
   1.108 -			this.SuspendLayout();
   1.109 -			// 
   1.110 -			// contextMenu
   1.111 -			// 
   1.112 -			this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
   1.113 -            this.miEditUnit,
   1.114 -            this.miDeleteUnit});
   1.115 -			this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup);
   1.116 -			// 
   1.117 -			// miEdit
   1.118 -			// 
   1.119 -			this.miEditUnit.Index = 0;
   1.120 -			this.miEditUnit.Text = "&edit unit";
   1.121 -			this.miEditUnit.Click += new System.EventHandler(this.miEdit_Click);
   1.122 -			this.miEditUnit.Name = "miEditUnit";
   1.123 -			// 
   1.124 -			// miDelete
   1.125 -			// 
   1.126 -			this.miDeleteUnit.Index = 1;
   1.127 -			this.miDeleteUnit.Text = "&delete unit";
   1.128 -			this.miDeleteUnit.Click += new System.EventHandler(this.miDelete_Click);
   1.129 -			this.miDeleteUnit.Name = "miDeleteUnit";
   1.130 -			// 
   1.131 -			// imageList
   1.132 -			// 
   1.133 -			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer) (resources.GetObject("imageList.ImageStream")));
   1.134 -			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
   1.135 -			this.imageList.Images.SetKeyName(0, "gtk-edit.png");
   1.136 -			this.imageList.Images.SetKeyName(1, "edit-delete.png");
   1.137 -			// 
   1.138 -			// toolBar
   1.139 -			// 
   1.140 -			this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
   1.141 -            this.bttnEdit,
   1.142 -            this.bttnDelete});
   1.143 -			this.toolBar.ButtonSize = new System.Drawing.Size(16, 16);
   1.144 -			this.toolBar.DropDownArrows = true;
   1.145 -			this.toolBar.ImageList = this.imageList;
   1.146 -			this.toolBar.Location = new System.Drawing.Point(0, 0);
   1.147 -			this.toolBar.Name = "toolBar";
   1.148 -			this.toolBar.ShowToolTips = true;
   1.149 -			this.toolBar.Size = new System.Drawing.Size(240, 28);
   1.150 -			this.toolBar.TabIndex = 3;
   1.151 -			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
   1.152 -			// 
   1.153 -			// bttnEdit
   1.154 -			// 
   1.155 -			this.bttnEdit.Enabled = false;
   1.156 -			this.bttnEdit.ImageIndex = 0;
   1.157 -			this.bttnEdit.Name = "bttnEdit";
   1.158 -			// 
   1.159 -			// bttnDelete
   1.160 -			// 
   1.161 -			this.bttnDelete.Enabled = false;
   1.162 -			this.bttnDelete.ImageIndex = 1;
   1.163 -			this.bttnDelete.Name = "bttnDelete";
   1.164 -			// 
   1.165 -			// treeView
   1.166 -			// 
   1.167 -			this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
   1.168 -						| System.Windows.Forms.AnchorStyles.Left)
   1.169 -						| System.Windows.Forms.AnchorStyles.Right)));
   1.170 -			this.treeView.ContextMenu = this.contextMenu;
   1.171 -			this.treeView.FullRowSelect = true;
   1.172 -			this.treeView.Location = new System.Drawing.Point(0, 28);
   1.173 -			this.treeView.Name = "treeView";
   1.174 -			this.treeView.Size = new System.Drawing.Size(240, 250);
   1.175 -			this.treeView.TabIndex = 0;
   1.176 -			this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick);
   1.177 -			this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
   1.178 -			this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);
   1.179 -			// 
   1.180 -			// FrmArmyTree
   1.181 -			// 
   1.182 -			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   1.183 -			this.ClientSize = new System.Drawing.Size(240, 277);
   1.184 -			this.ControlBox = false;
   1.185 -			this.Controls.Add(this.toolBar);
   1.186 -			this.Controls.Add(this.treeView);
   1.187 -			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
   1.188 -			this.MaximizeBox = false;
   1.189 -			this.MinimizeBox = false;
   1.190 -			this.Name = "FrmArmyTree";
   1.191 -			this.Text = "FrmArmyTree";
   1.192 -			this.ResumeLayout(false);
   1.193 -			this.PerformLayout();
   1.194 -
   1.195 -		}
   1.196 -		#endregion
   1.197 -
   1.198 -		private void ClearArmy()
   1.199 -		{
   1.200 -			nodes.Clear();
   1.201 -			treeView.Nodes.Clear();
   1.202 -		}
   1.203 -
   1.204 -		private void SetArmy(Army army)
   1.205 -		{
   1.206 -			ClearArmy();
   1.207 -
   1.208 -			if (army != null)
   1.209 -			{
   1.210 -				ArmyCategory[] cats = army.Categories;
   1.211 -				TreeNode[] catNodes = new TreeNode[cats.Length];
   1.212 -				Unit[] units;
   1.213 -
   1.214 -				for (int i = 0; i < cats.Length; i++)
   1.215 -				{
   1.216 -					ArmyCategory cat = cats[i];
   1.217 -					units = army.GetUnits(cat);
   1.218 -					cat.NameChanged += TreeNameChangedMethod;
   1.219 -					cat.PointsValueChanged += NodePointsValueChanged;
   1.220 -					TreeNode[] unitNodes = new TreeNode[units.Length];
   1.221 -					TreeNode temp;
   1.222 -
   1.223 -					for (int j = 0; j < units.Length; j++)
   1.224 -					{
   1.225 -						unitNodes[j] = CreateTreeNode(units[j]);
   1.226 -					}
   1.227 -
   1.228 -					temp = new TreeNode(CreateNodeName(cat), unitNodes);
   1.229 -					temp.Tag = cat;
   1.230 -					catNodes[i] = temp;
   1.231 -					nodes[cat.ID] = temp;
   1.232 -				}
   1.233 -
   1.234 -				TreeNode root = new TreeNode(CreateNodeName(army), catNodes);
   1.235 -				root.Tag = army;
   1.236 -				army.PointsValueChanged += NodePointsValueChanged;
   1.237 -				nodes[army.ID] = root;
   1.238 -				treeView.Nodes.Add(root);
   1.239 -				root.ExpandAll();
   1.240 -			}
   1.241 -		}
   1.242 -		
   1.243 -		private string CreateNodeName(ICostedWarFoundryObject obj)
   1.244 -		{
   1.245 -			return Translation.GetTranslation("treeNodeText", "{0} ({1}pts)", obj.Name, obj.Points);
   1.246 -		}
   1.247 -
   1.248 -		private void NodePointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
   1.249 -		{
   1.250 -			if (obj is ICostedWarFoundryObject)
   1.251 -			{
   1.252 -				SetNodeName(obj);
   1.253 -			}
   1.254 -		}
   1.255 -
   1.256 -		private void SetNodeName(WarFoundryObject obj)
   1.257 -		{
   1.258 -			TreeNode node = nodes[obj.ID];
   1.259 -
   1.260 -			if (node != null)
   1.261 -			{
   1.262 -				node.Text = CreateNodeName((ICostedWarFoundryObject)obj);
   1.263 -			}
   1.264 -		}
   1.265 -
   1.266 -		private TreeNode CreateTreeNode(Unit unit)
   1.267 -		{
   1.268 -			TreeNode temp = new TreeNode(CreateNodeName(unit));
   1.269 -			temp.Tag = unit;
   1.270 -			unit.NameChanged+= UnitNameChangedMethod;
   1.271 -			unit.PointsValueChanged += NodePointsValueChanged;
   1.272 -			nodes[unit.ID] = temp;
   1.273 -			return temp;
   1.274 -		}
   1.275 -
   1.276 -		public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
   1.277 -		{
   1.278 -			if (oldArmy != null)
   1.279 -			{
   1.280 -				oldArmy.UnitAdded -= UnitAddedMethod;
   1.281 -				oldArmy.UnitRemoved -= UnitRemovedMethod;
   1.282 -				oldArmy.PointsValueChanged -= NodePointsValueChanged;
   1.283 -			}
   1.284 -
   1.285 -			if (newArmy != null)
   1.286 -			{
   1.287 -				newArmy.UnitAdded += UnitAddedMethod;
   1.288 -				newArmy.UnitRemoved += UnitRemovedMethod;
   1.289 -				newArmy.PointsValueChanged += NodePointsValueChanged;
   1.290 -			}
   1.291 -
   1.292 -			SetArmy(newArmy);
   1.293 -		}
   1.294 -
   1.295 -		private void UnitAdded(WarFoundryObject obj)
   1.296 -		{
   1.297 -			if (obj is Unit)
   1.298 -			{
   1.299 -				Unit unit = (Unit)obj;
   1.300 -				ArmyCategory cat = unit.Category;
   1.301 -				TreeNode parent = nodes[cat.ID];
   1.302 -				TreeNode unitNode = CreateTreeNode(unit);
   1.303 -				parent.Nodes.Add(unitNode);
   1.304 -				parent.Expand(); //make sure it's expanded
   1.305 -			}
   1.306 -		}
   1.307 -
   1.308 -		public new FrmMain MdiParent
   1.309 -		{
   1.310 -			get { return (FrmMain) base.MdiParent; }
   1.311 -			set { base.MdiParent = value; }
   1.312 -		}
   1.313 -
   1.314 -		private void UnitRemoved(WarFoundryObject obj)
   1.315 -		{
   1.316 -			if (obj is Unit)
   1.317 -			{
   1.318 -				Unit unit = (Unit)obj;
   1.319 -				RemoveUnitFromTree(unit);
   1.320 -			}
   1.321 -		}
   1.322 -
   1.323 -		private void RemoveUnitFromTree(Unit unit)
   1.324 -		{
   1.325 -			TreeNode unitNode = nodes[unit.ID];
   1.326 -			unit.NameChanged-= UnitNameChangedMethod;
   1.327 -
   1.328 -			if (unitNode!=null)
   1.329 -			{
   1.330 -				unitNode.Remove();
   1.331 -				nodes.Remove(unit.ID);
   1.332 -			}
   1.333 -		}
   1.334 -
   1.335 -
   1.336 -		private void contextMenu_Popup(object sender, System.EventArgs e)
   1.337 -		{
   1.338 -			TreeNode node = treeView.SelectedNode;
   1.339 -
   1.340 -			if (node!=null && node.Tag is Unit)
   1.341 -			{
   1.342 -				foreach(MenuItem item in contextMenu.MenuItems)
   1.343 -				{
   1.344 -					item.Visible = true;
   1.345 -				}
   1.346 -			}
   1.347 -			else
   1.348 -			{
   1.349 -				foreach(MenuItem item in contextMenu.MenuItems)
   1.350 -				{
   1.351 -					item.Visible = false;
   1.352 -				}
   1.353 -			}
   1.354 -		}
   1.355 -
   1.356 -		private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   1.357 -		{
   1.358 -			if (e.Button == MouseButtons.Right)
   1.359 -			{
   1.360 -				TreeNode tn = treeView.GetNodeAt(e.X, e.Y);
   1.361 -
   1.362 -				if (tn!=null)
   1.363 -				{
   1.364 -					treeView.SelectedNode = tn;
   1.365 -				}
   1.366 -				else
   1.367 -				{
   1.368 -					treeView.SelectedNode = null;
   1.369 -				}
   1.370 -			}
   1.371 -		}
   1.372 -
   1.373 -		private void miDelete_Click(object sender, System.EventArgs e)
   1.374 -		{
   1.375 -			DeleteUnit();
   1.376 -		}
   1.377 -
   1.378 -		private void DeleteUnit()
   1.379 -		{
   1.380 -
   1.381 -			TreeNode selected = treeView.SelectedNode;
   1.382 -
   1.383 -			if (selected.Tag != null && selected.Tag is Unit)
   1.384 -			{
   1.385 -				Unit unit = (Unit) selected.Tag;
   1.386 -				commandStack.Execute(new RemoveUnitCommand(unit));
   1.387 -			}
   1.388 -		}
   1.389 -
   1.390 -		private void miEdit_Click(object sender, System.EventArgs e)
   1.391 -		{	
   1.392 -			EditUnit();
   1.393 -		}
   1.394 -
   1.395 -		private void treeView_DoubleClick(object sender, System.EventArgs e)
   1.396 -		{
   1.397 -			EditUnit();			
   1.398 -		}
   1.399 -
   1.400 -		private void EditUnit()
   1.401 -		{
   1.402 -			TreeNode selected = treeView.SelectedNode;
   1.403 -			object tagData = selected.Tag;
   1.404 -
   1.405 -			if (tagData is Unit)
   1.406 -			{
   1.407 -				Unit unit = (Unit) tagData;
   1.408 -				MdiParent.OpenUnitDialog(unit);
   1.409 -			}
   1.410 -		}
   1.411 -
   1.412 -		private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
   1.413 -		{
   1.414 -			if (obj is ICostedWarFoundryObject)
   1.415 -			{
   1.416 -				SetNodeName(obj);
   1.417 -			}
   1.418 -		}
   1.419 -
   1.420 -		private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue)
   1.421 -		{
   1.422 -			if (obj is Army)
   1.423 -			{
   1.424 -				Army army = (Army)obj;
   1.425 -				TreeNode node = treeView.Nodes[0];
   1.426 -				
   1.427 -				if (node!=null)
   1.428 -				{
   1.429 -					node.Text = army.Name;
   1.430 -				}
   1.431 -			}
   1.432 -		}
   1.433 -
   1.434 -		private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
   1.435 -		{
   1.436 -			TreeNode node = nodes[obj.ID];
   1.437 -
   1.438 -			if (node!=null)
   1.439 -			{
   1.440 -				node.Text = obj.Name;
   1.441 -			}
   1.442 -		}
   1.443 -
   1.444 -		private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
   1.445 -		{
   1.446 -			if (e.Button == bttnDelete)
   1.447 -			{
   1.448 -				DeleteUnit();
   1.449 -			}
   1.450 -			else if (e.Button == bttnEdit)
   1.451 -			{
   1.452 -				EditUnit();
   1.453 -			}
   1.454 -		}
   1.455 -
   1.456 -		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
   1.457 -		{
   1.458 -			TreeNode node = treeView.SelectedNode;
   1.459 -
   1.460 -			bttnDelete.Enabled = (node != null && node.Tag is Unit);
   1.461 -			bttnEdit.Enabled = bttnDelete.Enabled;
   1.462 -		}
   1.463 -	}
   1.464 -}
   1.465 +// This file (FrmArmyTree.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard.
   1.466 +//
   1.467 +// 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.
   1.468 +
   1.469 +using System;
   1.470 +using System.Drawing;
   1.471 +using System.Collections.Generic;
   1.472 +using System.ComponentModel;
   1.473 +using System.Windows.Forms;
   1.474 +using IBBoard.Commands;
   1.475 +using IBBoard.Lang;
   1.476 +using IBBoard.Windows.Forms.I18N;
   1.477 +using IBBoard.Windows.Forms;
   1.478 +using IBBoard.WarFoundry.API;
   1.479 +using IBBoard.WarFoundry.API.Commands;
   1.480 +using IBBoard.WarFoundry.API.Objects;
   1.481 +
   1.482 +namespace IBBoard.WarFoundry.GUI.WinForms
   1.483 +{
   1.484 +	/// <summary>
   1.485 +	/// Summary description for FrmArmyTree.
   1.486 +	/// </summary>
   1.487 +	public class FrmArmyTree : IBBoard.Windows.Forms.IBBForm
   1.488 +	{
   1.489 +		private System.Windows.Forms.TreeView treeView;
   1.490 +		private IContainer components;
   1.491 +		private Dictionary<string, TreeNode> nodes;
   1.492 +		private System.Windows.Forms.ContextMenu contextMenu;
   1.493 +		private IBBMenuItem miDeleteUnit;
   1.494 +		private IBBMenuItem miEditUnit;
   1.495 +		
   1.496 +		private ObjectAddDelegate UnitAddedMethod;
   1.497 +		private ObjectRemoveDelegate UnitRemovedMethod;
   1.498 +		private StringValChangedDelegate UnitNameChangedMethod, ArmyNameChangedMethod, TreeNameChangedMethod;
   1.499 +		private ToolBar toolBar;
   1.500 +		private IBBToolBarButton bttnEdit;
   1.501 +		private IBBToolBarButton bttnDelete;
   1.502 +		private ImageList imageList;
   1.503 +		private CommandStack commandStack;
   1.504 +
   1.505 +		public FrmArmyTree(CommandStack cmdStack)
   1.506 +		{
   1.507 +			commandStack = cmdStack;
   1.508 +			InitializeComponent();
   1.509 +			UnitAddedMethod = new ObjectAddDelegate(UnitAdded);
   1.510 +			UnitRemovedMethod = new ObjectRemoveDelegate(UnitRemoved);
   1.511 +			UnitNameChangedMethod = new StringValChangedDelegate(UpdateUnitName);
   1.512 +			ArmyNameChangedMethod = new StringValChangedDelegate(UpdateArmyName);
   1.513 +			TreeNameChangedMethod = new StringValChangedDelegate(FrmArmyTree_TreeNameChanged);
   1.514 +			nodes = new Dictionary<string, TreeNode>();
   1.515 +			this.Name = "ArmyTree";			
   1.516 +			WarFoundryCore.ArmyChanged+= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
   1.517 +			TranslateForm();
   1.518 +			Translation.TranslationChanged += new MethodInvoker(TranslateForm);
   1.519 +		}
   1.520 +
   1.521 +		private void TranslateForm()
   1.522 +		{
   1.523 +			ControlTranslator.TranslateControl(this);
   1.524 +
   1.525 +			foreach (Component comp in components.Components)
   1.526 +			{
   1.527 +				ControlTranslator.TranslateComponent(comp);
   1.528 +			}
   1.529 +
   1.530 +			foreach (IBBMenuItem mi in contextMenu.MenuItems)
   1.531 +			{
   1.532 +				ControlTranslator.TranslateComponent(mi);
   1.533 +			}
   1.534 +		}
   1.535 +
   1.536 +		/// <summary>
   1.537 +		/// Clean up any resources being used.
   1.538 +		/// </summary>
   1.539 +		protected override void Dispose( bool disposing )
   1.540 +		{
   1.541 +			if( disposing )
   1.542 +			{
   1.543 +				if(components != null)
   1.544 +				{
   1.545 +					components.Dispose();
   1.546 +				}
   1.547 +			}
   1.548 +			base.Dispose( disposing );
   1.549 +			Translation.TranslationChanged -= new MethodInvoker(TranslateForm);
   1.550 +			WarFoundryCore.ArmyChanged -= new ArmyChangedDelegate(FrmArmyTree_ArmyChanged);
   1.551 +		}
   1.552 +
   1.553 +		#region Windows Form Designer generated code
   1.554 +		/// <summary>
   1.555 +		/// Required method for Designer support - do not modify
   1.556 +		/// the contents of this method with the code editor.
   1.557 +		/// </summary>
   1.558 +		private void InitializeComponent()
   1.559 +		{
   1.560 +			this.components = new System.ComponentModel.Container();
   1.561 +			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmArmyTree));
   1.562 +			this.contextMenu = new System.Windows.Forms.ContextMenu();
   1.563 +			this.miEditUnit = new IBBoard.Windows.Forms.IBBMenuItem();
   1.564 +			this.miDeleteUnit = new IBBoard.Windows.Forms.IBBMenuItem();
   1.565 +			this.imageList = new System.Windows.Forms.ImageList(this.components);
   1.566 +			this.toolBar = new System.Windows.Forms.ToolBar();
   1.567 +			this.bttnEdit = new IBBoard.Windows.Forms.IBBToolBarButton();
   1.568 +			this.bttnDelete = new IBBoard.Windows.Forms.IBBToolBarButton();
   1.569 +			this.treeView = new System.Windows.Forms.TreeView();
   1.570 +			this.SuspendLayout();
   1.571 +			// 
   1.572 +			// contextMenu
   1.573 +			// 
   1.574 +			this.contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
   1.575 +            this.miEditUnit,
   1.576 +            this.miDeleteUnit});
   1.577 +			this.contextMenu.Popup += new System.EventHandler(this.contextMenu_Popup);
   1.578 +			// 
   1.579 +			// miEditUnit
   1.580 +			// 
   1.581 +			this.miEditUnit.Index = 0;
   1.582 +			this.miEditUnit.Text = "&edit unit";
   1.583 +			this.miEditUnit.Click += new System.EventHandler(this.miEdit_Click);
   1.584 +			this.miEditUnit.Name = "miEditUnit";
   1.585 +			// 
   1.586 +			// miDeleteUnit
   1.587 +			// 
   1.588 +			this.miDeleteUnit.Index = 1;
   1.589 +			this.miDeleteUnit.Text = "&delete unit";
   1.590 +			this.miDeleteUnit.Click += new System.EventHandler(this.miDelete_Click);
   1.591 +			this.miDeleteUnit.Name = "miDeleteUnit";
   1.592 +			// 
   1.593 +			// imageList
   1.594 +			// 
   1.595 +			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer) (resources.GetObject("imageList.ImageStream")));
   1.596 +			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
   1.597 +			this.imageList.Images.SetKeyName(0, "gtk-edit.png");
   1.598 +			this.imageList.Images.SetKeyName(1, "edit-delete.png");
   1.599 +			// 
   1.600 +			// toolBar
   1.601 +			// 
   1.602 +			this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
   1.603 +            this.bttnEdit,
   1.604 +            this.bttnDelete});
   1.605 +			this.toolBar.ButtonSize = new System.Drawing.Size(16, 16);
   1.606 +			this.toolBar.DropDownArrows = true;
   1.607 +			this.toolBar.ImageList = this.imageList;
   1.608 +			this.toolBar.Location = new System.Drawing.Point(0, 0);
   1.609 +			this.toolBar.Name = "toolBar";
   1.610 +			this.toolBar.ShowToolTips = true;
   1.611 +			this.toolBar.Size = new System.Drawing.Size(240, 28);
   1.612 +			this.toolBar.TabIndex = 3;
   1.613 +			this.toolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar_ButtonClick);
   1.614 +			// 
   1.615 +			// bttnEdit
   1.616 +			// 
   1.617 +			this.bttnEdit.Enabled = false;
   1.618 +			this.bttnEdit.ImageIndex = 0;
   1.619 +			this.bttnEdit.Name = "bttnEdit";
   1.620 +			// 
   1.621 +			// bttnDelete
   1.622 +			// 
   1.623 +			this.bttnDelete.Enabled = false;
   1.624 +			this.bttnDelete.ImageIndex = 1;
   1.625 +			this.bttnDelete.Name = "bttnDelete";
   1.626 +			// 
   1.627 +			// treeView
   1.628 +			// 
   1.629 +			this.treeView.Anchor = ((System.Windows.Forms.AnchorStyles) ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
   1.630 +						| System.Windows.Forms.AnchorStyles.Left)
   1.631 +						| System.Windows.Forms.AnchorStyles.Right)));
   1.632 +			this.treeView.ContextMenu = this.contextMenu;
   1.633 +			this.treeView.FullRowSelect = true;
   1.634 +			this.treeView.Location = new System.Drawing.Point(0, 28);
   1.635 +			this.treeView.Name = "treeView";
   1.636 +			this.treeView.Size = new System.Drawing.Size(240, 250);
   1.637 +			this.treeView.TabIndex = 0;
   1.638 +			this.treeView.DoubleClick += new System.EventHandler(this.treeView_DoubleClick);
   1.639 +			this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
   1.640 +			this.treeView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.treeView_MouseDown);
   1.641 +			// 
   1.642 +			// FrmArmyTree
   1.643 +			// 
   1.644 +			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   1.645 +			this.ClientSize = new System.Drawing.Size(240, 277);
   1.646 +			this.ControlBox = false;
   1.647 +			this.Controls.Add(this.toolBar);
   1.648 +			this.Controls.Add(this.treeView);
   1.649 +			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
   1.650 +			this.MaximizeBox = false;
   1.651 +			this.MinimizeBox = false;
   1.652 +			this.Name = "FrmArmyTree";
   1.653 +			this.Text = "FrmArmyTree";
   1.654 +			this.ResumeLayout(false);
   1.655 +			this.PerformLayout();
   1.656 +
   1.657 +		}
   1.658 +		#endregion
   1.659 +
   1.660 +		private void ClearArmy()
   1.661 +		{
   1.662 +			nodes.Clear();
   1.663 +			treeView.Nodes.Clear();
   1.664 +		}
   1.665 +
   1.666 +		private void SetArmy(Army army)
   1.667 +		{
   1.668 +			ClearArmy();
   1.669 +
   1.670 +			if (army != null)
   1.671 +			{
   1.672 +				ArmyCategory[] cats = army.Categories;
   1.673 +				TreeNode[] catNodes = new TreeNode[cats.Length];
   1.674 +				Unit[] units;
   1.675 +
   1.676 +				for (int i = 0; i < cats.Length; i++)
   1.677 +				{
   1.678 +					ArmyCategory cat = cats[i];
   1.679 +					units = army.GetUnits(cat);
   1.680 +					cat.NameChanged += TreeNameChangedMethod;
   1.681 +					cat.PointsValueChanged += NodePointsValueChanged;
   1.682 +					TreeNode[] unitNodes = new TreeNode[units.Length];
   1.683 +					TreeNode temp;
   1.684 +
   1.685 +					for (int j = 0; j < units.Length; j++)
   1.686 +					{
   1.687 +						unitNodes[j] = CreateTreeNode(units[j]);
   1.688 +					}
   1.689 +
   1.690 +					temp = new TreeNode(CreateNodeName(cat), unitNodes);
   1.691 +					temp.Tag = cat;
   1.692 +					catNodes[i] = temp;
   1.693 +					nodes[cat.ID] = temp;
   1.694 +				}
   1.695 +
   1.696 +				TreeNode root = new TreeNode(CreateNodeName(army), catNodes);
   1.697 +				root.Tag = army;
   1.698 +				army.PointsValueChanged += NodePointsValueChanged;
   1.699 +				nodes[army.ID] = root;
   1.700 +				treeView.Nodes.Add(root);
   1.701 +				root.ExpandAll();
   1.702 +			}
   1.703 +		}
   1.704 +		
   1.705 +		private string CreateNodeName(ICostedWarFoundryObject obj)
   1.706 +		{
   1.707 +			return Translation.GetTranslation("treeNodeText", "{0} ({1}pts)", obj.Name, obj.Points);
   1.708 +		}
   1.709 +
   1.710 +		private void NodePointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
   1.711 +		{
   1.712 +			if (obj is ICostedWarFoundryObject)
   1.713 +			{
   1.714 +				SetNodeName(obj);
   1.715 +			}
   1.716 +		}
   1.717 +
   1.718 +		private void SetNodeName(WarFoundryObject obj)
   1.719 +		{
   1.720 +			TreeNode node = nodes[obj.ID];
   1.721 +
   1.722 +			if (node != null)
   1.723 +			{
   1.724 +				node.Text = CreateNodeName((ICostedWarFoundryObject)obj);
   1.725 +			}
   1.726 +		}
   1.727 +
   1.728 +		private TreeNode CreateTreeNode(Unit unit)
   1.729 +		{
   1.730 +			TreeNode temp = new TreeNode(CreateNodeName(unit));
   1.731 +			temp.Tag = unit;
   1.732 +			unit.NameChanged+= UnitNameChangedMethod;
   1.733 +			unit.PointsValueChanged += NodePointsValueChanged;
   1.734 +			nodes[unit.ID] = temp;
   1.735 +			return temp;
   1.736 +		}
   1.737 +
   1.738 +		public void FrmArmyTree_ArmyChanged(Army oldArmy, Army newArmy)
   1.739 +		{
   1.740 +			if (oldArmy != null)
   1.741 +			{
   1.742 +				oldArmy.UnitAdded -= UnitAddedMethod;
   1.743 +				oldArmy.UnitRemoved -= UnitRemovedMethod;
   1.744 +				oldArmy.PointsValueChanged -= NodePointsValueChanged;
   1.745 +			}
   1.746 +
   1.747 +			if (newArmy != null)
   1.748 +			{
   1.749 +				newArmy.UnitAdded += UnitAddedMethod;
   1.750 +				newArmy.UnitRemoved += UnitRemovedMethod;
   1.751 +				newArmy.PointsValueChanged += NodePointsValueChanged;
   1.752 +			}
   1.753 +
   1.754 +			SetArmy(newArmy);
   1.755 +		}
   1.756 +
   1.757 +		private void UnitAdded(WarFoundryObject obj)
   1.758 +		{
   1.759 +			if (obj is Unit)
   1.760 +			{
   1.761 +				Unit unit = (Unit)obj;
   1.762 +				ArmyCategory cat = unit.Category;
   1.763 +				TreeNode parent = nodes[cat.ID];
   1.764 +				TreeNode unitNode = CreateTreeNode(unit);
   1.765 +				parent.Nodes.Add(unitNode);
   1.766 +				parent.Expand(); //make sure it's expanded
   1.767 +			}
   1.768 +		}
   1.769 +
   1.770 +		public new FrmMain MdiParent
   1.771 +		{
   1.772 +			get { return (FrmMain) base.MdiParent; }
   1.773 +			set { base.MdiParent = value; }
   1.774 +		}
   1.775 +
   1.776 +		private void UnitRemoved(WarFoundryObject obj)
   1.777 +		{
   1.778 +			if (obj is Unit)
   1.779 +			{
   1.780 +				Unit unit = (Unit)obj;
   1.781 +				RemoveUnitFromTree(unit);
   1.782 +			}
   1.783 +		}
   1.784 +
   1.785 +		private void RemoveUnitFromTree(Unit unit)
   1.786 +		{
   1.787 +			TreeNode unitNode = nodes[unit.ID];
   1.788 +			unit.NameChanged-= UnitNameChangedMethod;
   1.789 +
   1.790 +			if (unitNode!=null)
   1.791 +			{
   1.792 +				unitNode.Remove();
   1.793 +				nodes.Remove(unit.ID);
   1.794 +			}
   1.795 +		}
   1.796 +
   1.797 +
   1.798 +		private void contextMenu_Popup(object sender, System.EventArgs e)
   1.799 +		{
   1.800 +			TreeNode node = treeView.SelectedNode;
   1.801 +
   1.802 +			if (node!=null && node.Tag is Unit)
   1.803 +			{
   1.804 +				foreach(MenuItem item in contextMenu.MenuItems)
   1.805 +				{
   1.806 +					item.Visible = true;
   1.807 +				}
   1.808 +			}
   1.809 +			else
   1.810 +			{
   1.811 +				foreach(MenuItem item in contextMenu.MenuItems)
   1.812 +				{
   1.813 +					item.Visible = false;
   1.814 +				}
   1.815 +			}
   1.816 +		}
   1.817 +
   1.818 +		private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   1.819 +		{
   1.820 +			if (e.Button == MouseButtons.Right)
   1.821 +			{
   1.822 +				TreeNode tn = treeView.GetNodeAt(e.X, e.Y);
   1.823 +
   1.824 +				if (tn!=null)
   1.825 +				{
   1.826 +					treeView.SelectedNode = tn;
   1.827 +				}
   1.828 +				else
   1.829 +				{
   1.830 +					treeView.SelectedNode = null;
   1.831 +				}
   1.832 +			}
   1.833 +		}
   1.834 +
   1.835 +		private void miDelete_Click(object sender, System.EventArgs e)
   1.836 +		{
   1.837 +			DeleteUnit();
   1.838 +		}
   1.839 +
   1.840 +		private void DeleteUnit()
   1.841 +		{
   1.842 +
   1.843 +			TreeNode selected = treeView.SelectedNode;
   1.844 +
   1.845 +			if (selected.Tag != null && selected.Tag is Unit)
   1.846 +			{
   1.847 +				Unit unit = (Unit) selected.Tag;
   1.848 +				commandStack.Execute(new RemoveUnitCommand(unit));
   1.849 +			}
   1.850 +		}
   1.851 +
   1.852 +		private void miEdit_Click(object sender, System.EventArgs e)
   1.853 +		{	
   1.854 +			EditUnit();
   1.855 +		}
   1.856 +
   1.857 +		private void treeView_DoubleClick(object sender, System.EventArgs e)
   1.858 +		{
   1.859 +			EditUnit();			
   1.860 +		}
   1.861 +
   1.862 +		private void EditUnit()
   1.863 +		{
   1.864 +			TreeNode selected = treeView.SelectedNode;
   1.865 +			object tagData = selected.Tag;
   1.866 +
   1.867 +			if (tagData is Unit)
   1.868 +			{
   1.869 +				Unit unit = (Unit) tagData;
   1.870 +				MdiParent.OpenUnitDialog(unit);
   1.871 +			}
   1.872 +		}
   1.873 +
   1.874 +		private void UpdateUnitName(WarFoundryObject obj, string oldValue, string newValue)
   1.875 +		{
   1.876 +			if (obj is ICostedWarFoundryObject)
   1.877 +			{
   1.878 +				SetNodeName(obj);
   1.879 +			}
   1.880 +		}
   1.881 +
   1.882 +		private void UpdateArmyName(WarFoundryObject obj, string oldValue, string newValue)
   1.883 +		{
   1.884 +			if (obj is Army)
   1.885 +			{
   1.886 +				Army army = (Army)obj;
   1.887 +				TreeNode node = treeView.Nodes[0];
   1.888 +				
   1.889 +				if (node!=null)
   1.890 +				{
   1.891 +					node.Text = army.Name;
   1.892 +				}
   1.893 +			}
   1.894 +		}
   1.895 +
   1.896 +		private void FrmArmyTree_TreeNameChanged(WarFoundryObject obj, string oldValue, string newValue)
   1.897 +		{
   1.898 +			TreeNode node = nodes[obj.ID];
   1.899 +
   1.900 +			if (node!=null)
   1.901 +			{
   1.902 +				node.Text = obj.Name;
   1.903 +			}
   1.904 +		}
   1.905 +
   1.906 +		private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
   1.907 +		{
   1.908 +			if (e.Button == bttnDelete)
   1.909 +			{
   1.910 +				DeleteUnit();
   1.911 +			}
   1.912 +			else if (e.Button == bttnEdit)
   1.913 +			{
   1.914 +				EditUnit();
   1.915 +			}
   1.916 +		}
   1.917 +
   1.918 +		private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
   1.919 +		{
   1.920 +			TreeNode node = treeView.SelectedNode;
   1.921 +
   1.922 +			bttnDelete.Enabled = (node != null && node.Tag is Unit);
   1.923 +			bttnEdit.Enabled = bttnDelete.Enabled;
   1.924 +		}
   1.925 +	}
   1.926 +}
     2.1 --- a/FrmMain.cs	Mon May 31 20:09:12 2010 +0000
     2.2 +++ b/FrmMain.cs	Fri Jun 04 19:31:02 2010 +0000
     2.3 @@ -149,11 +149,6 @@
     2.4  			sbErrorPanel.Color = Color.Red;
     2.5  			sbPointsPanel.ToolTipText = Translation.GetTranslation("statusPanelPointsToolTip", "total points value");
     2.6  
     2.7 -			armyTree = new FrmArmyTree(CommandStack);
     2.8 -			armyTree.MdiParent = this;
     2.9 -			armyTree.StartPosition = FormStartPosition.Manual;
    2.10 -			armyTree.Location = new Point(this.DisplayRectangle.Width - armyTree.Width - 10, 10);
    2.11 -
    2.12  			// hack to load default files
    2.13  			WarFoundryLoader.GetDefault().AddLoadDirectory(new DirectoryInfo(Constants.ExecutablePath + Constants.DirectoryString + DefaultDataDir));
    2.14  			WarFoundryLoader.GetDefault().RegisterFactory(WarFoundryXmlFactory.GetFactory());
    2.15 @@ -161,6 +156,24 @@
    2.16  			WarFoundrySaver.SetFileSaver(new WarFoundryXmlSaver());
    2.17  		}
    2.18  
    2.19 +		private FrmArmyTree GetArmyTree()
    2.20 +		{
    2.21 +			if (armyTree == null || armyTree.IsDisposed)
    2.22 +			{
    2.23 +				CreateArmyTree();
    2.24 +			}
    2.25 +
    2.26 +			return armyTree;
    2.27 +		}
    2.28 +
    2.29 +		private void CreateArmyTree()
    2.30 +		{
    2.31 +			armyTree = new FrmArmyTree(CommandStack);
    2.32 +			armyTree.MdiParent = this;
    2.33 +			armyTree.StartPosition = FormStartPosition.Manual;
    2.34 +			armyTree.Location = new Point(this.DisplayRectangle.Width - armyTree.Width - 10, 10);
    2.35 +		}
    2.36 +
    2.37  		private void TranslateControls()
    2.38  		{
    2.39  			ControlTranslator.TranslateControls(Controls);
    2.40 @@ -924,7 +937,7 @@
    2.41  			miCloseArmy.Enabled = false;
    2.42  			miExportArmyAs.Enabled = false;
    2.43  			DisableCategoryButtons();
    2.44 -			armyTree.Hide();
    2.45 +			GetArmyTree().Hide();
    2.46  		}
    2.47  
    2.48  		private void SetNonNullArmyState(Army newArmy)
    2.49 @@ -934,7 +947,7 @@
    2.50  			miSaveArmyAs.Enabled = true;
    2.51  			miCloseArmy.Enabled = true;
    2.52  			miExportArmyAs.Enabled = true;
    2.53 -			armyTree.Show();
    2.54 +			GetArmyTree().Show();
    2.55  		}
    2.56  
    2.57  		private void SetCategoryButtons(Category[] cats)