tbf/TestBenchFramework/BenchControl/Output/Printers/Enhanced/PrinterCfgCtrl.cs

284 lines
7.8 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using Config.Entities;
using TBF.BenchControl.Generic;
using TBF.Resources;
namespace TBF.BenchControl.Output.Printers.Enhanced
{
public partial class PrinterCfgCtrl : UserControl, IComponentCfgCtrl
{
static readonly ILog log = LogManager.GetLogger(typeof(PrinterCfgCtrl));
public bool ShowMore { get { return false; } }
PrinterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as PrinterCfg;
Redraw();
}
}
string header;
string[] commonItems;
string[] testItems;
string footer;
public PrinterCfgCtrl()
{
InitializeComponent();
Localize();
orientationComboBox.Items.Add(PageOrientation.Portrait.ToString());
orientationComboBox.Items.Add(PageOrientation.Landscape.ToString());
}
private void PrinterCfgCtrl_Load(object sender, EventArgs e)
{
for (Culture s = 0; s < Culture.Count; s++) cultureComboBox.Items.Add(s.ToString());
header = config.Header;
commonItems = config.CommonItems;
testItems = config.SelectedItems;
footer = config.Footer;
Redraw();
}
void Localize()
{
nameLabel.Text = Strings.Name;
cultureLabel.Text = "Culture";
headerButton.Text = Strings.Header;
commonItemsButton.Text = "Common items";
testItemsButton.Text = "Test items";
footerButton.Text = Strings.Footer;
}
public void Closing()
{
}
PageOrientation GetOrientation(string str)
{
if (str.Equals(PageOrientation.Landscape.ToString())) return PageOrientation.Landscape;
if (str.Equals(PageOrientation.Portrait.ToString())) return PageOrientation.Portrait;
return (PageOrientation)(-1);
}
void Redraw()
{
if (config == null) return; /// Control was not loaded, settings were not changed
classNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
orientationComboBox.Text = config.PageOrientation.ToString();
topMarginTextBox.Text = config.TopMargin.ToString();
bottomMarginTextBox.Text = config.BottomMargin.ToString();
leftMarginTextBox.Text = config.LeftMargin.ToString();
supressPrintingCheckBox.Checked = config.SupressPrinting;
cultureComboBox.Text = config.Culture.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
orientationComboBox.Enabled = true;
topMarginTextBox.Enabled = true;
bottomMarginTextBox.Enabled = true;
leftMarginTextBox.Enabled = true;
supressPrintingCheckBox.Enabled = true;
cultureComboBox.Enabled = true;
headerButton.Enabled = true;
commonItemsButton.Enabled = true;
testItemsButton.Enabled = true;
footerButton.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if ((int)GetOrientation(orientationComboBox.Text) < 0)
{
message += Environment.NewLine + "Invalid 'Page orientation'";
flags |= CfgUpdateFlags.Error;
}
int dummy;
if (!int.TryParse(topMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 500)
{
message += Environment.NewLine + "'Top margin' should be between 0 and 500";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(bottomMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "'Bottom margin' should be between 0 and 200";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(leftMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "Left margin' should be between 0 and 200";
flags |= CfgUpdateFlags.Error;
}
if (!cultureComboBox.Items.Contains(cultureComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid culture";
}
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
if (config.Name != nameTextBox.Text)
{
config.Name = nameTextBox.Text;
flags |= (CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange);
}
if (config.PageOrientation != GetOrientation(orientationComboBox.Text))
{
config.PageOrientation = GetOrientation(orientationComboBox.Text);
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
int tmp = int.Parse(topMarginTextBox.Text);
if (config.TopMargin != tmp)
{
config.TopMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(bottomMarginTextBox.Text);
if (config.BottomMargin != tmp)
{
config.BottomMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(leftMarginTextBox.Text);
if (config.LeftMargin != tmp)
{
config.LeftMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.SupressPrinting != supressPrintingCheckBox.Checked)
{
config.SupressPrinting = supressPrintingCheckBox.Checked;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.CommonItems != commonItems)
{
config.CommonItems = commonItems;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.SelectedItems != testItems)
{
config.SelectedItems = testItems;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.Header != header)
{
config.Header = header;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.Footer != footer)
{
config.Footer = footer;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0)
{
Printer.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
}
return flags;
}
private void headerButton_Click(object sender, EventArgs e)
{
HeaderFooterDlg dlg = new HeaderFooterDlg(true, string.IsNullOrEmpty(header) ? string.Empty : header.Replace("~", Environment.NewLine));
if (dlg.ShowDialog() == DialogResult.OK)
{
header = dlg.EditedText.Replace(Environment.NewLine, "~");
}
}
private void commonItemsButton_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(commonItems),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
commonItems = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void testItemsButton_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg(true)
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(testItems),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
testItems = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void footerButton_Click(object sender, EventArgs e)
{
HeaderFooterDlg dlg = new HeaderFooterDlg(false, string.IsNullOrEmpty(footer) ? string.Empty : footer.Replace("~", Environment.NewLine));
if (dlg.ShowDialog() == DialogResult.OK)
{
footer = dlg.EditedText.Replace(Environment.NewLine, "~");
}
}
#region Configuration Change Handling
public static void OnCmdResponse(object sender, CmdResponseArgs args)
{
if (CmdResponseHandler == null) return;
try { CmdResponseHandler(sender, args); }
catch (Exception e) { log.Error("CmdResponseHandler(...) failed", e); }
}
public static event EventHandler<CmdResponseArgs> CmdResponseHandler;
public void StartResponseHandler() { }
public void StopResponseHandler() { }
#endregion Configuration Change Handling
}
}