// (c) 2005 Electronic Arts Inc.
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;

using EA.GameTalkMessages;

namespace EA.GameTalkViewer
{
	/// <summary>
	/// Summary description for TargetManagerDialog.
	/// </summary>
	public class TargetManagerDialog : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button _ButtonDone;
		private System.Windows.Forms.ListView _ListViewTargets;
		private System.Windows.Forms.ColumnHeader _ColumnHeaderTarget;
		private System.Windows.Forms.ColumnHeader _ColumnHeaderDnsName;
		private System.Windows.Forms.ColumnHeader _ColumnHeaderIpAddress;
		private System.Windows.Forms.Button _ButtonAdd;
		private System.Windows.Forms.Button _ButtonRemove;
		private System.Windows.Forms.TextBox _TextBoxDnsName;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.ComboBox _ComboBoxTargetName;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox _TextBoxPort;
		private System.Windows.Forms.ColumnHeader _ColumnHeaderPort;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox _TextBoxIpAddress;
		private ComboBox _ComboBoxPlatform;
		private Label label5;
		private ColumnHeader _ColumnHeaderPlatform;
		private Button _ButtonUpdate;
		private GroupBox groupBox2;
		private ColumnHeader _ColumnHeaderAutoResolve;
		private CheckBox _CheckBoxAutoResolve;
		private Button mButtonEdit;
		private Button mButtonUpdate;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public TargetManagerDialog()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			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._ButtonDone = new System.Windows.Forms.Button();
			this._ListViewTargets = new System.Windows.Forms.ListView();
			this._ColumnHeaderTarget = new System.Windows.Forms.ColumnHeader();
			this._ColumnHeaderPlatform = new System.Windows.Forms.ColumnHeader();
			this._ColumnHeaderDnsName = new System.Windows.Forms.ColumnHeader();
			this._ColumnHeaderIpAddress = new System.Windows.Forms.ColumnHeader();
			this._ColumnHeaderPort = new System.Windows.Forms.ColumnHeader();
			this._ColumnHeaderAutoResolve = new System.Windows.Forms.ColumnHeader();
			this._ButtonAdd = new System.Windows.Forms.Button();
			this._ButtonRemove = new System.Windows.Forms.Button();
			this._TextBoxDnsName = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this._ComboBoxTargetName = new System.Windows.Forms.ComboBox();
			this.label3 = new System.Windows.Forms.Label();
			this._TextBoxPort = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this._TextBoxIpAddress = new System.Windows.Forms.TextBox();
			this._ComboBoxPlatform = new System.Windows.Forms.ComboBox();
			this.label5 = new System.Windows.Forms.Label();
			this._ButtonUpdate = new System.Windows.Forms.Button();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this._CheckBoxAutoResolve = new System.Windows.Forms.CheckBox();
			this.mButtonEdit = new System.Windows.Forms.Button();
			this.mButtonUpdate = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// _ButtonDone
			// 
			this._ButtonDone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this._ButtonDone.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonDone.Location = new System.Drawing.Point(675, 338);
			this._ButtonDone.Name = "_ButtonDone";
			this._ButtonDone.Size = new System.Drawing.Size(75, 23);
			this._ButtonDone.TabIndex = 8;
			this._ButtonDone.Text = "Done";
			this._ButtonDone.Click += new System.EventHandler(this.ButtonDone_Click);
			// 
			// _ListViewTargets
			// 
			this._ListViewTargets.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this._ListViewTargets.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this._ColumnHeaderTarget,
            this._ColumnHeaderPlatform,
            this._ColumnHeaderDnsName,
            this._ColumnHeaderIpAddress,
            this._ColumnHeaderPort,
            this._ColumnHeaderAutoResolve});
			this._ListViewTargets.FullRowSelect = true;
			this._ListViewTargets.GridLines = true;
			this._ListViewTargets.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this._ListViewTargets.HideSelection = false;
			this._ListViewTargets.Location = new System.Drawing.Point(4, 166);
			this._ListViewTargets.Name = "_ListViewTargets";
			this._ListViewTargets.Size = new System.Drawing.Size(665, 152);
			this._ListViewTargets.TabIndex = 6;
			this._ListViewTargets.UseCompatibleStateImageBehavior = false;
			this._ListViewTargets.View = System.Windows.Forms.View.Details;
			this._ListViewTargets.SelectedIndexChanged += new System.EventHandler(this.ListViewTargets_SelectedIndexChanged);
			// 
			// _ColumnHeaderTarget
			// 
			this._ColumnHeaderTarget.Text = "Target";
			this._ColumnHeaderTarget.Width = 93;
			// 
			// _ColumnHeaderPlatform
			// 
			this._ColumnHeaderPlatform.Text = "Platform";
			this._ColumnHeaderPlatform.Width = 110;
			// 
			// _ColumnHeaderDnsName
			// 
			this._ColumnHeaderDnsName.Text = "DNS Name";
			this._ColumnHeaderDnsName.Width = 120;
			// 
			// _ColumnHeaderIpAddress
			// 
			this._ColumnHeaderIpAddress.Text = "Ip Address";
			this._ColumnHeaderIpAddress.Width = 120;
			// 
			// _ColumnHeaderPort
			// 
			this._ColumnHeaderPort.Text = "Port";
			// 
			// _ColumnHeaderAutoResolve
			// 
			this._ColumnHeaderAutoResolve.Text = "Auto-Resolve IP-Address";
			this._ColumnHeaderAutoResolve.Width = 135;
			// 
			// _ButtonAdd
			// 
			this._ButtonAdd.Enabled = false;
			this._ButtonAdd.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonAdd.Location = new System.Drawing.Point(484, 129);
			this._ButtonAdd.Name = "_ButtonAdd";
			this._ButtonAdd.Size = new System.Drawing.Size(75, 23);
			this._ButtonAdd.TabIndex = 5;
			this._ButtonAdd.Text = "Add";
			this._ButtonAdd.Click += new System.EventHandler(this.ButtonAdd_Click);
			// 
			// _ButtonRemove
			// 
			this._ButtonRemove.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this._ButtonRemove.Enabled = false;
			this._ButtonRemove.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this._ButtonRemove.Location = new System.Drawing.Point(675, 295);
			this._ButtonRemove.Name = "_ButtonRemove";
			this._ButtonRemove.Size = new System.Drawing.Size(75, 23);
			this._ButtonRemove.TabIndex = 7;
			this._ButtonRemove.Text = "Remove";
			this._ButtonRemove.Click += new System.EventHandler(this.ButtonRemove_Click);
			// 
			// _TextBoxDnsName
			// 
			this._TextBoxDnsName.Location = new System.Drawing.Point(268, 55);
			this._TextBoxDnsName.Name = "_TextBoxDnsName";
			this._TextBoxDnsName.Size = new System.Drawing.Size(144, 20);
			this._TextBoxDnsName.TabIndex = 2;
			this._TextBoxDnsName.KeyUp += new System.Windows.Forms.KeyEventHandler(this.TextBoxDnsName_KeyUp);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 55);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(244, 23);
			this.label1.TabIndex = 8;
			this.label1.Text = "DNS name or IP Address of the console:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 4);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(244, 23);
			this.label2.TabIndex = 7;
			this.label2.Text = "Target name:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox1.Location = new System.Drawing.Point(4, 152);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(746, 8);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			// 
			// _ComboBoxTargetName
			// 
			this._ComboBoxTargetName.Location = new System.Drawing.Point(268, 4);
			this._ComboBoxTargetName.Name = "_ComboBoxTargetName";
			this._ComboBoxTargetName.Size = new System.Drawing.Size(144, 21);
			this._ComboBoxTargetName.TabIndex = 0;
			this._ComboBoxTargetName.SelectedIndexChanged += new System.EventHandler(this.ComboBoxTargetName_SelectedIndexChanged);
			this._ComboBoxTargetName.KeyUp += new System.Windows.Forms.KeyEventHandler(this._ComboBoxTargetName_KeyUp);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 99);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(244, 23);
			this.label3.TabIndex = 9;
			this.label3.Text = "Port:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// _TextBoxPort
			// 
			this._TextBoxPort.Location = new System.Drawing.Point(268, 103);
			this._TextBoxPort.Name = "_TextBoxPort";
			this._TextBoxPort.Size = new System.Drawing.Size(144, 20);
			this._TextBoxPort.TabIndex = 4;
			this._TextBoxPort.TextChanged += new System.EventHandler(this._TextBoxPort_TextChanged);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(24, 75);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(244, 23);
			this.label4.TabIndex = 12;
			this.label4.Text = "IP Address of the console:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// _TextBoxIpAddress
			// 
			this._TextBoxIpAddress.Location = new System.Drawing.Point(268, 79);
			this._TextBoxIpAddress.Name = "_TextBoxIpAddress";
			this._TextBoxIpAddress.Size = new System.Drawing.Size(144, 20);
			this._TextBoxIpAddress.TabIndex = 3;
			this._TextBoxIpAddress.TextChanged += new System.EventHandler(this._TextBoxIpAddress_TextChanged);
			// 
			// _ComboBoxPlatform
			// 
			this._ComboBoxPlatform.FormattingEnabled = true;
			this._ComboBoxPlatform.Items.AddRange(new object[] {
            "xenon",
            "xbox",
            "ps2",
            "ps3",
            "gamecube",
            "revolution",
            "pc"});
			this._ComboBoxPlatform.Location = new System.Drawing.Point(269, 29);
			this._ComboBoxPlatform.Name = "_ComboBoxPlatform";
			this._ComboBoxPlatform.Size = new System.Drawing.Size(143, 21);
			this._ComboBoxPlatform.TabIndex = 1;
			// 
			// label5
			// 
			this.label5.AutoSize = true;
			this.label5.Location = new System.Drawing.Point(219, 32);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(48, 13);
			this.label5.TabIndex = 14;
			this.label5.Text = "Platform:";
			// 
			// _ButtonUpdate
			// 
			this._ButtonUpdate.Location = new System.Drawing.Point(418, 79);
			this._ButtonUpdate.Name = "_ButtonUpdate";
			this._ButtonUpdate.Size = new System.Drawing.Size(117, 23);
			this._ButtonUpdate.TabIndex = 15;
			this._ButtonUpdate.Text = "Lookup IP Address";
			this._ButtonUpdate.UseVisualStyleBackColor = true;
			this._ButtonUpdate.Click += new System.EventHandler(this._ButtonUpdate_Click);
			// 
			// groupBox2
			// 
			this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox2.Location = new System.Drawing.Point(4, 324);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(746, 8);
			this.groupBox2.TabIndex = 11;
			this.groupBox2.TabStop = false;
			// 
			// _CheckBoxAutoResolve
			// 
			this._CheckBoxAutoResolve.AutoSize = true;
			this._CheckBoxAutoResolve.Checked = true;
			this._CheckBoxAutoResolve.CheckState = System.Windows.Forms.CheckState.Checked;
			this._CheckBoxAutoResolve.Location = new System.Drawing.Point(269, 129);
			this._CheckBoxAutoResolve.Name = "_CheckBoxAutoResolve";
			this._CheckBoxAutoResolve.Size = new System.Drawing.Size(162, 17);
			this._CheckBoxAutoResolve.TabIndex = 16;
			this._CheckBoxAutoResolve.Text = "Auto-Resolve (of IP-Address)";
			this._CheckBoxAutoResolve.UseVisualStyleBackColor = true;
			// 
			// mButtonEdit
			// 
			this.mButtonEdit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.mButtonEdit.Location = new System.Drawing.Point(675, 266);
			this.mButtonEdit.Name = "mButtonEdit";
			this.mButtonEdit.Size = new System.Drawing.Size(75, 23);
			this.mButtonEdit.TabIndex = 17;
			this.mButtonEdit.Text = "Edit";
			this.mButtonEdit.UseVisualStyleBackColor = true;
			this.mButtonEdit.Click += new System.EventHandler(this.mButtonEdit_Click);
			// 
			// mButtonUpdate
			// 
			this.mButtonUpdate.Enabled = false;
			this.mButtonUpdate.Location = new System.Drawing.Point(565, 129);
			this.mButtonUpdate.Name = "mButtonUpdate";
			this.mButtonUpdate.Size = new System.Drawing.Size(75, 23);
			this.mButtonUpdate.TabIndex = 18;
			this.mButtonUpdate.Text = "Update";
			this.mButtonUpdate.UseVisualStyleBackColor = true;
			this.mButtonUpdate.Click += new System.EventHandler(this.mButtonUpdate_Click);
			// 
			// TargetManagerDialog
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
			this.ClientSize = new System.Drawing.Size(756, 380);
			this.ControlBox = false;
			this.Controls.Add(this.mButtonUpdate);
			this.Controls.Add(this.mButtonEdit);
			this.Controls.Add(this._CheckBoxAutoResolve);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this._ButtonUpdate);
			this.Controls.Add(this.label5);
			this.Controls.Add(this._ComboBoxPlatform);
			this.Controls.Add(this._TextBoxIpAddress);
			this.Controls.Add(this.label4);
			this.Controls.Add(this._TextBoxPort);
			this.Controls.Add(this._TextBoxDnsName);
			this.Controls.Add(this.label3);
			this.Controls.Add(this._ComboBoxTargetName);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this._ButtonRemove);
			this.Controls.Add(this._ButtonAdd);
			this.Controls.Add(this._ListViewTargets);
			this.Controls.Add(this._ButtonDone);
			this.Controls.Add(this.groupBox1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.MinimumSize = new System.Drawing.Size(608, 363);
			this.Name = "TargetManagerDialog";
			this.ShowInTaskbar = false;
			this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
			this.Text = "Target Manager";
			this.Load += new System.EventHandler(this.TargetManagerDialog_Load);
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		private void TargetManagerDialog_Load(object sender, System.EventArgs e)
		{
			TargetManager.Instance.Initialize();
			// We only need to update the _ComboBoxTargetName once - since we don't remove or add from that list
			_ComboBoxTargetName.Items.Clear();
			foreach (string targetName in TargetManager.Instance.TargetNames)
			{
				_ComboBoxTargetName.Items.Add(targetName);
			}

			UpdateList();
		}

		private int GetUserSelectedPort()
		{
			int port = TargetManager.DEFAULT_PORT;
			try
			{
				port = Convert.ToInt32(_TextBoxPort.Text);
			}
			catch (Exception) { }
			return port;
		}

		private void ButtonAdd_Click(object sender, System.EventArgs e)
		{
			int port = GetUserSelectedPort();
			TargetManager.Instance.Add(_ComboBoxTargetName.Text, _ComboBoxPlatform.Text, _TextBoxDnsName.Text, _TextBoxIpAddress.Text, port, _CheckBoxAutoResolve.Checked);
			UpdateList();
		}

		private void ButtonRemove_Click(object sender, System.EventArgs e)
		{
			foreach (ListViewItem item in _ListViewTargets.SelectedItems)
			{
				TargetManager.Instance.Remove(item.Text);
			}
			UpdateList();
		}

		private void ButtonDone_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void ListViewTargets_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateButton();
		}

		private void UpdateButton()
		{
			_ButtonRemove.Enabled = _ListViewTargets.SelectedItems.Count > 0;

			bool foundMatch = false;
			if (!string.IsNullOrEmpty(_ComboBoxTargetName.Text))
			{
				string currentlySelectedTarget = _ComboBoxTargetName.Text.ToLower();
				int portNum = GetUserSelectedPort();
				if (TargetManager.Instance.ContainsTarget(currentlySelectedTarget, _TextBoxIpAddress.Text, portNum))
					foundMatch = true;
			}

			if (foundMatch)
			{
				_ButtonAdd.Enabled = false;
				mButtonUpdate.Enabled = true;
			}
			else
			{
				_ButtonAdd.Enabled = true;
				mButtonUpdate.Enabled = false;
			}
		}

		/// <summary>
		/// Because DNS lookups are slow - we actually thread each look-up - so that they are 
		/// done concurrently
		/// </summary>
		/// <param name="listViewItem"></param>
		private static void UpdateListItemThread(ListViewItem item, Target t)
		{
			item.SubItems.Add(t.Platform);
			item.SubItems.Add(t.DnsName);
			if (t.AutoResolveIp)
			{
				String dynamicIpAddress = TargetManager.Instance.GetIpAddress(t.Name, t.DnsName, t.Platform);
				if ((dynamicIpAddress != null) && (t.IpAddress != dynamicIpAddress))
				{
					t.IpAddress = dynamicIpAddress;
					// This allows TargetManagerDialog to send an update to the IP address if it notices that the IP address has changed 
					// (exp - DHCP gives out a different IP) since the last time it was recorded in the registry.
					Configuration.Instance.AddOrUpdateRegisteredTarget(t);
				}
			}
			item.SubItems.Add(t.IpAddress);
			item.SubItems.Add(t.Port.ToString());
			item.SubItems.Add(t.AutoResolveIp.ToString());
		}

		private void UpdateList()
		{
			_ListViewTargets.SuspendLayout();
			_ListViewTargets.Items.Clear();

			List<Thread> threadList = new List<Thread>();
			List<ListViewItem> listViewItems = new List<ListViewItem>();
			foreach (Target t in TargetManager.Instance.Targets)
			{
				ListViewItem item = new ListViewItem(t.Name);
				listViewItems.Add(item);
				// We clone the Target - so that reference semantics don't mess up the other threads during subsequent iterations in the loop 
				Target targetByValue = (Target)t.Clone();

				ThreadStart starter = delegate { UpdateListItemThread(item, targetByValue); };
				Thread updateListItemThread = new Thread(starter);
				updateListItemThread.Name = "UpdateListItem";
				updateListItemThread.Start();
				threadList.Add(updateListItemThread);
			}

			for (int i = 0; i < threadList.Count; i++)
			{
				threadList[i].Join();
				_ListViewTargets.Items.Add(listViewItems[i]);
			}

			_ListViewTargets.ResumeLayout();

			UpdateButton();
		}

		private void TextBoxDnsName_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			UpdateButton();
		}

		private void ComboBoxTargetName_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			_TextBoxPort.Text = TargetManager.DEFAULT_PORT.ToString();
			UpdateButton();
			string dnsName = TargetManager.Instance.GetDnsName(_ComboBoxTargetName.Text);
			_TextBoxDnsName.Text = dnsName;
		}

		private void _ComboBoxTargetName_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			UpdateButton();
		}

		private void UpdateIPAddressBasedOnDNSOrIPAdress()
		{
			string ipAddress = TargetManager.Instance.GetIpAddress(this._ComboBoxTargetName.Text, this._TextBoxDnsName.Text, this._ComboBoxPlatform.Text);
			if (ipAddress != null)
				this._TextBoxIpAddress.Text = ipAddress;
		}

		private void _ButtonUpdate_Click(object sender, EventArgs e)
		{
			UpdateIPAddressBasedOnDNSOrIPAdress();
		}

		private void _TextBoxPort_TextChanged(object sender, EventArgs e)
		{
			UpdateButton();
		}

		private void _TextBoxIpAddress_TextChanged(object sender, EventArgs e)
		{
			UpdateButton();
		}

		private void mButtonEdit_Click(object sender, EventArgs e)
		{
			if (_ListViewTargets.SelectedItems.Count == 0)
				return;

			ListViewItem listViewItem = _ListViewTargets.SelectedItems[0];
			if (listViewItem.SubItems.Count < 6)
				return;

			string target = listViewItem.SubItems[0].Text;
			string platform = listViewItem.SubItems[1].Text;
			string dnsName = listViewItem.SubItems[2].Text;
			string ipAddress = listViewItem.SubItems[3].Text;
			string port = listViewItem.SubItems[4].Text;
			bool autoResolveIpAddress = false;
			try
			{
				autoResolveIpAddress = Convert.ToBoolean(listViewItem.SubItems[5].Text);
			}
			catch { }

			_ComboBoxTargetName.Text = target;
			_ComboBoxPlatform.Text = platform;
			_TextBoxDnsName.Text = dnsName;
			_TextBoxIpAddress.Text = ipAddress;
			_TextBoxPort.Text = port;
			_CheckBoxAutoResolve.Checked = autoResolveIpAddress;
		}

		private void mButtonUpdate_Click(object sender, EventArgs e)
		{
			try
			{
				if (string.IsNullOrEmpty(_ComboBoxTargetName.Text))
					return;

				TargetManager.Instance.Update(_ComboBoxTargetName.Text, _ComboBoxPlatform.Text,
					_TextBoxDnsName.Text, _TextBoxIpAddress.Text, GetUserSelectedPort(), _CheckBoxAutoResolve.Checked);
				UpdateList();
			}
			catch 
			{
				// Dunno what use it would be log this or attempt to recover in some meaningful way... 
			}
		}
	}
}
