tbf/Workplace/CheckItems/Keyboard.cs

190 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SharedDatabase;
using SharedDatabase.Entities;
namespace Workplace.CheckItems
{
public partial class Keyboard : UserControl, ICheckItem
{
public override string ToString()
{
return string.Format("Kbd: {0} {1}", IxPolozky, Part.Name);
}
WorkplaceDlg parent;
public Part Part { get { return part; } }
Part part;
public int IxPolozky
{
get { return ixPolozky; }
set { ixPolozky = value; }
}
int ixPolozky;
public IList<string> BlackListedCodes
{
get { return blackListedCodes; }
set
{
blackListedCodes = value;
blackListLabel.Visible = (blackListedCodes != null) && (blackListedCodes.Count > 0);
}
}
IList<string> blackListedCodes;
public string ScannedCode
{
get { return scannedCodeTextBox.Text; }
set
{
scannedCodeTextBox.Text = value;
blackListLabel.Visible = false;
}
}
Keyboard()
{
InitializeComponent();
blackListedCodes = new List<string>();
}
public Keyboard(WorkplaceDlg parent, Part part)
: this()
{
this.parent = parent;
this.part = part;
scannedCodeTextBox.Text = (part.DefaultCode != null) ? part.DefaultCode : string.Empty;
kodMaterialuLabel.Text = part.Name;
descriptionMaterialuLabel.Text = part.Description;
}
public void ClearCode()
{
if (part.CodeType != (sbyte)CodeType.Silicon)
{
scannedCodeTextBox.Text = (part.DefaultCode != null) ? part.DefaultCode : string.Empty;
}
}
public void SubmitCode(string code)
{
scannedCodeTextBox.Text = code;
scannedCodeTextBox_KeyPress(null, new KeyPressEventArgs('\r'));
}
public void ResetFocus()
{
setFocusButton.Text = "";
}
public void SetFocus()
{
ClearCode();
setFocusButton.Text = ">";
scannedCodeTextBox.Focus();
}
private void setFocusButton_Click(object sender, EventArgs e)
{
setFocusButton.Text = ">";
parent.OnFocusPressed(this, new FocusPressedEventArgs(IxPolozky, Part.CodeLocation));
}
private void scannedCodeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != '\r') return;
if (blackListedCodes != null)
{
if (null != blackListedCodes.FirstOrDefault<string>(x => x.Equals(scannedCodeTextBox.Text)))
{
parent.OnBarcodeReceived(this, new BarcodeReceivedEventArgs(scannedCodeTextBox.Text, IxPolozky, Part.CodeLocation, PartError.BlacklistedPart));
return;
}
}
PartError error = PartError.None;
///
if (string.IsNullOrEmpty(scannedCodeTextBox.Text))
{
error = PartError.FieldEmpty;
}
parent.OnBarcodeReceived(this, new BarcodeReceivedEventArgs(scannedCodeTextBox.Text, IxPolozky, Part.CodeLocation, error));
return;
}
private void PolozkaKbd_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right) return;
LocalSettingsFormForPolozka dlg = new LocalSettingsFormForPolozka();
if (dlg.ShowDialog() != DialogResult.OK) return;
if (string.IsNullOrEmpty(dlg.BlackListFileName))
{
if (this.ixPolozky == Program.LocalSettings.BlackListIx1)
{
Program.LocalSettings.BlackListIx1 = 0;
Program.LocalSettings.BlackListFile1 = string.Empty;
}
if (this.ixPolozky == Program.LocalSettings.BlackListIx2)
{
Program.LocalSettings.BlackListIx2 = 0;
Program.LocalSettings.BlackListFile2 = string.Empty;
}
if (this.ixPolozky == Program.LocalSettings.BlackListIx3)
{
Program.LocalSettings.BlackListIx3 = 0;
Program.LocalSettings.BlackListFile3 = string.Empty;
}
}
else if (Program.LocalSettings.BlackListIx1 == this.ixPolozky)
{
Program.LocalSettings.BlackListIx1 = this.ixPolozky;
Program.LocalSettings.BlackListFile1 = dlg.BlackListFileName;
}
else if (Program.LocalSettings.BlackListIx2 == this.ixPolozky)
{
Program.LocalSettings.BlackListIx2 = this.ixPolozky;
Program.LocalSettings.BlackListFile2 = dlg.BlackListFileName;
}
else if (Program.LocalSettings.BlackListIx3 == this.ixPolozky)
{
Program.LocalSettings.BlackListIx3 = this.ixPolozky;
Program.LocalSettings.BlackListFile3 = dlg.BlackListFileName;
}
else if (Program.LocalSettings.BlackListIx1 == 0)
{
Program.LocalSettings.BlackListIx1 = this.ixPolozky;
Program.LocalSettings.BlackListFile1 = dlg.BlackListFileName;
}
else if (Program.LocalSettings.BlackListIx2 == 0)
{
Program.LocalSettings.BlackListIx2 = this.ixPolozky;
Program.LocalSettings.BlackListFile2 = dlg.BlackListFileName;
}
else if (Program.LocalSettings.BlackListIx3 == 0)
{
Program.LocalSettings.BlackListIx3 = this.ixPolozky;
Program.LocalSettings.BlackListFile3 = dlg.BlackListFileName;
}
else
{
MessageBox.Show("Too many black lists, the black list was not saved !");
return;
}
BlackListedCodes = !string.IsNullOrEmpty(dlg.BlackListFileName) ? Workplace.BlackList.FromFile(dlg.BlackListFileName).Codes : null;
Program.LocalSettings.Save();
}
}
}