EduNetworkBuilder/EduNetworkBuilder/NetTestEditor.cs
2015-11-19 14:49:14 -06:00

280 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Resources;
namespace EduNetworkBuilder
{
public partial class NetTestEditor : Form
{
NetTest ToEdit;
NetTest OrigTest;
List<string> HostNames = new List<string>();
List<string> Networks = new List<string>();
List<string> Broadcasts = new List<string>();
bool processing = false;
public NetTestEditor(NetTest WhatToEdit)
{
InitializeComponent();
LanguagifyComponents();
OrigTest = WhatToEdit;
ToEdit = new NetTest(OrigTest);
Network mynet = NB.GetNetwork();
HostNames.AddRange(mynet.GetHostnames(true));
Networks.AddRange(mynet.GetSubnets());
Broadcasts.AddRange(mynet.GetBroadcasts());
UpdateForm();
}
private void LanguagifyComponents()
{
Text = NB.Translate("NTE_lblSource");
Text = NB.Translate("NTE_lblTest");
Text = NB.Translate("NTE_lblDest");
Text = NB.Translate("_Done");
Text = NB.Translate("_Cancel");
Text = NB.Translate("NTE_Form");
}
private void btnOK_Click(object sender, EventArgs e)
{
OrigTest.UpdateValuesFromAnother(ToEdit);
OrigTest.TaskWasDone = false; //We edited it. Set it to false
OrigTest.SetInitialDoneState(); //Set some of them to true (locked. etc)
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void UpdateForm()
{
NetworkDevice sItem;
processing = true;
Network theNet = NB.GetNetwork();
//We add all the hostnames as source
cbSource.Items.Clear();
foreach(string host in HostNames)
{
cbSource.Items.Add(host);
}
//The tests are just the list of available tests.
cbTest.Items.Clear();
List<string> tList = new List<string>();
foreach (string test in Enum.GetNames(typeof(NetTestType)))
{
tList.Add(test);
}
tList.Sort(); //Sort them alphabetically. Just to make things easier
foreach (string test in tList)
{
cbTest.Items.Add(test);
}
cbDest.Items.Clear();
if (ToEdit.TheTest == NetTestType.NeedsRouteToNet)
{ //List all the networks
foreach (string subnet in Networks)
{
cbDest.Items.Add(subnet);
}
}
else if (ToEdit.TheTest == NetTestType.DHCPServerEnabled)
{
cbDest.Items.Add(true.ToString());
cbDest.Items.Add(false.ToString());
}
else if (ToEdit.TheTest == NetTestType.HelpRequest)
{
foreach (string HelpLevel in Enum.GetNames(typeof(NetTestVerbosity)))
{
cbDest.Items.Add(HelpLevel);
}
cbDest.Items.Add("?Button");
}
else if (ToEdit.TheTest == NetTestType.ReadContextHelp)
{
cbSource.Items.Clear();
List<string> theNames = new List<string>();
theNames.AddRange(Enum.GetNames(typeof(HelpTopics)));
theNames.Sort();
theNames.Remove("None");
foreach (string ContextHelp in theNames)
{
cbSource.Items.Add(ContextHelp);
}
cbDest.Items.Add(NB.Translate("_Read"));
}
else if (ToEdit.TheTest == NetTestType.LockAll || ToEdit.TheTest == NetTestType.LockGateway)
{
cbDest.Items.Add(NB.Translate("_All"));
}
else if (ToEdit.TheTest == NetTestType.LockDHCP)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach(string ip in sItem.DHCPStrings(false))
{
cbDest.Items.Add(ip);
}
}
else if (ToEdit.TheTest == NetTestType.LockIP)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (IPAddress ip in sItem.IPAddressList())
{
cbDest.Items.Add(ip.GetIPString);
}
}
else if (ToEdit.TheTest == NetTestType.LockRoute)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (IPAddress ip in sItem.ListRoutes())
{
cbDest.Items.Add(ip.GetIPString);
}
}
else if (ToEdit.TheTest == NetTestType.LockNic)
{
//return all the dhcp ranges
sItem = theNet.GetDeviceFromName(ToEdit.sHost);
foreach (string nname in sItem.NICNames())
{
cbDest.Items.Add(nname);
}
}
else
{ //List all the hosts
foreach (string host in HostNames)
{
cbDest.Items.Add(host);
}
if (ToEdit.TheTest == NetTestType.SuccessfullyPings)
{ //List all the network broadcasts
foreach (string subnet in Broadcasts)
{
cbDest.Items.Add(subnet);
}
}
}
//Now we select all the appropriate items.
if(cbSource.Items.Contains(ToEdit.sHost))
{
cbSource.SelectedItem = ToEdit.sHost;
}
else
{
if(cbSource.Items.Count > 0)
cbSource.SelectedIndex = 0; //select the first item
}
if (cbDest.Items.Contains(ToEdit.dHost))
{
cbDest.SelectedItem = ToEdit.dHost;
}
else
{
if (cbDest.Items.Count > 0)
cbDest.SelectedIndex = 0; //select the first item
}
if (cbTest.Items.Contains(ToEdit.TheTest.ToString()))
{
cbTest.SelectedItem = ToEdit.TheTest.ToString();
}
else
{
if (cbTest.Items.Count > 0)
cbTest.SelectedIndex = 0; //select the first item
}
processing = false;
}
private void NetTestEditor_Load(object sender, EventArgs e)
{
}
private bool validate_choices()
{
if (processing) return true; //If we are processing, we are all OK.
Network theNet = NB.GetNetwork();
NetTestType ntt = NB.ParseEnum<NetTestType>(cbTest.SelectedItem.ToString());
if (ntt == NetTestType.ReadContextHelp) return true;
if (ntt == NetTestType.DHCPServerEnabled) return true;
if (cbSource.SelectedItem.ToString() == cbDest.SelectedItem.ToString() && ntt != NetTestType.HelpRequest)
return false; //Source cannot equal dest
if (theNet.GetDeviceFromName(cbSource.SelectedItem.ToString()) == null)
return false; //This should never happen with a drop-down list, but just in case...
if (ntt == NetTestType.LockAll || ntt == NetTestType.LockDHCP || ntt == NetTestType.LockIP ||
ntt == NetTestType.LockNic || ntt == NetTestType.LockRoute)
return true;
if(ntt == NetTestType.NeedsRouteToNet)
{
//We should have a network, not a host.
}
else if (ntt == NetTestType.HelpRequest)
{
//This uses a verbosity
}
else
{
if (ntt == NetTestType.SuccessfullyPings) return true;
if (theNet.GetDeviceFromName(cbDest.SelectedItem.ToString()) == null)
return false; //This should never happen with a drop-down list, but just in case...
}
return true;
}
private void cbTest_SelectedValueChanged(object sender, EventArgs e)
{
ToEdit.TheTest = NB.ParseEnum<NetTestType>(cbTest.SelectedItem.ToString());
if (!processing)
UpdateForm();
}
private void cbSource_SelectedValueChanged(object sender, EventArgs e)
{
if (validate_choices())
{
ToEdit.sHost = cbSource.SelectedItem.ToString();
}
else
if (!processing)
UpdateForm();
}
private void cbDest_SelectedValueChanged(object sender, EventArgs e)
{
if (validate_choices())
{
ToEdit.dHost = cbDest.SelectedItem.ToString();
}
else
if (!processing)
UpdateForm();
}
}
}