- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/InfoTransmitter/TransmitterForm.cs

    00001 ﻿//-----------------------------------------------------------------------------
    00002 // TransmitterForm.cs
    00003 // (A. Schiffler, 2009-2011)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace InfoTransmitter
    00007 {
    00008     using System;
    00009     using System.Drawing;
    00010     using System.IO;
    00011     using System.Text;
    00012     using System.Windows.Forms;
    00013     using NewGamePhysics.Networking;
    00014
    00018     public partial class TransmitterForm : Form
    00019     {
    00020         InfoLinkTransmitter infoLinkTransmitter;
    00021
    00022         string userMessage = string.Empty;
    00023
    00029         public TransmitterForm()
    00030         {
    00031             InitializeComponent();
    00032
    00033             this.infoLinkTransmitter = new InfoLinkTransmitter();
    00034             this.infoLinkTransmitter.StartTransmitter();
    00035
    00036             this.timer1.Enabled = true;
    00037             UpdateTransmitButton();
    00038         }
    00039
    00045         private void buttonTransmit_Click(object sender, EventArgs e)
    00046         {
    00047             if (!string.IsNullOrEmpty(textBoxUrl.Text))
    00048             {
    00049                 string url = textBoxUrl.Text;
    00050
    00051                 // Add http
    00052                 if (!url.Equals("about:blank") &&
    00053                     !url.StartsWith("http://") &&
    00054                     !url.StartsWith("https://"))
    00055                 {
    00056                     url = "http://" + url;
    00057                 }
    00058
    00059                 Uri uri;
    00060                 try
    00061                 {
    00062                     uri = new Uri(url, UriKind.Absolute);
    00063                 }
    00064                 catch (System.UriFormatException)
    00065                 {
    00066                     this.userMessage = "Invalid URL ...";
    00067                     this.textBoxUrl.ForeColor = Color.Red;
    00068                     this.textBoxUrl.Focus();
    00069                     return;
    00070                 }
    00071
    00072                 this.userMessage = "Sending ...";
    00073                 InfoLink infoLink = new InfoLink(
    00074                     this.textBoxCategory.Text,
    00075                     this.textBoxSubCategory.Text,
    00076                     this.textBoxTitle.Text,
    00077                     uri);
    00078                 this.infoLinkTransmitter.Transmit(infoLink);
    00079
    00080                 this.textBoxTitle.Text = string.Empty;
    00081                 this.textBoxUrl.Text = string.Empty;
    00082                 this.textBoxTitle.Focus();
    00083             }
    00084         }
    00085
    00089         private void UpdateTransmitButton()
    00090         {
    00091             this.buttonTransmit.Enabled =
    00092                 (!string.IsNullOrEmpty(this.textBoxCategory.Text)) &&
    00093                 (!string.IsNullOrEmpty(this.textBoxSubCategory.Text)) &&
    00094                 (!string.IsNullOrEmpty(this.textBoxTitle.Text)) &&
    00095                 (!string.IsNullOrEmpty(this.textBoxUrl.Text));
    00096         }
    00097
    00103         private void textBoxUrl_TextChanged(object sender, EventArgs e)
    00104         {
    00105             UpdateTransmitButton();
    00106         }
    00107
    00113         private void textBoxCategory_TextChanged(object sender, EventArgs e)
    00114         {
    00115             UpdateTransmitButton();
    00116         }
    00117
    00123         private void textBoxTitle_TextChanged(object sender, EventArgs e)
    00124         {
    00125             UpdateTransmitButton();
    00126         }
    00127
    00133         private void textBoxCategory_KeyDown(object sender, KeyEventArgs e)
    00134         {
    00135             if (e.KeyCode == Keys.Enter)
    00136             {
    00137                 this.textBoxSubCategory.Focus();
    00138             }
    00139         }
    00140
    00146         private void textBoxSubCategory_KeyDown(object sender, KeyEventArgs e)
    00147         {
    00148             if (e.KeyCode == Keys.Enter)
    00149             {
    00150                 this.textBoxTitle.Focus();
    00151             }
    00152         }
    00153
    00159         private void textBoxTitle_KeyDown(object sender, KeyEventArgs e)
    00160         {
    00161             if (e.KeyCode == Keys.Enter)
    00162             {
    00163                 this.textBoxUrl.Focus();
    00164             }
    00165         }
    00166
    00172         private void textBoxUrl_KeyDown(object sender, KeyEventArgs e)
    00173         {
    00174             this.textBoxUrl.ForeColor = Color.Black;
    00175             if (e.KeyCode == Keys.Enter)
    00176             {
    00177                 this.buttonTransmit.Focus();
    00178             }
    00179         }
    00180
    00186         private void TransmitterForm_FormClosing(object sender, FormClosingEventArgs e)
    00187         {
    00188             this.infoLinkTransmitter.StopTransmitter();
    00189             e.Cancel = false;
    00190         }
    00191
    00197         private void timer1_Tick(object sender, EventArgs e)
    00198         {
    00199             long transmitted = this.infoLinkTransmitter.Transmitted;
    00200             long errors = this.infoLinkTransmitter.Errors;
    00201
    00202             string message = string.Empty;
    00203
    00204             if (string.IsNullOrEmpty(this.userMessage))
    00205             {
    00206                 if (transmitted > 0)
    00207                 {
    00208                     message += string.Format("  Transmitted InfoLinks: {0}", transmitted);
    00209                 }
    00210
    00211                 if (errors > 0)
    00212                 {
    00213                     message += string.Format("  Errors: {0}", errors);
    00214                 }
    00215             }
    00216             else
    00217             {
    00218                 message = this.userMessage;
    00219                 this.userMessage = string.Empty;
    00220             }
    00221
    00222             this.labelSending.Text = message.Trim();
    00223         }
    00224
    00230         private void toolStripMenuItemExit_Click(object sender, EventArgs e)
    00231         {
    00232             this.Close();
    00233         }
    00234
    00240         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    00241         {
    00242             MessageBox.Show(
    00243                 "InfoLink Transmitter" + Environment.NewLine +
    00244                 "Transmitting UDP packets on port: " + this.infoLinkTransmitter.Port + Environment.NewLine +
    00245                 "(C) A. Schiffler, 2009-2011" + Environment.NewLine,
    00246                 "InfoTransmitter About");
    00247         }
    00248
    00254         private void toolStripMenuItemLoad_Click(object sender, EventArgs e)
    00255         {
    00256             OpenFileDialog openFileDialog = new OpenFileDialog();
    00257             openFileDialog.Filter = "InfoLink XML|*.xml|All|*.*";
    00258             string startDir = Path.Combine(Environment.CurrentDirectory, "InfoLinkData");
    00259             if (Directory.Exists(startDir))
    00260             {
    00261                 openFileDialog.InitialDirectory = startDir;
    00262             }
    00263             else
    00264             {
    00265                 openFileDialog.InitialDirectory = Environment.CurrentDirectory;
    00266             }
    00267
    00268             if (openFileDialog.ShowDialog() == DialogResult.OK)
    00269             {
    00270                 string fileContent = System.IO.File.ReadAllText(openFileDialog.FileName, Encoding.Unicode);
    00271                 InfoLink[] infoLinks = InfoLinkSerializer.DeserializeArray(fileContent);
    00272                 this.infoLinkTransmitter.Transmit(infoLinks);
    00273                 this.userMessage = "Sending " + infoLinks.Length + " ...";
    00274             }
    00275         }
    00276     }
    00277 }