using System;
using System.Collections.Generic;
using TBF.BenchControl;
using TBF.BenchControl.GenericDevices;
namespace TBF.BenchControl.DataEntry.Munich3
{
public class EntryForm : Generic.IComponent, IOperation, GenericDevices.IDataEntry, GenericDevices.IHasProtocolTitle
{
readonly EntryFormCfg entryFormCfg;
public Generic.IComponentCfg Cfg { get { return entryFormCfg; } }
public static void ResetStaticProperties() { }
public string Name { get { return entryFormCfg.Name; } }
///
/// Properties set by the Begin and the End form
///
public string ProtocolTitle { get { return protocolTitle; } }
string protocolTitle;
System.Windows.Forms.Form modelessDlg;
IList waterMeters;
public enum CurrentOp
{
None,
ShowFormAtCycleBeginning,
ShowFormAtCycleEnd,
}
CurrentOp currentOp;
public EntryForm(EntryFormCfg cfg)
{
if (cfg == null) throw new ArgumentNullException("cfg");
this.entryFormCfg = cfg;
currentOp = CurrentOp.None;
}
/// Reference to the operation
public IOperation ShowFormAtCycleBeginningOp(IList waterMeters)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleBeginning;
this.waterMeters = waterMeters;
return this;
}
/// Reference to the operation
public IOperation ShowFormAtCycleEndOp(IList waterMeters)
{
if (currentOp != CurrentOp.None) throw new Exception("Sequence error");
currentOp = CurrentOp.ShowFormAtCycleEnd;
this.waterMeters = waterMeters;
return this;
}
delegate void EntryFormDlgt(EntryForm myRef);
///
void OpenBeginningDlg(EntryForm myRef)
{
myRef.modelessDlg = new CycleBeginningForm(Program.WMsCount);
modelessDlg.Show();
}
///
void OpenEndDlg(EntryForm myRef)
{
CycleEndForm endForm = new CycleEndForm();
endForm.ProtocolName1 = entryFormCfg.ProtocolTitle1;
endForm.ProtocolName2 = entryFormCfg.ProtocolTitle2;
endForm.ProtocolName3 = entryFormCfg.ProtocolTitle3;
myRef.modelessDlg = endForm;
modelessDlg.Show();
}
/// Start this operation
public void Start()
{
switch (currentOp)
{
case CurrentOp.ShowFormAtCycleBeginning:
Program.MainWnd.Invoke(new EntryFormDlgt(OpenBeginningDlg), this);
break;
case CurrentOp.ShowFormAtCycleEnd:
Program.MainWnd.Invoke(new EntryFormDlgt(OpenEndDlg), this);
break;
}
}
/// Run this operation
/// Event.ResultsPrinted
public Event Run()
{
if (!(modelessDlg as GenericDevices.IHasCompleted).Completed) return Event.None;
if (modelessDlg is CycleBeginningForm)
{
CycleBeginningForm dlg = (modelessDlg as CycleBeginningForm);
if (dlg.WaterMetersCount > 0 && waterMeters[0] != null) waterMeters[0].SerialNr = dlg.SNText[0];
if (dlg.WaterMetersCount > 1 && waterMeters[1] != null) waterMeters[1].SerialNr = dlg.SNText[1];
if (dlg.WaterMetersCount > 2 && waterMeters[2] != null) waterMeters[2].SerialNr = dlg.SNText[2];
if (dlg.WaterMetersCount > 3 && waterMeters[3] != null) waterMeters[3].SerialNr = dlg.SNText[3];
if (dlg.WaterMetersCount > 4 && waterMeters[4] != null) waterMeters[4].SerialNr = dlg.SNText[4];
if (dlg.WaterMetersCount > 5 && waterMeters[5] != null) waterMeters[5].SerialNr = dlg.SNText[5];
}
else if (modelessDlg is CycleEndForm)
{
CycleEndForm dlg = (modelessDlg as CycleEndForm);
protocolTitle = dlg.ProtocolTitle;
if (dlg.WaterMetersCount > 0 && waterMeters[0] != null) waterMeters[0].EndState = dlg.EndStateText[0];
if (dlg.WaterMetersCount > 1 && waterMeters[1] != null) waterMeters[1].EndState = dlg.EndStateText[1];
if (dlg.WaterMetersCount > 2 && waterMeters[2] != null) waterMeters[2].EndState = dlg.EndStateText[2];
if (dlg.WaterMetersCount > 3 && waterMeters[3] != null) waterMeters[3].EndState = dlg.EndStateText[3];
if (dlg.WaterMetersCount > 4 && waterMeters[4] != null) waterMeters[4].EndState = dlg.EndStateText[4];
if (dlg.WaterMetersCount > 5 && waterMeters[5] != null) waterMeters[5].EndState = dlg.EndStateText[5];
}
return Event.Done;
}
/// Stop this operation
public void Stop()
{
modelessDlg = null;
currentOp = CurrentOp.None;
}
///
/// Updates test results with the information collected in watermeters.
///
/// Watermetes with information entered in the forms
/// Test results to be updated with the information
public void UpdateTestResults(IList waterMeters, IList results)
{
foreach (var test in results)
{
if (test.MetersKind != Entities.MetersKind.Single) continue;
//TODO
}
}
}
}