// mainform.cs: main program  
// --------------------------------------------------------
// July 10,2004 
// By Xiaotao Shu.

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

namespace tools
{
	/// <summary>
	/// Summary description for MapListForm.
	/// </summary>
	public class MapListForm : System.Windows.Forms.Form
	{
		public string WorkDir;            //set the wotking directory for mapsource.xml 
		public string Title;              //the map name of the chosen map
		public string FileName;           //the image file name of the chosen map
		public double MapTop;             //the map range
		public double MapLeft;            //the map range
		public double MapRight;           //the map range
		public double MapBottom;          //the map range

		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label1;
		private System.Data.DataColumn dcTitle;
		private System.Data.DataColumn dcImageFile;
		private System.Data.DataColumn dcTop;
		private System.Data.DataColumn dcLeft;
		private System.Data.DataColumn dcRight;
		private System.Data.DataColumn dcBottom;
		private System.Data.DataSet dataSetMap;
		private System.Data.DataTable dtMapSource;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Panel panel3;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn3;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn5;
		private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn6;
		private System.Windows.Forms.Button buttonOk;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MapListForm()
		{
			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.panel1 = new System.Windows.Forms.Panel();
			this.button2 = new System.Windows.Forms.Button();
			this.buttonOk = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.dataSetMap = new System.Data.DataSet();
			this.dtMapSource = new System.Data.DataTable();
			this.dcTitle = new System.Data.DataColumn();
			this.dcImageFile = new System.Data.DataColumn();
			this.dcTop = new System.Data.DataColumn();
			this.dcLeft = new System.Data.DataColumn();
			this.dcRight = new System.Data.DataColumn();
			this.dcBottom = new System.Data.DataColumn();
			this.panel3 = new System.Windows.Forms.Panel();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
			this.dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn2 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn3 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn4 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn5 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.dataGridTextBoxColumn6 = new System.Windows.Forms.DataGridTextBoxColumn();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataSetMap)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dtMapSource)).BeginInit();
			this.panel3.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.buttonOk);
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 0);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(712, 72);
			this.panel1.TabIndex = 0;
			// 
			// button2
			// 
			this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.button2.Location = new System.Drawing.Point(608, 40);
			this.button2.Name = "button2";
			this.button2.TabIndex = 6;
			this.button2.Text = "Cancel";
			// 
			// buttonOk
			// 
			this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.buttonOk.Location = new System.Drawing.Point(608, 8);
			this.buttonOk.Name = "buttonOk";
			this.buttonOk.TabIndex = 5;
			this.buttonOk.Text = "Ok";
			this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(128, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(424, 20);
			this.textBox1.TabIndex = 4;
			this.textBox1.Text = "";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(104, 23);
			this.label2.TabIndex = 1;
			this.label2.Text = "Your chosen map: ";
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(176, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "total maps = 0";
			// 
			// dataSetMap
			// 
			this.dataSetMap.DataSetName = "dsMap";
			this.dataSetMap.Locale = new System.Globalization.CultureInfo("en-US");
			this.dataSetMap.Tables.AddRange(new System.Data.DataTable[] {
																			this.dtMapSource});
			// 
			// dtMapSource
			// 
			this.dtMapSource.Columns.AddRange(new System.Data.DataColumn[] {
																			   this.dcTitle,
																			   this.dcImageFile,
																			   this.dcTop,
																			   this.dcLeft,
																			   this.dcRight,
																			   this.dcBottom});
			this.dtMapSource.TableName = "dtMapSource";
			// 
			// dcTitle
			// 
			this.dcTitle.AllowDBNull = false;
			this.dcTitle.Caption = "Title";
			this.dcTitle.ColumnName = "Title";
			// 
			// dcImageFile
			// 
			this.dcImageFile.Caption = "File Name";
			this.dcImageFile.ColumnName = "FileName";
			// 
			// dcTop
			// 
			this.dcTop.Caption = "Top Latitude";
			this.dcTop.ColumnName = "TopLatitude";
			this.dcTop.DataType = typeof(System.Double);
			// 
			// dcLeft
			// 
			this.dcLeft.Caption = "Left Longitude";
			this.dcLeft.ColumnName = "LeftLongitude";
			this.dcLeft.DataType = typeof(System.Double);
			// 
			// dcRight
			// 
			this.dcRight.Caption = "Right Longitude";
			this.dcRight.ColumnName = "RightLongitude";
			this.dcRight.DataType = typeof(System.Double);
			// 
			// dcBottom
			// 
			this.dcBottom.Caption = "Bottom Latitude";
			this.dcBottom.ColumnName = "BottomLatitude";
			this.dcBottom.DataType = typeof(System.Double);
			// 
			// panel3
			// 
			this.panel3.Controls.Add(this.dataGrid1);
			this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel3.Location = new System.Drawing.Point(0, 72);
			this.panel3.Name = "panel3";
			this.panel3.Size = new System.Drawing.Size(712, 357);
			this.panel3.TabIndex = 3;
			// 
			// dataGrid1
			// 
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.dataGrid1.DataMember = "dtMapSource";
			this.dataGrid1.DataSource = this.dataSetMap;
			this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.Size = new System.Drawing.Size(712, 357);
			this.dataGrid1.TabIndex = 2;
			this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
																								  this.dataGridTableStyle1});
			// 
			// dataGridTableStyle1
			// 
			this.dataGridTableStyle1.DataGrid = this.dataGrid1;
			this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
																												  this.dataGridTextBoxColumn1,
																												  this.dataGridTextBoxColumn2,
																												  this.dataGridTextBoxColumn3,
																												  this.dataGridTextBoxColumn4,
																												  this.dataGridTextBoxColumn5,
																												  this.dataGridTextBoxColumn6});
			this.dataGridTableStyle1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridTableStyle1.MappingName = "dtMapSource";
			// 
			// dataGridTextBoxColumn1
			// 
			this.dataGridTextBoxColumn1.Format = "";
			this.dataGridTextBoxColumn1.FormatInfo = null;
			this.dataGridTextBoxColumn1.HeaderText = "Title";
			this.dataGridTextBoxColumn1.MappingName = "Title";
			this.dataGridTextBoxColumn1.Width = 150;
			// 
			// dataGridTextBoxColumn2
			// 
			this.dataGridTextBoxColumn2.Format = "";
			this.dataGridTextBoxColumn2.FormatInfo = null;
			this.dataGridTextBoxColumn2.HeaderText = "FileName";
			this.dataGridTextBoxColumn2.MappingName = "FileName";
			this.dataGridTextBoxColumn2.Width = 200;
			// 
			// dataGridTextBoxColumn3
			// 
			this.dataGridTextBoxColumn3.Format = "";
			this.dataGridTextBoxColumn3.FormatInfo = null;
			this.dataGridTextBoxColumn3.HeaderText = "Top Latitude";
			this.dataGridTextBoxColumn3.MappingName = "TopLatitude";
			this.dataGridTextBoxColumn3.Width = 75;
			// 
			// dataGridTextBoxColumn4
			// 
			this.dataGridTextBoxColumn4.Format = "";
			this.dataGridTextBoxColumn4.FormatInfo = null;
			this.dataGridTextBoxColumn4.HeaderText = "Left Longitude";
			this.dataGridTextBoxColumn4.MappingName = "LeftLongitude";
			this.dataGridTextBoxColumn4.Width = 75;
			// 
			// dataGridTextBoxColumn5
			// 
			this.dataGridTextBoxColumn5.Format = "";
			this.dataGridTextBoxColumn5.FormatInfo = null;
			this.dataGridTextBoxColumn5.HeaderText = "Right Longitude";
			this.dataGridTextBoxColumn5.MappingName = "RightLongitude";
			this.dataGridTextBoxColumn5.Width = 75;
			// 
			// dataGridTextBoxColumn6
			// 
			this.dataGridTextBoxColumn6.Format = "";
			this.dataGridTextBoxColumn6.FormatInfo = null;
			this.dataGridTextBoxColumn6.HeaderText = "Bottom Latitude";
			this.dataGridTextBoxColumn6.MappingName = "BottomLatitude";
			this.dataGridTextBoxColumn6.Width = 75;
			// 
			// MapListForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(712, 429);
			this.Controls.Add(this.panel3);
			this.Controls.Add(this.panel1);
			this.Name = "MapListForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "MapListForm";
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataSetMap)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dtMapSource)).EndInit();
			this.panel3.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void buttonOk_Click(object sender, System.EventArgs e)
		{
			try
			{
				int index = dataGrid1.CurrentCell.RowNumber;
				Title = (string)dataGrid1[index,0];//(string)dtMapSource.Rows[index]["Title"];
				FileName = (string)dataGrid1[index,1];//(string)dtMapSource.Rows[index]["FileName"];
				if (dataGrid1[index,2]!=DBNull.Value)
					MapTop = (double)dataGrid1[index,2];
				if (dataGrid1[index,3]!=DBNull.Value)
					MapLeft = (double)dataGrid1[index,3];
				if (dataGrid1[index,4]!=DBNull.Value)
					MapRight = (double)dataGrid1[index,4];
				if (dataGrid1[index,5]!=DBNull.Value)
					MapBottom = (double)dataGrid1[index,5];
			}
			catch (Exception e1)
			{
				MessageBox.Show(e1.ToString());
				buttonOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			}
		}
	
		protected override void OnLoad(EventArgs e)
		{
			//read map source information to the dataset 
			base.OnLoad (e);
			try 
			{
				//load the map information from the xml file
				this.dataSetMap.ReadXml(WorkDir+"\\mapsource.xml", XmlReadMode.IgnoreSchema);
			
				label1.Text = "total maps = "+ Convert.ToString(dtMapSource.Rows.Count);
				// dynamically binding Title to textbox 
				textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSetMap, "dtMapSource.Title"));
			}
			catch (Exception e1)
			{
				MessageBox.Show("No map source"+e1);
			}
		}

	}
}
