SourceCode/PROMS/Volian.Controls.Library/frmEPAnnotationDetails.cs
mschill 3e53ec9191 C2025-023 - Electronic Procedures - Modifications to PROMS
Small change to handle removed items that were selected.
2025-05-13 07:48:50 -04:00

334 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VEPROMS.CSLA.Library;
using Volian.Controls.Library;
using System.Linq;
namespace Volian.Controls.Library
{
//C2025-023 - Electronic Procedures - Modifications to PROMS
//Form for Dynamic Showing of EP detail controls and entry
public partial class frmEPAnnotationDetails : Form
{
private AnnotationInfo _CurrentAnnotation;
private EPFields myEPFields;
private AnnotationConfig MyConfig;
private StepTabRibbon _MyStepTabRibbon;
private Dictionary<string, StepRTB> _DicStepRtb;
private Dictionary<string, CheckBox> _DicCheckBox;
private Dictionary<string, ComboBox> _DicComboBox;
private Dictionary<string, ComboBox> _DicSingleRO;
private Dictionary<string, ListBoxMulti> _DicMultiRO;
private TablePropertiesControl _TablePropControl;
private string multiseparator = ",";
public frmEPAnnotationDetails(AnnotationInfo currAnn)
{
InitializeComponent();
_DicStepRtb = new Dictionary<string, StepRTB>();
_DicCheckBox = new Dictionary<string, CheckBox>();
_DicComboBox = new Dictionary<string, ComboBox>();
_DicSingleRO = new Dictionary<string, ComboBox>();
_DicMultiRO = new Dictionary<string, ListBoxMulti>();
InitializeSpecificControls(currAnn);
_MyStepTabRibbon = new StepTabRibbon();
}
private void InitializeSpecificControls(AnnotationInfo currAnn)
{
_CurrentAnnotation = currAnn;
myEPFields = _CurrentAnnotation.MyItem.GetValidEPFields(_CurrentAnnotation.TypeID);
MyConfig = new AnnotationConfig(currAnn.Config);
int MaxCharsInLabel = myEPFields.Where(y => y.type.ToLower() != "tableinput").Max(x => x.label.Length);
//font size 8 - make labels slightly bigger than largest label
panelEP.ColumnStyles[0].SizeType = SizeType.Absolute;
panelEP.ColumnStyles[0].Width = (8 * MaxCharsInLabel) + 5;
int RowCount = 0;
foreach (EPField EP in myEPFields)
{
RowCount += 1;
panelEP.RowCount = RowCount;
panelEP.Top = 20;
if (EP.type.ToLower() != "tableinput")
{
Label wlbl = new Label();
wlbl.Text = EP.label;
wlbl.Visible = true;
wlbl.TextAlign = ContentAlignment.MiddleLeft;
wlbl.Anchor = AnchorStyles.Left | AnchorStyles.Top;
wlbl.Width = (8 * MaxCharsInLabel) + 5;
panelEP.Controls.Add(wlbl, 0, panelEP.RowCount - 1);
}
if (EP.type.ToLower() == "text")
{
StepRTB tb = new StepRTB();
tb.Font = tb.FormatFont = new Font("Arial", 8);
tb.FieldToEdit = E_FieldToEdit.PSI; //use same right click menu as PSI
tb.BorderStyle = BorderStyle.FixedSingle;
//aligning to both left and right stretches the textbox to the width of the form
tb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
tb.Enter += new System.EventHandler(this.FieldStepRTB_Enter);
string val = MyConfig.GetValue("EP", EP.name).Replace("\\u8209?", "-");
DisplayText dt = new DisplayText(val, new VE_Font("Arial", 10, E_Style.None, 12), false);
StringBuilder sb = new StringBuilder();
sb.Append(@"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset2 Arial;}");
sb.Append(@"{\f1\fnil\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;}"); //C2017-036 changed to just Arial because Microsoft removed Arial Unicode MS with Word16
sb.Append(@"\viewkind4\uc1\pard\sl-240\slmult0\fs" + (int)(this.Font.SizeInPoints * 2) + " " + dt.StartText + @"}");
tb.Rtf = sb.ToString();
tb.Visible = true;
tb.Height = EP.numlines * tb.Font.Height + 1 + tb.Margin.Vertical;
tb.MinimumSize = new Size(0, EP.numlines * tb.Font.Height + 1 + tb.Margin.Vertical);
if (EP.numlines > 1)
{
tb.Multiline = true;
tb.ScrollBars = RichTextBoxScrollBars.Both;
panelEP.RowStyles.Insert(panelEP.RowCount - 1, new RowStyle(SizeType.Absolute, 50));
}
else
{
panelEP.RowStyles.Insert(panelEP.RowCount - 1, new RowStyle(SizeType.AutoSize));
}
_DicStepRtb.Add(EP.name, tb);
panelEP.Controls.Add(tb, 1, panelEP.RowCount - 1);
}
else
{
panelEP.RowStyles.Insert(panelEP.RowCount - 1, new RowStyle(SizeType.AutoSize));
}
if (EP.type.ToLower() == "logical")
{
CheckBox cb = new CheckBox();
cb.Text = EP.text;
cb.Visible = true;
string val = MyConfig.GetValue("EP", EP.name);
cb.Checked = val != null && val != "" && val.ToUpper()[0] == 'Y';
_DicCheckBox.Add(EP.name, cb);
cb.AutoSize = true;
panelEP.Controls.Add(cb, 1, panelEP.RowCount - 1);
}
if (EP.type.ToLower() == "combo")
{
ComboBox cmb = new ComboBox();
cmb.Visible = true;
string tmp = EP.text;
string[] tmps = tmp.Split(",".ToCharArray());
foreach (string t in tmps) cmb.Items.Add(t.Trim());
string val = MyConfig.GetValue("EP", EP.name);
if (val != null && val != "") cmb.SelectedItem = val;
cmb.DropDownWidth = TextRenderer.MeasureText(tmps.OrderByDescending(x => x.Length).First(), cmb.Font).Width + SystemInformation.VerticalScrollBarWidth;
cmb.Width = cmb.DropDownWidth;
_DicComboBox.Add(EP.name, cmb);
panelEP.Controls.Add(cmb, 1, panelEP.RowCount - 1);
}
if (EP.type.ToLower() == "rosingle")
{
ComboBox cmb = new ComboBox();
cmb.Visible = true;
List<ROListItem> tmps = EP.getROList(currAnn, true);
cmb.DisplayMember = "Text";
cmb.ValueMember = "Value";
cmb.DataSource = tmps;
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
cmb.DropDownWidth = TextRenderer.MeasureText(tmps.OrderByDescending(x => x.Text.Length).First().Text, cmb.Font).Width + SystemInformation.VerticalScrollBarWidth;
cmb.Width = cmb.DropDownWidth;
_DicSingleRO.Add(EP.name, cmb);
panelEP.Controls.Add(cmb, 1, panelEP.RowCount - 1);
}
if (EP.type.ToLower() == "romulti")
{
ListBoxMulti lb = new ListBoxMulti();
List<ROListItem> tmps = EP.getROList(currAnn, false);
lb.DisplayMember = "Text";
lb.ValueMember = "Value";
lb.Width = TextRenderer.MeasureText(tmps.OrderByDescending(x => x.Text.Length).First().Text, lb.Font).Width + SystemInformation.VerticalScrollBarWidth;
lb.DataSource = tmps;
_DicMultiRO.Add(EP.name, lb);
panelEP.Controls.Add(lb, 1, panelEP.RowCount - 1);
}
//note will allow only 1 tableproperties control since it is a 1:1 match with the table that is in the step
if (EP.type.ToLower() == "tableinput" && _TablePropControl == null)
{
string val = MyConfig.GetValue("EP", EP.name);
_TablePropControl = new TablePropertiesControl(EP.name, EP.label, val);
_TablePropControl.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
panelEP.Controls.Add(_TablePropControl, 1, panelEP.RowCount - 1);
}
}
Load += new EventHandler(Form1Load_setDefaults);
}
void Form1Load_setDefaults(object sender, EventArgs e)
{
foreach (KeyValuePair<string, ComboBox> pair in _DicSingleRO)
{
string val = MyConfig.GetValue("EP", pair.Key);
if (!string.IsNullOrEmpty(val) && pair.Value.Items.OfType<ROListItem>().Any(x=> x.Value == val))
pair.Value.SelectedValue = val;
else
pair.Value.SelectedValue = "";
}
foreach (KeyValuePair<string, ListBoxMulti> pair in _DicMultiRO)
{
//clear all items at start in case items were autoselected - bug in Winforms ListBox
pair.Value.ClearSelected();
string val = MyConfig.GetValue("EP", pair.Key);
if (val != null && val != "")
{
var selectedvalues = val.Split(multiseparator.ToCharArray());
foreach (string item in selectedvalues)
{
string text = ((List<ROListItem>)pair.Value.DataSource).Find(x => x.Value == item)?.Text;
if (!string.IsNullOrEmpty(text)) pair.Value.SetSelected(pair.Value.FindString(text), true);
}
}
//set this to -1 after initial setting of values
//this will help to fix bug in Winforms ListBox
//that autoselects first item when no items are selected
pair.Value.singleselectedindex = -1;
}
}
private void FieldStepRTB_Enter(object sender, EventArgs e)
{
_MyStepTabRibbon.MyStepRTB = (StepRTB)sender;
}
private void btnOk_Click(object sender, EventArgs e)
{
// loop through all of the fields
bool isDirty = false;
foreach (EPField EP in myEPFields)
{
if (EP.type.ToLower() == "text")
{
StepRTB cur = _DicStepRtb[EP.name];
string rtf = cur.Rtf;
string newval = DisplayText.StaticStripRtfCommands(rtf, false);
string oldval = MyConfig.GetValue("EP", EP.name);
if (oldval != newval)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newval.Replace("\\u8209?","-"));
}
}
else if (EP.type.ToLower() == "logical")
{
CheckBox cur = _DicCheckBox[EP.name];
string newval = cur.Checked ? "Y" : "N";
string oldval = MyConfig.GetValue("EP", EP.name);
if (newval != oldval)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newval);
}
}
else if (EP.type.ToLower() == "combo")
{
ComboBox cmbcur = _DicComboBox[EP.name];
string newval = cmbcur.Text;
string oldval = MyConfig.GetValue("EP", EP.name);
if (newval != oldval)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newval);
}
}
else if (EP.type.ToLower() == "rosingle")
{
ComboBox cmbcur = _DicSingleRO[EP.name];
string newval = cmbcur.SelectedValue.ToString();
string oldval = MyConfig.GetValue("EP", EP.name);
if (newval == "-1") newval = "";
if (newval != oldval)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newval);
}
}
else if (EP.type.ToLower() == "romulti")
{
ListBoxMulti lbcur = _DicMultiRO[EP.name];
string newvalues = String.Join(multiseparator, lbcur.SelectedItems.OfType<ROListItem>().Select(item => item.Value));
string oldvalues = MyConfig.GetValue("EP", EP.name);
if (newvalues != oldvalues)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newvalues);
}
}
else if (EP.type.ToLower() == "tableinput" && EP.name == _TablePropControl.epname)
{
string newvalues = _TablePropControl.GetStorageValue();
string oldvalues = MyConfig.GetValue("EP", EP.name);
if (newvalues != oldvalues)
{
isDirty = true;
MyConfig.SetValue("EP", EP.name, newvalues);
}
}
}
if (isDirty)
{
using (Annotation annotation = _CurrentAnnotation.Get())
{
if (annotation != null)
{
annotation.Config = MyConfig.ToString();
annotation.DTS = DateTime.Now;
annotation.UserID = Volian.Base.Library.VlnSettings.UserID;
annotation.Save();
}
}
}
DialogResult = DialogResult.OK;
Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
//remove events so no memory loss
private void frmEPAnnotationDetails_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (StepRTB tb in _DicStepRtb.Values)
{
tb.Enter -= FieldStepRTB_Enter;
}
foreach (ListBoxMulti lb in _DicMultiRO.Values)
{
lb.Dispose();
}
Load -= Form1Load_setDefaults;
}
}
}