tbf/Workplace/Forms/WeightLimitsWizardDlg.cs

151 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using Workplace.Resources;
using System.Text;
namespace Workplace.Forms
{
public partial class WeightLimitsWizardDlg : Form
{
public double MinWeight;
public double MaxWeight;
public string WeightOptionsWay;
IList<WizardMenu> menus;
string[] levelPaths;
int currentLevel;
WizardMenu currentMenu;
public WeightLimitsWizardDlg(string initialPath)
{
InitializeComponent();
levelPaths = Directory.Exists(initialPath) ? Directory.GetDirectories(initialPath) : new string[0];
menus = new List<WizardMenu>();
currentLevel = 0;
WeightOptionsWay = string.Empty;
AddListBoxWithOptions(levelPaths[currentLevel], Path.GetFileName(levelPaths[currentLevel]));
}
void AddListBoxWithOptions(string path, string title)
{
currentMenu = new WizardMenu(path, title, Height - 20);
flowLayoutPanel1.Controls.Add(currentMenu.ListBox);
currentMenu.Enable(this.listBox_SelectedIndexChanged);
menus.Add(currentMenu);
Text = title;
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
currentMenu.Disable(listBox_SelectedIndexChanged);
switch (currentMenu.Selection)
{
case Selection.Leave:
double minWeight, maxWeight;
if (!Process(currentMenu.SelectedFile, out minWeight, out maxWeight))
{
MessageBox.Show(string.Format(Strings.Invalid_file_0, currentMenu.SelectedFile));
DialogResult = DialogResult.Cancel;
Close();
return;
}
if (++currentLevel < levelPaths.Length)
{
/// Go to the next level / the next wizzard tree
AddListBoxWithOptions(levelPaths[currentLevel], Path.GetFileName(levelPaths[currentLevel]));
return;
}
/// Calculate min/max weight and the options way
MinWeight = 0;
MaxWeight = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < menus.Count; i++)
{
if (sb.Length > 0) sb.Append(" - ");
sb.Append(menus[i].SelectedOption);
if (menus[i].Selection == Selection.Leave)
{
if (Process(menus[i].SelectedFile, out minWeight, out maxWeight))
{
MinWeight += minWeight;
MaxWeight += maxWeight;
}
}
}
WeightOptionsWay = sb.ToString();
if (MessageBox.Show(string.Format("{0} = {1}g, {2} = {3}g", Strings.Min_weight, MinWeight, Strings.Max_weight, MaxWeight),
Strings.Confirm_weight_limits,
MessageBoxButtons.OKCancel,
MessageBoxIcon.Asterisk) == DialogResult.OK)
{
DialogResult = DialogResult.OK;
Close();
}
return;
case Selection.Submenu:
AddListBoxWithOptions(currentMenu.SelectedPath, Text);
return;
case Selection.Back:
if (menus.Count == 1)
{
DialogResult = DialogResult.Cancel;
Close();
}
else if (menus.Count > 1 && menus[menus.Count - 1] == currentMenu)
{
/// Make one step back
flowLayoutPanel1.Controls.Remove(currentMenu.ListBox);
menus.RemoveAt(menus.Count - 1);
currentMenu = menus[menus.Count - 1];
if (Text != currentMenu.Title)
{
Text = currentMenu.Title;
currentLevel--;
}
currentMenu.Enable(listBox_SelectedIndexChanged);
}
return;
default:
DialogResult = DialogResult.Cancel;
Close();
return;
}
}
bool Process(string fileName, out double minWeight, out double maxWeight)
{
using (TextReader reader = new StreamReader(fileName))
{
string line1 = reader.ReadLine();
string line2 = reader.ReadLine();
if ((double.TryParse(line1, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out minWeight) ||
double.TryParse(line1, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out minWeight)) &&
(double.TryParse(line2, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture, out maxWeight) ||
double.TryParse(line2, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out maxWeight)) &&
maxWeight >= minWeight &&
minWeight >= 0)
{
return true;
}
}
minWeight = maxWeight = 0;
return false;
}
}
}