369 lines
12 KiB
C#
369 lines
12 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
/// Author: Milan Hanajík
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows.Forms;
|
|
using log4net;
|
|
using Config.Entities;
|
|
using TBF.BenchControl;
|
|
using TBF.BenchControl.Generic;
|
|
using TBF.Resources;
|
|
using TBF.UiControls;
|
|
|
|
namespace TBF
|
|
{
|
|
public partial class ComponentsManagerDlg : Form, IParentOfListViewEx
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(ComponentsManagerDlg));
|
|
|
|
/// <summary>
|
|
/// List of components (=component configuration instances)
|
|
/// </summary>
|
|
IList<Component> cmpntEntities;
|
|
|
|
IList<Component> toBeDeletedEntities;
|
|
|
|
NHibernate.ISession session;
|
|
|
|
SelectComponentClassDlg selectComponentTypeDlg; /// Constructed once, the selection is kept between dialog usages
|
|
|
|
CfgUpdateFlags flags; /// Or-ed from particular Flags from ComponentParametersDlg
|
|
|
|
/// <summary>
|
|
/// ListViewEx columns
|
|
/// </summary>
|
|
enum Column
|
|
{
|
|
Name,
|
|
ClassName,
|
|
ParentName,
|
|
DebugMode,
|
|
Logging,
|
|
Parameters,
|
|
ColumnsCount,
|
|
}
|
|
|
|
/// Editors used by listViewEx
|
|
ComboBox debugCB;
|
|
ComboBox logCB;
|
|
|
|
bool unlocked;
|
|
|
|
public ComponentsManagerDlg()
|
|
{
|
|
toBeDeletedEntities = new List<Component>();
|
|
|
|
InitializeComponent();
|
|
|
|
flags = CfgUpdateFlags.None;
|
|
|
|
selectComponentTypeDlg = new SelectComponentClassDlg();
|
|
|
|
/// SharedDlgButtons configuration
|
|
sharedButtons.RequiredGroupMembership = Config.Grp.GID.Metrologists;
|
|
sharedButtons.OptionalButtons = SharedButtons.Buttons.Add |
|
|
SharedButtons.Buttons.Remove |
|
|
//SharedButtons.Buttons.Up |
|
|
//SharedButtons.Buttons.Down |
|
|
SharedButtons.Buttons.Edit;
|
|
sharedButtons.Unlocked += Unlocked;
|
|
sharedButtons.OKClicked += okButton_Click;
|
|
sharedButtons.CancelClicked += cancelButton_Click;
|
|
sharedButtons.AddClicked += addButton_Click;
|
|
sharedButtons.RemoveClicked += removeButton_Click;
|
|
//sharedButtons.UpClicked += upButton_Click;
|
|
//sharedButtons.DownClicked += downButton_Click;
|
|
sharedButtons.EditClicked += editButton_Click;
|
|
}
|
|
|
|
private void ComponentsManagerDlg_Load(object sender, EventArgs e)
|
|
{
|
|
TBF.LocalSettings ls = Program.LocalSettings;
|
|
Width = (ls.ComponentsDlgWidth > 0) ? ls.ComponentsDlgWidth : 850;
|
|
Height = (ls.ComponentsDlgHeight > 0) ? ls.ComponentsDlgHeight : 500;
|
|
Left = (ls.ComponentsDlgLeft != 0) ? ls.ComponentsDlgLeft : 200;
|
|
Top = (ls.ComponentsDlgTop != 0) ? ls.ComponentsDlgTop : 100;
|
|
|
|
Text = Strings.Test_Bench_Components_Configuration;
|
|
listViewEx.Columns.Add(Strings.Name, (ls.ComponentsColumnCount > 0) ? ls.ComponentsColumnWidths[0] : 80);
|
|
listViewEx.Columns.Add(Strings.Type, (ls.ComponentsColumnCount > 1) ? ls.ComponentsColumnWidths[1] : 120);
|
|
listViewEx.Columns.Add(Strings.Parent, (ls.ComponentsColumnCount > 2) ? ls.ComponentsColumnWidths[2] : 55);
|
|
listViewEx.Columns.Add(Strings.Mode, (ls.ComponentsColumnCount > 3) ? ls.ComponentsColumnWidths[3] : 55);
|
|
listViewEx.Columns.Add(Strings.Logging, (ls.ComponentsColumnCount > 4) ? ls.ComponentsColumnWidths[4] : 55);
|
|
listViewEx.Columns.Add(Strings.Parameters, (ls.ComponentsColumnCount > 5) ? ls.ComponentsColumnWidths[5] : 600);
|
|
|
|
debugCB = new ComboBox();
|
|
for (DebugMode level = 0; level < DebugMode.Count; level++) debugCB.Items.Add(level.ToString());
|
|
splitContainer.Panel1.Controls.Add(debugCB);
|
|
|
|
logCB = new ComboBox();
|
|
for (LogLevel level = 0; level < LogLevel.Count; level++) logCB.Items.Add(level.ToString());
|
|
splitContainer.Panel1.Controls.Add(logCB);
|
|
|
|
listViewEx.SubItemClicked += new Results.Forms.SubItemEventHandler(listViewEx_SubItemClicked);
|
|
listViewEx.SubItemEndEditing += new Results.Forms.SubItemEndEditingEventHandler(listViewEx_SubItemEndEditing);
|
|
|
|
sharedButtons.DisableButtons(UiControls.SharedButtons.Buttons.Remove | UiControls.SharedButtons.Buttons.Edit);
|
|
|
|
session = Config.FluentCommon.CreateSession(Config.Entities.DBKind.Config);
|
|
cmpntEntities = session
|
|
.CreateQuery("FROM Component ORDER BY ItemNr")
|
|
.List<Component>();
|
|
|
|
RedrawAll();
|
|
}
|
|
|
|
void listViewEx_SubItemClicked(object sender, Results.Forms.SubItemEventArgs e)
|
|
{
|
|
if (unlocked && e.SubItem == (int)Column.DebugMode)
|
|
{
|
|
listViewEx.StartEditing(debugCB, e.Item, e.SubItem);
|
|
}
|
|
else if (unlocked && e.SubItem == (int)Column.Logging)
|
|
{
|
|
listViewEx.StartEditing(logCB, e.Item, e.SubItem);
|
|
}
|
|
}
|
|
|
|
void listViewEx_SubItemEndEditing(object sender, Results.Forms.SubItemEndEditingEventArgs e)
|
|
{
|
|
Component cmpnt = (Component)e.Item.Tag;
|
|
|
|
if (e.SubItem == (int)Column.DebugMode)
|
|
{
|
|
for (DebugMode level = 0; level < DebugMode.Count; level++)
|
|
{
|
|
if (debugCB.Text.Equals(level.ToString()))
|
|
{
|
|
cmpnt.Mode = level;
|
|
flags |= CfgUpdateFlags.RestartRqrd;
|
|
return;
|
|
}
|
|
}
|
|
e.DisplayText = cmpnt.Mode.ToString();
|
|
}
|
|
else if (e.SubItem == (int)Column.Logging)
|
|
{
|
|
for (LogLevel level = 0; level < LogLevel.Count; level++)
|
|
{
|
|
if (logCB.Text.Equals(level.ToString()))
|
|
{
|
|
cmpnt.Logging = level;
|
|
flags |= CfgUpdateFlags.RestartRqrd;
|
|
return;
|
|
}
|
|
}
|
|
e.DisplayText = cmpnt.Logging.ToString();
|
|
}
|
|
}
|
|
|
|
private void Unlocked(object sender, EventArgs e)
|
|
{
|
|
unlocked = true;
|
|
}
|
|
|
|
void RedrawAll()
|
|
{
|
|
listViewEx.Items.Clear();
|
|
foreach (var cmpnt in cmpntEntities) DrawOne(cmpnt);
|
|
}
|
|
|
|
void DrawOne(Component cmpnt)
|
|
{
|
|
IComponentCfg cmpCfg = TbfComponents.CmpntCfgFromCmpntEntity(cmpnt);
|
|
|
|
if (cmpCfg != null)
|
|
{
|
|
ListViewItem lvi = new ListViewItem(cmpCfg.Name);
|
|
lvi.SubItems.Add(cmpCfg.Factory.ClassName);
|
|
lvi.SubItems.Add(cmpCfg.ParentName);
|
|
lvi.SubItems.Add(cmpCfg.DebugLevel.ToString());
|
|
lvi.SubItems.Add(cmpCfg.LogLevel.ToString());
|
|
lvi.SubItems.Add(cmpCfg.ToString(1));
|
|
|
|
lvi.Tag = cmpnt;
|
|
|
|
listViewEx.Items.Add(lvi);
|
|
}
|
|
}
|
|
|
|
/// OK
|
|
private void okButton_Click(object sender, EventArgs e)
|
|
{
|
|
if ((flags & CfgUpdateFlags.AnyChange) != 0 || (flags & CfgUpdateFlags.RestartRqrd) != 0)
|
|
{
|
|
foreach (var cmpnt in toBeDeletedEntities)
|
|
{
|
|
Config.FluentCommon.DeleteFromDb(session, cmpnt);
|
|
}
|
|
foreach (var cmpnt in cmpntEntities)
|
|
{
|
|
Config.FluentCommon.SaveToDb(session, cmpnt);
|
|
}
|
|
|
|
Program.LocalSettings.ComponentsDlgLeft = Location.X;
|
|
Program.LocalSettings.ComponentsDlgTop = Location.Y;
|
|
Program.LocalSettings.ComponentsDlgWidth = Size.Width;
|
|
Program.LocalSettings.ComponentsDlgHeight = Size.Height;
|
|
Program.LocalSettings.Save();
|
|
}
|
|
|
|
/// Update the column widths
|
|
Program.LocalSettings.ComponentsColumnWidths = new int[(int)Column.ColumnsCount];
|
|
for (int i = 0; i < (int)Column.ColumnsCount; i++)
|
|
{
|
|
Program.LocalSettings.ComponentsColumnWidths[i] = listViewEx.Columns[i].Width;
|
|
}
|
|
Program.LocalSettings.Save();
|
|
|
|
DialogResult = DialogResult.OK;
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
/// Cancel
|
|
private void cancelButton_Click(object sender, EventArgs e)
|
|
{
|
|
Program.LocalSettings.ComponentsDlgLeft = Location.X;
|
|
Program.LocalSettings.ComponentsDlgTop = Location.Y;
|
|
Program.LocalSettings.ComponentsDlgWidth = Size.Width;
|
|
Program.LocalSettings.ComponentsDlgHeight = Size.Height;
|
|
Program.LocalSettings.Save();
|
|
|
|
if ((flags & CfgUpdateFlags.AnyChange) != 0 || (flags & CfgUpdateFlags.RestartRqrd) != 0)
|
|
{
|
|
DialogResult dr = MessageBox.Show(Strings.Changes_will_be_lost_Do_you_want_to_proceed,
|
|
Strings.Warning,
|
|
MessageBoxButtons.YesNo,
|
|
MessageBoxIcon.Question);
|
|
if (dr != DialogResult.Yes) return;
|
|
}
|
|
|
|
DialogResult = DialogResult.Cancel;
|
|
Close();
|
|
return;
|
|
}
|
|
|
|
/// Add a new component
|
|
private void addButton_Click(object sender, EventArgs e)
|
|
{
|
|
DialogResult dr = selectComponentTypeDlg.ShowDialog();
|
|
if (dr != DialogResult.OK) return;
|
|
|
|
IComponentFactory factory = selectComponentTypeDlg.SlctdCmpntFactory;
|
|
IComponentCfg cfg = factory.DefaultConfig();
|
|
IComponentCfgCtrl cfgControl = cfg.GetControl();
|
|
cfgControl.Config = cfg;
|
|
cfgControl.Config.ItemNr = listViewEx.Items.Count;
|
|
|
|
ComponentParametersDlg cfgForm = new ComponentParametersDlg();
|
|
cfgForm.TbfComponents = cmpntEntities;
|
|
cfgForm.ComponentCfgCtrl = cfgControl;
|
|
cfgForm.UnlockAfterStart = true;
|
|
dr = cfgForm.ShowDialog();
|
|
if (dr != DialogResult.OK) return;
|
|
|
|
flags |= CfgUpdateFlags.RestartRqrd;
|
|
AddOne(cfgForm.Config.CreateDbEntity());
|
|
}
|
|
|
|
void AddOne(Component cmpnt)
|
|
{
|
|
cmpntEntities.Add(cmpnt);
|
|
DrawOne(cmpnt);
|
|
}
|
|
|
|
/// Delete the component
|
|
private void removeButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (listViewEx.SelectedIndices.Count != 1) return;
|
|
int ix = listViewEx.SelectedIndices[0];
|
|
if (ix < 0) return;
|
|
|
|
Component selectedCmpnt = (Component)listViewEx.Items[ix].Tag;
|
|
if (selectedCmpnt.Id != 0) toBeDeletedEntities.Add(selectedCmpnt);
|
|
listViewEx.Items.RemoveAt(ix);
|
|
RedrawAll();
|
|
}
|
|
|
|
/// DoubleClick -> Edit the component
|
|
private void listViewEx_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
{
|
|
editButton_Click(sender, e);
|
|
}
|
|
|
|
/// Edit the component
|
|
private void editButton_Click(object sender, EventArgs e)
|
|
{
|
|
if (listViewEx.SelectedIndices.Count != 1) return;
|
|
ListViewItem lvi = listViewEx.SelectedItems[0];
|
|
|
|
ComponentParametersDlg cfgForm = new ComponentParametersDlg();
|
|
cfgForm.TbfComponents = cmpntEntities;
|
|
IComponentCfg cfg = TbfComponents.CmpntCfgFromCmpntEntity((Component)lvi.Tag);
|
|
cfgForm.ComponentCfgCtrl = cfg.GetControl();
|
|
cfgForm.ComponentCfgCtrl.Config = cfg;
|
|
DialogResult dr = cfgForm.ShowDialog();
|
|
if (dr != DialogResult.OK) return;
|
|
|
|
flags |= cfgForm.Flags;
|
|
|
|
sharedButtons.UnlockButtons(); /// Unlock this dialog buttons as changes were enabled
|
|
unlocked = true; /// in the ComponentParametersDlg.
|
|
|
|
Component modified = cfgForm.Config.CreateDbEntity();
|
|
Component original = (Component)lvi.Tag;
|
|
|
|
original.Name = modified.Name;
|
|
original.ClassName = modified.ClassName;
|
|
original.ParentName = modified.ParentName;
|
|
original.Mode = modified.Mode;
|
|
original.Logging = modified.Logging;
|
|
original.Parameters = modified.Parameters;
|
|
|
|
RedrawAll();
|
|
}
|
|
|
|
private void componentsListView_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
if (listViewEx.SelectedIndices.Count == 1)
|
|
{
|
|
sharedButtons.EnableButtons(UiControls.SharedButtons.Buttons.Remove | UiControls.SharedButtons.Buttons.Edit);
|
|
}
|
|
else
|
|
{
|
|
sharedButtons.DisableButtons(UiControls.SharedButtons.Buttons.Remove | UiControls.SharedButtons.Buttons.Edit);
|
|
}
|
|
}
|
|
|
|
public void UpdateButtonStates(SharedButtons.SelectedItemPos selectedItemPos)
|
|
{
|
|
if (selectedItemPos == SharedButtons.SelectedItemPos.None)
|
|
{
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.First)
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Down);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Up);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.Last)
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Down);
|
|
}
|
|
else if (selectedItemPos == SharedButtons.SelectedItemPos.FirstAndLast)
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Remove);
|
|
sharedButtons.DisableButtons(SharedButtons.Buttons.Up | SharedButtons.Buttons.Down);
|
|
}
|
|
else
|
|
{
|
|
sharedButtons.EnableButtons(SharedButtons.Buttons.Remove | SharedButtons.Buttons.Up | SharedButtons.Buttons.Down);
|
|
}
|
|
}
|
|
}
|
|
}
|