tbf/ResultsBrowser/Forms/PrinterConfigDlg.cs

171 lines
4.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Config.Entities;
using TBF.BenchControl;
using TBF.BenchControl.Generic;
namespace ResultsBrowser.Forms
{
public partial class PrinterConfigDlg : Form
{
public string PrinterClass; /// To be initialized in the constructor
public string PrinterCfg;
IList<IComponentFactory> printerFactories; /// Printer components classes
IComponentFactory printerFactory;
public PrinterConfigDlg()
{
InitializeComponent();
printerFactories = new List<IComponentFactory>();
foreach (var fctry in TbfComponents.Factories)
{
if (fctry.ClassName.Contains("Output.Printers.")) printerFactories.Add(fctry);
}
}
private void PrinterClassDlg_Load(object sender, EventArgs e)
{
printerClassComboBox.Items.Add("---");
foreach (var fctry in printerFactories)
{
printerClassComboBox.Items.Add(fctry.ClassName);
}
printerClassComboBox.Text = string.IsNullOrEmpty(PrinterClass) ? "---" : PrinterClass;
}
private void changePrinterClassButton_Click(object sender, EventArgs e)
{
printerClassComboBox.Enabled = true;
}
private void cancelButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void configureButton_Click(object sender, EventArgs e)
{
string oriClassName = PrinterClass;
PrinterClass = string.Empty;
if (printerClassComboBox.Text != "---")
{
foreach (var pc in printerFactories)
{
if (printerClassComboBox.Text == pc.ClassName) PrinterClass = pc.ClassName;
}
}
printerFactory = GetFactory(PrinterClass);
if (printerFactory == null)
{
MessageBox.Show("Invalid printer");
return;
}
IComponentCfg printerCfg;
if ((oriClassName != printerFactory.ClassName) || string.IsNullOrEmpty(PrinterCfg))
{
printerCfg = printerFactory.DefaultConfig();
}
else
{
Config.Entities.Component cmpnt = new Config.Entities.Component();
cmpnt.Name = "printer";
cmpnt.ClassName = PrinterClass;
cmpnt.Parameters = PrinterCfg;
printerCfg = printerFactory.CmpntCfgFromCmpntEntity(cmpnt);
}
IComponentCfgCtrl cfgControl = printerCfg.GetControl();
cfgControl.Config = printerCfg;
cfgControl.Config.ItemNr = 0;
TBF.UI.Bench.Components.ComponentParametersDlg cfgForm = new TBF.UI.Bench.Components.ComponentParametersDlg();
cfgForm.CmpntEntities = new List<Component>();
cfgForm.ComponentCfgCtrl = cfgControl;
cfgForm.UnlockAfterStart = true;
DialogResult dr = cfgForm.ShowDialog();
if (dr != DialogResult.OK) return;
PrinterCfg = (printerCfg != null) ? printerCfg.CreateDbEntity().Parameters : string.Empty;
DialogResult = DialogResult.OK;
Close();
}
private void importButton_Click(object sender, EventArgs e)
{
string oriClassName = PrinterClass;
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK) return;
/// Import a procedure from a file
Component newComponent = Component.Import(new StreamReader(dlg.FileName, System.Text.Encoding.UTF8));
bool classIsOK = false;
foreach (var pc in printerFactories)
{
if (newComponent.ClassName == pc.ClassName) { classIsOK = true; break; }
}
if (classIsOK)
{
printerClassComboBox.Text = newComponent.ClassName;
IComponentCfg printerCfg = TbfComponents.CmpntCfgFromCmpntEntity(newComponent);
if (printerCfg != null)
{
PrinterClass = newComponent.ClassName;
PrinterCfg = printerCfg.CreateDbEntity().Parameters;
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show("Importing printer configuration failed, file is corrupt.");
return;
}
}
else
{
MessageBox.Show("Invalid class of the imported component.");
return;
}
}
/// <summary>
/// Updates component factory
/// </summary>
/// <param name="className">Component class name</param>
/// <param name="factory">Reference to a factory</param>
/// <returns>true when factory changed</returns>
IComponentFactory GetFactory(string className)
{
if (!string.IsNullOrEmpty(className))
{
foreach (var fac in TbfComponents.Factories)
{
if (fac.ClassName == className) return fac;
}
}
return null;
}
}
}