using System;

using System.Drawing;

using System.Collections;

using System.Windows.Forms;

using System.Data;

using OpenNETCF.ComponentModel;

 

namespace BayAreaTraffic

{

    /// <summary>

    /// Summary description for form.

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

        private ComboBox comboBoxFrom;

        private MenuItem menuItem1;

        private MenuItem menuItem2;

        private MainMenu mainMenu2;

        private System.Windows.Forms.MainMenu mainMenu1;

        private MenuItem menuItem4;

        private MenuItem menuItem3;

        private BackgroundWorker backgroundWorker1;

        private Label labelAsOfTime;

        private TextBox textBoxFromTo;

        private Label labelDriveTime;

        private Label labelDistance;

        private MenuItem menuItem5;

        Org511 org;

 

        public Form1()

        {

            InitializeComponent();

            InitializeBackgoundWorker();

            InitializeCombos();

        }

 

        private void InitializeCombos()

        {

            org = new Org511();

            System.Collections.IEnumerator myEnumerator = org.routeList.GetEnumerator();

            while (myEnumerator.MoveNext())

            {

                RouteListItem ri = (RouteListItem)myEnumerator.Current;

                comboBoxFrom.Items.Add(ri.name);

            }

            comboBoxFrom.SelectedIndex = 0;

        }

 

        #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.mainMenu1 = new System.Windows.Forms.MainMenu();

            this.menuItem1 = new System.Windows.Forms.MenuItem();

            this.menuItem2 = new System.Windows.Forms.MenuItem();

            this.menuItem5 = new System.Windows.Forms.MenuItem();

            this.menuItem4 = new System.Windows.Forms.MenuItem();

            this.menuItem3 = new System.Windows.Forms.MenuItem();

            this.comboBoxFrom = new System.Windows.Forms.ComboBox();

            this.mainMenu2 = new System.Windows.Forms.MainMenu();

            this.labelAsOfTime = new System.Windows.Forms.Label();

            this.textBoxFromTo = new System.Windows.Forms.TextBox();

            this.labelDriveTime = new System.Windows.Forms.Label();

            this.labelDistance = new System.Windows.Forms.Label();

//

// mainMenu1

//

            this.mainMenu1.MenuItems.Add(this.menuItem1);

            this.mainMenu1.MenuItems.Add(this.menuItem2);

//

// menuItem1

//

            this.menuItem1.Text = "Fetch";

            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);

//

// menuItem2

//

            this.menuItem2.MenuItems.Add(this.menuItem5);

            this.menuItem2.MenuItems.Add(this.menuItem4);

            this.menuItem2.MenuItems.Add(this.menuItem3);

            this.menuItem2.Text = "Menu";

//

// menuItem5

//

            this.menuItem5.Text = "Map";

            this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);

//

// menuItem4

//

            this.menuItem4.Text = "About";

            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);

//

// menuItem3

//

            this.menuItem3.Text = "Exit";

            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);

//

// comboBoxFrom

//

            this.comboBoxFrom.Font = new System.Drawing.Font("Nina", 8F, System.Drawing.FontStyle.Bold);

            this.comboBoxFrom.Location = new System.Drawing.Point(0, 3);

            this.comboBoxFrom.Size = new System.Drawing.Size(176, 19);

//

// labelAsOfTime

//

            this.labelAsOfTime.Font = new System.Drawing.Font("Nina", 10F, System.Drawing.FontStyle.Regular);

            this.labelAsOfTime.ForeColor = System.Drawing.SystemColors.WindowText;

            this.labelAsOfTime.Location = new System.Drawing.Point(7, 153);

            this.labelAsOfTime.Size = new System.Drawing.Size(163, 24);

            this.labelAsOfTime.Text = "as of time";

            this.labelAsOfTime.TextAlign = System.Drawing.ContentAlignment.TopCenter;

//

// textBoxFromTo

//

            this.textBoxFromTo.Font = new System.Drawing.Font("Nina", 8F, System.Drawing.FontStyle.Regular);

            this.textBoxFromTo.Location = new System.Drawing.Point(0, 29);

            this.textBoxFromTo.Multiline = true;

            this.textBoxFromTo.Size = new System.Drawing.Size(176, 62);

            this.textBoxFromTo.Text = "Select route and press \'Fetch\'";

            this.textBoxFromTo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;

//

// labelDriveTime

//

            this.labelDriveTime.Font = new System.Drawing.Font("Nina", 12F, System.Drawing.FontStyle.Bold);

            this.labelDriveTime.ForeColor = System.Drawing.SystemColors.Highlight;

            this.labelDriveTime.Location = new System.Drawing.Point(7, 98);

            this.labelDriveTime.Size = new System.Drawing.Size(163, 24);

            this.labelDriveTime.Text = "drive time";

            this.labelDriveTime.TextAlign = System.Drawing.ContentAlignment.TopCenter;

//

// labelDistance

//

            this.labelDistance.Font = new System.Drawing.Font("Nina", 12F, System.Drawing.FontStyle.Bold);

            this.labelDistance.Location = new System.Drawing.Point(7, 126);

            this.labelDistance.Size = new System.Drawing.Size(163, 24);

            this.labelDistance.Text = "distance";

            this.labelDistance.TextAlign = System.Drawing.ContentAlignment.TopCenter;

//

// Form1

//

            this.ClientSize = new System.Drawing.Size(176, 180);

            this.Controls.Add(this.comboBoxFrom);

            this.Controls.Add(this.textBoxFromTo);

            this.Controls.Add(this.labelDriveTime);

            this.Controls.Add(this.labelDistance);

            this.Controls.Add(this.labelAsOfTime);

            this.Menu = this.mainMenu1;

            this.Text = "BayAreaTraffic";

 

        }

 

        #endregion

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose(bool disposing)

        {

            base.Dispose(disposing);

        }

 

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        static void Main()

        {

            Application.Run(new Form1());

        }

 

        private void menuItem1_Click(object sender, EventArgs e)

        {

            // hack - until abort implemented

            if (this.menuItem1.Text != "Stop")

            {

                Cursor.Current = Cursors.WaitCursor;

                this.menuItem1.Text = "Stop";

                backgroundWorker1.RunWorkerAsync(((RouteListItem)org.routeList[comboBoxFrom.SelectedIndex]).url);

            }

        }

 

        // Set up the BackgroundWorker object by

        // attaching event handlers.

        private void InitializeBackgoundWorker()

        {

            backgroundWorker1 = new BackgroundWorker();

            backgroundWorker1.WorkerReportsProgress = false;

            backgroundWorker1.WorkerSupportsCancellation = false;

            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);

            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);

        }

 

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

        {

            string url = e.Argument.ToString();

            org.maslan.www.DriveTime dt = null;

            dt = getTraffic(url);

            e.Result = dt;

        }

 

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            if (e.Error != null)

            {

                MessageBox.Show(e.Error.Message);

            }

            else

            {

                org.maslan.www.DriveTime dt = (org.maslan.www.DriveTime)e.Result;

                textBoxFromTo.Text = "From: " + dt.from + "\r\nto: " + dt.to;

                labelDriveTime.Text = dt.driveTime;

                labelDistance.Text = dt.distance;

                labelAsOfTime.Text = "as of " + DateTime.Now.ToLongTimeString();

            }

            this.menuItem1.Text = "Fetch";

           // this.Text = "BayAreaTraffic";

            Cursor.Current = Cursors.Default;

        }

 

        private org.maslan.www.DriveTime getTraffic(string url)

        {

            org.maslan.www.DriveTime dt = null;

            org.maslan.www.TrafficService ts = new org.maslan.www.TrafficService();

            try

            {

                dt = ts.GetDriveTime(url);

            }

            catch (Exception x)

            {

                MessageBox.Show(x.ToString());

            }

            return dt;

        }

 

        private void menuItem3_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }

 

        private void menuItem4_Click(object sender, EventArgs e)

        {

            System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();

            string fullName = ass.FullName;

            MessageBox.Show(fullName +

                "\n\nCopyright © 2005 Carter Maslan\nblogs.msdn.com/jcmaslan\n\nData screen-scraped from www.511.or", "About");

        }

 

        private void menuItem5_Click(object sender, EventArgs e)

        {

            Form2 f2 = new Form2();

            f2.Show();

        }

 

    }

}