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

namespace DiagramUI
{
	/// <summary>
	/// Summary description for FormOrder.
	/// </summary>
	public class FormOrder : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label labelObject;
		private PanelDesktop desktop;
		private System.Windows.Forms.ListBox listBoxComponents;
		private System.Windows.Forms.Button buttonOK;
		private ObjectLabel label;

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public FormOrder(PanelDesktop desktop)
		{
			InitializeComponent();
			ResourceService.Resources.LoadControlResources(this);
			this.desktop = desktop;
			label = desktop.Selected[0][0] as ObjectLabel;
			labelObject.Text = label.ComponentName;
			foreach (Control c in desktop.Controls)
			{
				if (!(c is NamedComponent))
				{
					continue;
				}
				NamedComponent nc = c as NamedComponent;
				listBoxComponents.Items.Add(nc.ComponentName);
			}
		}


		/// <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.labelObject = new System.Windows.Forms.Label();
			this.listBoxComponents = new System.Windows.Forms.ListBox();
			this.buttonOK = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// labelObject
			// 
			this.labelObject.Location = new System.Drawing.Point(40, 40);
			this.labelObject.Name = "labelObject";
			this.labelObject.TabIndex = 0;
			this.labelObject.Text = "label1";
			// 
			// listBoxComponents
			// 
			this.listBoxComponents.Location = new System.Drawing.Point(72, 96);
			this.listBoxComponents.Name = "listBoxComponents";
			this.listBoxComponents.Size = new System.Drawing.Size(304, 277);
			this.listBoxComponents.TabIndex = 1;
			// 
			// buttonOK
			// 
			this.buttonOK.Location = new System.Drawing.Point(88, 392);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 2;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// FormOrder
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(536, 445);
			this.Controls.Add(this.buttonOK);
			this.Controls.Add(this.listBoxComponents);
			this.Controls.Add(this.labelObject);
			this.Name = "FormOrder";
			this.Text = "Change control order";
			this.ResumeLayout(false);

		}
		#endregion

		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			int ord = label.Ord;
			try
			{
				int n = listBoxComponents.SelectedIndex;
				label.Ord = n;
				desktop.CheckOrder();
				Dispose();
			}
			catch (Exception ex)
			{
				label.Ord = ord;
				DefaultForm.ShowError(this, ex);
			}
		}
	}
}
