using System; using System.Collections.Generic; using System.Windows.Forms; using Common; using Config.Entities; using log4net; using NHibernate; using TBF.Resources; using TBF.Rig.Generic; namespace TBF.Rig.Output.Printers.GroupPrinting.Single { public partial class PrinterTestBenchComponentSelectorDlg : Form { static readonly ILog log = LogManager.GetLogger(typeof(PrinterTestBenchComponentSelectorDlg)); /// /// List of components (=component configuration instances) /// IList cmpntEntities; public Component SelectedComponent { get; set; } ISession session; MySortOrder sortOrder = MySortOrder.Ascending; int sortColumn = -1; /// 0-based index of column to be used for sorting public PrinterTestBenchComponentSelectorDlg() { InitializeComponent(); } private void LoadFormPosition() { 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; } /// /// Save the dialog position and ListViewEx column widths /// void SaveUISettings() { /// Obtain ComponentsmanagerDlg dimensions, etc. bool isMaximized = (WindowState == FormWindowState.Maximized); int left = (WindowState == FormWindowState.Normal) ? Location.X : RestoreBounds.Left; int top = (WindowState == FormWindowState.Normal) ? Location.Y : RestoreBounds.Top; int width = (WindowState == FormWindowState.Normal) ? Size.Width : RestoreBounds.Width; int height = (WindowState == FormWindowState.Normal) ? Size.Height : RestoreBounds.Height; LocalSettings ls = Program.LocalSettings; /// Compare list view column widths with the saved ones bool anyColumnDiffers = false; // if ((int)Column.ColumnsCount != ls.ComponentsColumnCount) // { // anyColumnDiffers = true; // } // else // { // for (int i = 0; i < (int)Column.ColumnsCount; i++) // { // if (listViewEx.Columns[i].Width != ls.ComponentsColumnWidths[i]) { anyColumnDiffers = true; break; } // } // } if (ls != null && (ls.ComponentsDlgMaximized != isMaximized || ls.ComponentsDlgLeft != left || ls.ComponentsDlgTop != top || ls.ComponentsDlgWidth != width || ls.ComponentsDlgHeight != height || anyColumnDiffers)) { /// At least one ComponentsManagerDlg dimension differs => Update local settings and save them /// ls.ComponentsDlgMaximized = isMaximized; ls.ComponentsDlgLeft = left; ls.ComponentsDlgTop = top; ls.ComponentsDlgWidth = width; ls.ComponentsDlgHeight = height; /// List view column widths // ls.ComponentsColumnWidths = new int[(int)Column.ColumnsCount]; // for (int i = 0; i < (int)Column.ColumnsCount; i++) // { // ls.ComponentsColumnWidths[i] = listViewEx.Columns[i].Width; // } ls.Save(); } } private void btSelect_Click(object sender, EventArgs e) { if (listViewEx.SelectedItems.Count > 0) { SelectedComponent = cmpntEntities[listViewEx.SelectedIndices[0]]; this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("No Selection", "Selection", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void PrinterTestBenchComponentSelectorDlg_Load(object sender, EventArgs e) { LoadFormPosition(); LocalSettings ls = Program.LocalSettings; listViewEx.Columns.Add(Strings.Nr, (ls.ComponentsColumnCount > 0) ? ls.ComponentsColumnWidths[0] : 40); listViewEx.Columns.Add(Strings.Name, (ls.ComponentsColumnCount > 1) ? ls.ComponentsColumnWidths[1] : 80); listViewEx.Columns.Add(Strings.Type, (ls.ComponentsColumnCount > 2) ? ls.ComponentsColumnWidths[2] : 120); listViewEx.Columns.Add(Strings.Parent, (ls.ComponentsColumnCount > 3) ? ls.ComponentsColumnWidths[3] : 55); listViewEx.Columns.Add(Strings.Parameters, (ls.ComponentsColumnCount > 6) ? ls.ComponentsColumnWidths[6] : 600); listViewEx.HeaderStyle = ColumnHeaderStyle.Clickable; GetEntities(); RedrawAll(); } private void GetEntities() { session = TBF.DB.CreateSession(DBKind.Config); IList cmpntEntitiesAll = session.QueryOver() .OrderBy(x => x.ItemNr).Asc .List(); if (cmpntEntities == null) { cmpntEntities = new List(); } else { cmpntEntities.Clear(); } foreach (var component in cmpntEntitiesAll) { if (component.ClassName.StartsWith("Output.Printers.OnePerMeter")) { cmpntEntities.Add(component); } } } void RedrawAll() { listViewEx.Items.Clear(); foreach (var cmpnt in cmpntEntities) DrawOne(cmpnt); } void DrawOne(Component cmpnt) { ListViewItem lvi = new ListViewItem(cmpnt.ItemNr.ToString()); lvi.Tag = cmpnt; lvi.SubItems.Add(cmpnt.Name); lvi.SubItems.Add(string.IsNullOrEmpty(cmpnt.ClassName) ? string.Empty : cmpnt.ClassName); lvi.SubItems.Add(string.IsNullOrEmpty(cmpnt.Parent) ? string.Empty : cmpnt.Parent); IComponentCfg cmpCfg = TbfComponents.CmpntCfgFromCmpntEntity(cmpnt); if (cmpCfg != null) { lvi.SubItems.Add(cmpCfg.ToString(-1)); } else { lvi.SubItems.Add("Not a component"); } listViewEx.Items.Add(lvi); } private void btCancel_Click(object sender, EventArgs e) { SaveUISettings(); SelectedComponent = null; this.DialogResult = DialogResult.Cancel; this.Close(); } } }