view FrmUnit.cs @ 210:694f23814ec2

Re #354: Single-button Add Unit * Make the form translatable * Update FrmArmyTree so it doesn't look out of place (ContextMenuStrip and update the icons) * Add "change selection on right-click" code to solve calculation issues * Hide categories that don't have entries (assume they're not relevant to that race) Also: * Clean up code and replace space indents with tabs
author IBBoard <dev@ibboard.co.uk>
date Sat, 20 Aug 2011 20:58:16 +0100
parents eaa7b639d390
children 72beddaffb71
line wrap: on
line source

// This file (FrmUnit.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2007, 2008, 2009 IBBoard.
//
// 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.

using System;
using System.Drawing;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using IBBoard.Commands;
using IBBoard.Lang;
using IBBoard.Windows.Forms;
using IBBoard.Windows.Forms.I18N;
using IBBoard.WarFoundry.API;
using IBBoard.WarFoundry.API.Commands;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.GUI.WinForms.UI;
using IBBoard.WarFoundry.GUI.WinForms.Util;
using log4net;

namespace IBBoard.WarFoundry.GUI.WinForms
{
	/// <summary>
	/// Summary description for FrmUnit.
	/// </summary>
	public class FrmUnit : IBBoard.Windows.Forms.IBBForm
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(FrmUnit));
		private static readonly int BORDER_WIDTH = 1;
		private static readonly int NAME_COL_WIDTH_MULTIPLIER = 3;
		private Unit unit;
		private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>();
		private Dictionary<string, DataGridView> DataGridViews = new Dictionary<string, DataGridView>();
		private CommandStack commandStack;
		private System.Windows.Forms.TextBox tbUnitName;
		private System.Windows.Forms.NumericUpDown unitSize;
		private IBBLabel lblUnitSize;
		private IBBButton bttnAddWeapon;
		private IBBButton bttnRemoveWeapon;
		private IBBLabel lblEquip;
		private System.Windows.Forms.ListBox equipmentList;
		private IBBButton bttnReplaceWeapon;
		private IBBButton bttnEditWeapon;
		private Label lblPoints;
		private IBBLabel lblNotes;
		private TextBox notes;
		private ListBox abilitiesList;
		private IBBLabel lblAbilities;
		private FlowLayoutPanel statsPanel;
        public GameSystem CurrentGameSystem
        {
            get { return WarFoundryCore.CurrentGameSystem; }
            set { WarFoundryCore.CurrentGameSystem = value; }
        }

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FrmUnit(Unit toDisplay, CommandStack cmdStack)
		{
			unit = toDisplay;
			commandStack = cmdStack;
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			TranslateForm();
			Translation.TranslationChanged += new MethodInvoker(TranslateForm);
			unit.NameChanged += new StringValChangedDelegate(unit_NameChanged);
			unit.UnitSizeChanged += new IntValChangedDelegate(unit_UnitSizeChanged);
			unit.UnitEquipmentAmountChanged += new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged);
			unit.PointsValueChanged += new DoubleValChangedDelegate(unit_PointsValueChanged);

			unitSize.Value = unit.Size;
			unitSize.Maximum = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize);
			unitSize.Minimum = unit.UnitType.MinSize;
			unitSize.Enabled = (unitSize.Maximum != unitSize.Minimum);

			notes.Text = unit.UnitType.Notes;
			abilitiesList.DataSource = new List<Ability>(unit.UnitType.GetRequiredAbilities());
			abilitiesList.DisplayMember = "Name";
			SetPointsValueText();
			SetStats();
			SetWeapons();
		}

		private void TranslateForm()
		{
			ControlTranslator.TranslateControl(this);
			SetUnitName();
			RefreshUnitEquipment();
		}

		private void SetUnitName()
		{
			tbUnitName.Text = unit.Name;
			Text = Translation.GetTranslation("FrmUnit", "{0} ({1})", unit.Name, unit.UnitType.Name);
		}

		void unit_PointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
		{
			SetPointsValueText();
		}

		private void SetPointsValueText()
		{
            lblPoints.Text = Translation.GetTranslation("FrmUnitlblPoints", "(" + unit.Points + " pts)", unit.Points, CurrentGameSystem.GetPointsAbbrev(unit.Points));
		}

		private void SetStats()
		{
			Stat[][] stats = unit.UnitStatsArraysWithName;
			string[] statsIDs = unit.UnitStatsArrayIDs;
			int statsCount = stats.Length;
			log.DebugFormat("Unit {0} has {1} stats arrays", unit.UnitType.Name, statsCount);

			for (int i = 0; i < statsCount; i++)
			{
				DataGridView statsGrid = GetDataGridView(statsIDs[i]);
				DataTable dt = (DataTable)statsGrid.DataSource;
				DataRow dr = dt.NewRow();
				dr.ItemArray = stats[i];
				log.DebugFormat("Add row to data table for {0}", statsIDs[i]);
				dt.Rows.Add(dr);
				statsGrid.ClearSelection();
			}
		}

		private DataGridView GetDataGridView(string statsID)
		{
			DataGridView grid;

			if (DataGridViews.ContainsKey(statsID))
			{
				grid = DictionaryUtils.GetValue(DataGridViews, statsID);
			}
			else
			{
				grid = CreateDataGridView(statsID);
				DataGridViews[statsID] = grid;
			}

			return grid;
		}

		private DataGridView CreateDataGridView(string statsID)
		{
			log.DebugFormat("Create DataGridView for stats ID {0}", statsID);
			SystemStats sysStats = unit.Race.GameSystem.GetSystemStatsForID(statsID);
			StatSlot[] sysStatSlots = sysStats.StatSlots;
			int statsCount = sysStatSlots.Length;
			int statsWithNameCount = statsCount + 1;
			StatSlot[] statsWithName = new StatSlot[statsWithNameCount];
			statsWithName[0] = new StatSlot("Name");
			sysStatSlots.CopyTo(statsWithName, 1);
			DataTable dt = new DataTable();
			DataGridView statsGrid = CreateDataGridView();
			statsGrid.DataSource = dt;
			int columnWidth = statsGrid.Width / (statsCount + NAME_COL_WIDTH_MULTIPLIER);

			for (int i = 0; i < statsWithNameCount; i++)
			{
				StatSlot stat = statsWithName[i];
				string slotName = stat.Name;
				statsGrid.Columns.Add(CreateStatColumn(slotName, columnWidth));
				dt.Columns.Add(CreateDataColumn(slotName));
			}

			int otherStatsWidth = statsCount * columnWidth;
			SetNameColumnWidth(statsGrid, otherStatsWidth);
			
			return statsGrid;
		}

		private static DataGridViewColumn CreateStatColumn(string slotName, int columnWidth)
		{
			DataGridViewColumn col = new DataGridViewTextBoxColumn();
			col.Width = columnWidth;
			col.Name = slotName;
			col.HeaderText = slotName;
			col.DataPropertyName = slotName;
			col.SortMode = DataGridViewColumnSortMode.NotSortable;
			col.CellTemplate = new StatsDataGridViewCell();
			col.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
			return col;
		}

		private static void SetNameColumnWidth(DataGridView statsGrid, int otherStatsWidth)
		{

			DataGridViewColumn nameColumn = statsGrid.Columns[0];
			nameColumn.HeaderText = Translation.GetTranslation("StatLineName", "name");
			nameColumn.Width = statsGrid.Width - otherStatsWidth;
		}

		private static DataColumn CreateDataColumn(string slotName)
		{
			log.DebugFormat("Create column {0}", slotName);
			DataColumn tempCol = new DataColumn(slotName,  typeof(Stat));
			return tempCol;
		}

		public DataGridView CreateDataGridView()
		{
			log.Debug("Create DataGridView widget");
			DataGridView statsGrid = new DataGridView();
			statsGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			statsGrid.CausesValidation = false;
			statsGrid.ReadOnly = true;
			statsGrid.RowHeadersVisible = false;
			statsGrid.Size = new System.Drawing.Size(600, 88);
			statsGrid.TabStop = false;
			statsGrid.AllowUserToAddRows = false;
			statsGrid.ScrollBars = ScrollBars.None;
			statsGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			statsGrid.BorderStyle = BorderStyle.None;
			statsGrid.BackgroundColor = SystemColors.Control;
			statsGrid.RowsAdded += new DataGridViewRowsAddedEventHandler(statsGrid_RowsAdded);
			statsPanel.Controls.Add(statsGrid);
			statsGrid.Width = statsPanel.Width - (int)Math.Round(SystemInformation.VerticalScrollBarWidth * 1.4);
			return statsGrid;
		}

		private void statsGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			if (sender is DataGridView)
			{
				SetGridHeight((DataGridView)sender);
			}
		}

		private static void SetGridHeight(DataGridView statsGrid)
		{
			DataGridViewRowCollection rows = statsGrid.Rows;
			statsGrid.Height = statsGrid.Columns[0].HeaderCell.Size.Height + (rows.Count * rows[0].Height);
			log.DebugFormat("Set height to {0} for grid of {1} rows", statsGrid.Height, rows.Count);
		}

		private void SetWeapons()
		{
			foreach (UnitEquipmentItem item in unit.GetEquipment())
			{
				equipmentList.Items.Add(GetEquipmentChoice(item));
			}
		}

		private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item)
		{
			UnitEquipmentChoice choice = null;
			equipmentChoices.TryGetValue(item, out choice);

			if (choice == null)
			{
				choice = new UnitEquipmentChoice(Unit, item);
				equipmentChoices[item] = choice;
			}

			return choice;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			//remove our leave events so that disposing doesn't trigger them
			tbUnitName.Leave -= new System.EventHandler(this.tbUnitName_Leave);
			unitSize.Leave -= new System.EventHandler(this.unitSize_Leave);

			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.tbUnitName = new System.Windows.Forms.TextBox();
			this.unitSize = new System.Windows.Forms.NumericUpDown();
			this.lblUnitSize = new IBBoard.Windows.Forms.IBBLabel();
			this.lblEquip = new IBBoard.Windows.Forms.IBBLabel();
			this.bttnAddWeapon = new IBBoard.Windows.Forms.IBBButton();
			this.bttnRemoveWeapon = new IBBoard.Windows.Forms.IBBButton();
			this.equipmentList = new System.Windows.Forms.ListBox();
			this.bttnReplaceWeapon = new IBBoard.Windows.Forms.IBBButton();
			this.bttnEditWeapon = new IBBoard.Windows.Forms.IBBButton();
			this.lblPoints = new System.Windows.Forms.Label();
			this.lblNotes = new IBBoard.Windows.Forms.IBBLabel();
			this.notes = new System.Windows.Forms.TextBox();
			this.abilitiesList = new System.Windows.Forms.ListBox();
			this.lblAbilities = new IBBoard.Windows.Forms.IBBLabel();
			this.statsPanel = new System.Windows.Forms.FlowLayoutPanel();
			((System.ComponentModel.ISupportInitialize)(this.unitSize)).BeginInit();
			this.SuspendLayout();
			// 
			// tbUnitName
			// 
			this.tbUnitName.Location = new System.Drawing.Point(8, 8);
			this.tbUnitName.Name = "tbUnitName";
			this.tbUnitName.Size = new System.Drawing.Size(344, 20);
			this.tbUnitName.TabIndex = 1;
			this.tbUnitName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbUnitName_KeyDown);
			this.tbUnitName.Leave += new System.EventHandler(this.tbUnitName_Leave);
			// 
			// unitSize
			// 
			this.unitSize.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.unitSize.Location = new System.Drawing.Point(528, 8);
			this.unitSize.Name = "unitSize";
			this.unitSize.Size = new System.Drawing.Size(80, 20);
			this.unitSize.TabIndex = 1;
			this.unitSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
			this.unitSize.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
			this.unitSize.Leave += new System.EventHandler(this.unitSize_Leave);
			this.unitSize.KeyDown += new System.Windows.Forms.KeyEventHandler(this.unitSize_KeyDown);
			// 
			// lblUnitSize
			// 
			this.lblUnitSize.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.lblUnitSize.Location = new System.Drawing.Point(426, 8);
			this.lblUnitSize.Name = "lblUnitSize";
			this.lblUnitSize.Size = new System.Drawing.Size(98, 23);
			this.lblUnitSize.TabIndex = 0;
			this.lblUnitSize.Text = "unit size";
			this.lblUnitSize.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// lblEquip
			// 
			this.lblEquip.Location = new System.Drawing.Point(15, 126);
			this.lblEquip.Name = "lblEquip";
			this.lblEquip.Size = new System.Drawing.Size(81, 108);
			this.lblEquip.TabIndex = 3;
			this.lblEquip.Text = "equipment";
			this.lblEquip.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// bttnAddWeapon
			// 
			this.bttnAddWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.bttnAddWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnAddWeapon.Location = new System.Drawing.Point(516, 126);
			this.bttnAddWeapon.Name = "bttnAddWeapon";
			this.bttnAddWeapon.Size = new System.Drawing.Size(88, 22);
			this.bttnAddWeapon.TabIndex = 4;
			this.bttnAddWeapon.Text = "add";
			this.bttnAddWeapon.Click += new System.EventHandler(this.bttnAddWeapon_Click);
			// 
			// bttnRemoveWeapon
			// 
			this.bttnRemoveWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.bttnRemoveWeapon.Enabled = false;
			this.bttnRemoveWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnRemoveWeapon.Location = new System.Drawing.Point(516, 210);
			this.bttnRemoveWeapon.Name = "bttnRemoveWeapon";
			this.bttnRemoveWeapon.Size = new System.Drawing.Size(88, 22);
			this.bttnRemoveWeapon.TabIndex = 5;
			this.bttnRemoveWeapon.Text = "remove";
			this.bttnRemoveWeapon.Click += new System.EventHandler(this.bttnRemoveWeapon_Click);
			// 
			// equipmentList
			// 
			this.equipmentList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.equipmentList.Location = new System.Drawing.Point(102, 126);
			this.equipmentList.Name = "equipmentList";
			this.equipmentList.Size = new System.Drawing.Size(408, 108);
			this.equipmentList.TabIndex = 6;
			this.equipmentList.SelectedIndexChanged += new System.EventHandler(this.equipmentList_SelectedIndexChanged);
			this.equipmentList.DoubleClick += new System.EventHandler(this.equipmentList_DoubleClick);
			// 
			// bttnReplaceWeapon
			// 
			this.bttnReplaceWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.bttnReplaceWeapon.Enabled = false;
			this.bttnReplaceWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnReplaceWeapon.Location = new System.Drawing.Point(516, 182);
			this.bttnReplaceWeapon.Name = "bttnReplaceWeapon";
			this.bttnReplaceWeapon.Size = new System.Drawing.Size(88, 22);
			this.bttnReplaceWeapon.TabIndex = 10;
			this.bttnReplaceWeapon.Text = "replace";
			this.bttnReplaceWeapon.Click += new System.EventHandler(this.bttnReplaceWeapon_Click);
			// 
			// bttnEditWeapon
			// 
			this.bttnEditWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
			this.bttnEditWeapon.Enabled = false;
			this.bttnEditWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.bttnEditWeapon.Location = new System.Drawing.Point(516, 154);
			this.bttnEditWeapon.Name = "bttnEditWeapon";
			this.bttnEditWeapon.Size = new System.Drawing.Size(88, 22);
			this.bttnEditWeapon.TabIndex = 11;
			this.bttnEditWeapon.Text = "edit";
			this.bttnEditWeapon.Click += new System.EventHandler(this.bttnEditWeapon_Click);
			// 
			// lblPoints
			// 
			this.lblPoints.Location = new System.Drawing.Point(358, 8);
			this.lblPoints.Name = "lblPoints";
			this.lblPoints.Size = new System.Drawing.Size(77, 21);
			this.lblPoints.TabIndex = 12;
			this.lblPoints.Text = "(points)";
			// 
			// lblNotes
			// 
			this.lblNotes.Location = new System.Drawing.Point(13, 317);
			this.lblNotes.Name = "lblNotes";
			this.lblNotes.Size = new System.Drawing.Size(84, 62);
			this.lblNotes.TabIndex = 13;
			this.lblNotes.Text = "notes";
			this.lblNotes.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// notes
			// 
			this.notes.Location = new System.Drawing.Point(102, 317);
			this.notes.Multiline = true;
			this.notes.Name = "notes";
			this.notes.ReadOnly = true;
			this.notes.Size = new System.Drawing.Size(408, 62);
			this.notes.TabIndex = 14;
			// 
			// abilitiesList
			// 
			this.abilitiesList.FormattingEnabled = true;
			this.abilitiesList.Location = new System.Drawing.Point(102, 240);
			this.abilitiesList.Name = "abilitiesList";
			this.abilitiesList.Size = new System.Drawing.Size(408, 69);
			this.abilitiesList.TabIndex = 15;
			// 
			// lblAbilities
			// 
			this.lblAbilities.Location = new System.Drawing.Point(13, 240);
			this.lblAbilities.Name = "lblAbilities";
			this.lblAbilities.Size = new System.Drawing.Size(84, 62);
			this.lblAbilities.TabIndex = 16;
			this.lblAbilities.Text = "abilities";
			this.lblAbilities.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// statsPanel
			// 
			this.statsPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.statsPanel.AutoScroll = true;
			this.statsPanel.Location = new System.Drawing.Point(8, 35);
			this.statsPanel.Name = "statsPanel";
			this.statsPanel.Size = new System.Drawing.Size(600, 85);
			this.statsPanel.TabIndex = 17;
			// 
			// FrmUnit
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(616, 391);
			this.Controls.Add(this.statsPanel);
			this.Controls.Add(this.lblAbilities);
			this.Controls.Add(this.abilitiesList);
			this.Controls.Add(this.notes);
			this.Controls.Add(this.lblNotes);
			this.Controls.Add(this.lblPoints);
			this.Controls.Add(this.bttnEditWeapon);
			this.Controls.Add(this.bttnReplaceWeapon);
			this.Controls.Add(this.equipmentList);
			this.Controls.Add(this.bttnRemoveWeapon);
			this.Controls.Add(this.bttnAddWeapon);
			this.Controls.Add(this.lblEquip);
			this.Controls.Add(this.lblUnitSize);
			this.Controls.Add(this.unitSize);
			this.Controls.Add(this.tbUnitName);
			this.Name = "FrmUnit";
			this.ShowIcon = false;
			this.ShowInTaskbar = false;
			this.Text = "FrmUnit";
			this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmUnit_FormClosing);
			((System.ComponentModel.ISupportInitialize)(this.unitSize)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		public Unit Unit
		{
			get { return unit; }
		}

		private void tbUnitName_Leave(object sender, System.EventArgs e)
		{
			UpdateUnitName();
		}

		private void tbUnitName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				UpdateUnitName();
			}
		}

		private void UpdateUnitName()
		{
			if (unit.Name != tbUnitName.Text)
			{
				commandStack.Execute(new SetNameCommand(unit, tbUnitName.Text));
			}
		}

		private void unitSize_Leave(object sender, System.EventArgs e)
		{
			UpdateUnitSize();
		}

		private void unitSize_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				UpdateUnitSize();
			}
		}

		private void UpdateUnitSize()
		{
			if (unit.Size != unitSize.Value)
			{
				commandStack.Execute(new SetUnitSizeCommand(unit, (int) unitSize.Value));
			}
		}

		private void unit_NameChanged(WarFoundryObject obj, string oldValue, string newValue)
		{
			if (obj is Unit && obj.Equals(unit))
			{
				SetUnitName();
			}
		}

		private void unit_UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue)
		{
			if (obj is Unit && obj.Equals(unit))
			{
				unitSize.Value = newValue;
			}
		}

		private void RefreshUnitEquipment()
		{
			foreach (UnitEquipmentChoice choice in equipmentChoices.Values)
			{
				SetEquipmentListValue(choice);
			}
		}

		private void equipmentList_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SetButtonsEnabledState();
		}

		private void SetButtonsEnabledState()
		{

			UnitEquipmentItem equipItem = GetSelectedUnitEquipmentItem();
			bttnReplaceWeapon.Enabled = (equipItem != null && equipItem.HasAlternatives());
			bttnEditWeapon.Enabled = (equipItem != null);
			bttnRemoveWeapon.Enabled = (equipItem != null && !equipItem.IsRequired);
		}

		private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue)
		{
			if (obj is UnitEquipmentItem)
			{
				UnitEquipmentItem equip = (UnitEquipmentItem) obj;
				UnitEquipmentChoice equipChoice = GetEquipmentChoice(equip);

				if (newValue == 0)
				{
					equipmentList.Items.Remove(equipChoice);
				}
				else
				{
					SetEquipmentListValue(equipChoice);
				}
			}
		}

		private void SetEquipmentListValue(UnitEquipmentChoice equipChoice)
		{
			int idx = equipmentList.Items.IndexOf(equipChoice);

			if (idx > -1)
			{
				equipmentList.Items[idx] = equipChoice;
			}
			else
			{
				equipmentList.Items.Add(equipChoice);
			}
		}

		private void EditWeapon()
		{
			UnitEquipmentItem item = GetSelectedUnitEquipmentItem();

			if (item != null)
			{
				FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, item, commandStack);
				editEquip.ShowDialog(this);
			}
		}

		private UnitEquipmentItem GetSelectedUnitEquipmentItem()
		{
			UnitEquipmentChoice selectedItem = GetSelectedUnitEquipmentChoice();
			UnitEquipmentItem equipItem = null;

			if (selectedItem!=null)
			{
				equipItem = selectedItem.Item;
			}

			return equipItem;
		}

		private UnitEquipmentChoice GetSelectedUnitEquipmentChoice()
		{
			return (UnitEquipmentChoice) equipmentList.SelectedItem;
		}

		private void bttnEditWeapon_Click(object sender, System.EventArgs e)
		{
			EditWeapon();
		}

		private void equipmentList_DoubleClick(object sender, System.EventArgs e)
		{
			EditWeapon();
		}

		private void AddWeapon()
		{
			FrmNewUnitEquipment newEquip = new FrmNewUnitEquipment(Unit, commandStack);
			newEquip.ShowDialog(this);
		}

		private void bttnAddWeapon_Click(object sender, System.EventArgs e)
		{
			AddWeapon();
		}

		private void RemoveWeapon()
		{
			commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, GetSelectedUnitEquipmentItem(), 0));
		}

		private void bttnRemoveWeapon_Click(object sender, System.EventArgs e)
		{
			RemoveWeapon();
		}

		private void bttnReplaceWeapon_Click(object sender, System.EventArgs e)
		{
			FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, GetSelectedUnitEquipmentItem(), commandStack);
			replace.ShowDialog(this);
		}

		private void FrmUnit_FormClosing(object sender, FormClosingEventArgs e)
		{
			UpdateUnitName();
			UpdateUnitSize();
		}
	}
}