using System; using System.Collections.Generic; using System.Windows.Forms; using SharedDatabase.Entities; using WorkflowConfigurator.Resources; namespace WorkflowConfigurator.UserControls { public partial class WorkstepCtrl : UserControl, IMyUI { bool unsavedChanges; bool suppressNotifications; bool limitedUnlock; bool completeUnlock; WorkstepsCtrl wstepsCtrl; TabPage tabPage; Process process; Workstep workstep; /// Constructor public WorkstepCtrl() { InitializeComponent(); Localize(); suppressNotifications = false; } public WorkstepCtrl(WorkstepsCtrl workstepsCtrl, TabPage tabPage, Process process, Workstep workstep) : this() { this.wstepsCtrl = workstepsCtrl; this.tabPage = tabPage; this.process = process; this.workstep = workstep; Data2UI(); } /// Localizes constant strings void Localize() { workstepNameLabel.Text = Strings.Workstep; workstepDescriptionLabel.Text = Strings.Description; refPartLabel.Text = Strings.Reference_part; } /// /// Enable/disable/hide UI elements based on lock/unlock/complete unlock state /// /// true = unlock for released processes /// true = unlock for processes in preparation (complete unlock) public void UpdateLockState(bool unlock, bool completely) { workstepNameTextBox.Enabled = completely; workstepDescriptionTextBox.Enabled = unlock; refPartComboBox.Enabled = completely; } /// /// Updates UI elements with workstep data entities. /// public void Data2UI() { suppressNotifications = true; workstepNameTextBox.Text = workstep.Name; workstepDescriptionTextBox.Text = workstep.Description; /// Show reference part, update combo box items refPartComboBox.Items.Clear(); refPartComboBox.Items.Add(Strings._none_); foreach (var p in process.Parts) { if (p.CodeLocation == SharedDatabase.CodeLocation.OnPart) refPartComboBox.Items.Add(p.ToString()); } refPartComboBox.Text = (workstep.ReferencePart != null) ? workstep.ReferencePart.ToString() : Strings._none_; /// Sort non-reference workstep parts before displaying Part[] sortedParts = new Part[workstep.Parts.Count]; int i = 0; foreach (var part in this.workstep.Parts) sortedParts[i++] = part; Array.Sort(sortedParts, (Part part1, Part part2) => part1.StepNumber.CompareTo(part2.StepNumber)); /// Display non-reference workstep parts listBox1.Items.Clear(); listBox2.Items.Clear(); foreach (var p in sortedParts) { if (p.CodeLocation == SharedDatabase.CodeLocation.OnPart) { listBox1.Items.Add(p); } else { listBox2.Items.Add(p); } } suppressNotifications = false; } /// /// Validates data in UI elements. /// /// Explanation message when UI data are NOT OK and this function returns false /// true when data are OK public bool VerifyUIData(out string message) { bool allValid = true; message = string.Empty; /// Reference part if (!refPartComboBox.Items.Contains(refPartComboBox.Text)) { message += string.Format(Strings.Invalid_0, Strings.Reference_part) + Environment.NewLine; allValid = false; } return allValid; } /// /// Updates workstep data entities from UI elements. /// public void UI2Data() { UI2Data(workstep.WorkstepNr); } public void UI2Data(int workstepNr) { workstep.WorkstepNr = workstepNr; workstep.Name = workstepNameTextBox.Text; workstep.Description = workstepDescriptionTextBox.Text; foreach (var part in process.Parts) { if (part.ToString().Equals(refPartComboBox.Text)) { workstep.ReferencePart = part; break; } } } public void UpdateProcess(Process process, Reason reason, IList allProcesses) { } public void Settings2UI() { } public void UI2Settings() { } public void ResetUnsavedFlag() { unsavedChanges = false; } public void SetUnsavedFlag() { if (!suppressNotifications) { unsavedChanges = true; wstepsCtrl.SetUnsavedFlag(); } } public bool AddPart(Part part) { if (part == null) { return true; /// There was no part to add = OK } else if (part == workstep.ReferencePart) { MessageBox.Show(Strings.Selected_part_is_the_reference_part_in_this_workstep, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); return false; /// NOK } else if (workstep.Parts.Contains(part)) { MessageBox.Show(Strings.Selected_part_is_already_used_in_this_workstep, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Hand); return false; /// NOK } part.Workstep = workstep; workstep.Parts.Add(part); Data2UI(); return true; /// Part added = OK } /// /// Deletes a part from UI and Workstep entity. /// Returns the part to be deleted from the DB. /// /// Part to be deleted from the DB public Part RemovePart() { ListBox currentListBox; if (listBox1.SelectedIndices.Count == 1 && listBox2.SelectedIndices.Count == 0) { currentListBox = listBox1; } else if (listBox1.SelectedIndices.Count == 0 && listBox2.SelectedIndices.Count == 1) { currentListBox = listBox2; } else { return null; } int ix = currentListBox.SelectedIndex; Part partToBedeleted = currentListBox.Items[ix] as Part; currentListBox.Items.RemoveAt(ix); workstep.Parts.Remove(partToBedeleted); partToBedeleted.Workstep = null; return partToBedeleted; } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (suppressNotifications) return; if (listBox1.SelectedIndices.Count == 1) { listBox2.SelectedIndices.Clear(); } } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { if (suppressNotifications) return; if (listBox2.SelectedIndices.Count == 1) { listBox1.SelectedIndices.Clear(); } } private void workstepNameTextBox_TextChanged(object sender, EventArgs e) { if (suppressNotifications) return; tabPage.Text = workstepNameTextBox.Text; SetUnsavedFlag(); } private void workstepDescriptionTextBox_TextChanged(object sender, EventArgs e) { if (suppressNotifications) return; SetUnsavedFlag(); } private void refPartComboBox_TextChanged(object sender, EventArgs e) { if (suppressNotifications) return; SetUnsavedFlag(); } private void refPartComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (suppressNotifications) return; SetUnsavedFlag(); } } }