using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.Collections; using HeuristicLab.MainForm.WindowsForms; using HeuristicLab.Common; using HeuristicLab.MainForm; namespace HeuristicLab.OKB.AlgorithmHost { [ViewAttribute("Mapping View")] [Content(typeof(Mapping), true)] public partial class MappingView : ContentView { public MappingView() { InitializeComponent(); } public new Mapping Content { get { return (Mapping)base.Content; } set { base.Content = value; } } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.Map.CollectionReset += Content_MapChanged; Content.Map.ItemsAdded += Content_MapChanged; Content.Map.ItemsRemoved += Content_MapChanged; Content.Map.ItemsReplaced += Content_MapChanged; Content.Values.CollectionReset += Content_ValuesChanged; Content.Values.ItemsAdded += Content_ValuesChanged; Content.Values.ItemsRemoved += Content_ValuesChanged; } protected override void DeregisterContentEvents() { base.DeregisterContentEvents(); Content.Map.CollectionReset -= Content_MapChanged; Content.Map.ItemsAdded -= Content_MapChanged; Content.Map.ItemsRemoved -= Content_MapChanged; Content.Map.ItemsReplaced -= Content_MapChanged; Content.Values.CollectionReset -= Content_ValuesChanged; Content.Values.ItemsAdded -= Content_ValuesChanged; Content.Values.ItemsRemoved -= Content_ValuesChanged; } void Content_MapChanged(object sender, CollectionItemsChangedEventArgs> e) { mappingTable.BeginLoadData(); mappingTable.Clear(); if (Content != null) { foreach (var mapping in Content.Map) { mappingTable.Rows.Add(new object[] { mapping.Key, mapping.Value }); } } mappingTable.EndLoadData(); mappingTable.AcceptChanges(); } void Content_ValuesChanged(object sender, CollectionItemsChangedEventArgs e) { ValueTable.BeginLoadData(); ValueTable.Clear(); if (Content != null) { foreach (var value in Content.Values) { ValueTable.Rows.Add(new object[] { value }); } } ValueTable.EndLoadData(); ValueTable.AcceptChanges(); } protected override void OnContentChanged() { base.OnContentChanged(); if (Content == null) { mappingTable.Clear(); ValueTable.Clear(); } else { Content_MapChanged(this, new CollectionItemsChangedEventArgs>(Enumerable.Empty>())); Content_ValuesChanged(this, new CollectionItemsChangedEventArgs(Enumerable.Empty())); } } private void mappingGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { mappingGridView.EndEdit(); if (e.ColumnIndex != valueColumn.Ordinal || e.FormattedValue == DBNull.Value) return; DataGridViewRow currentRow = mappingGridView.Rows[e.RowIndex]; foreach (DataGridViewRow row in mappingGridView.Rows) { if (row.Index == e.RowIndex) continue; if (row.Cells[e.ColumnIndex].Value == e.FormattedValue) { currentRow.ErrorText = "ERROR: Duplicate DB parameter!"; e.Cancel = true; return; } } if (currentRow.ErrorText == "" && ((string)e.FormattedValue).Contains('.')) { currentRow.ErrorText = "WARNING: Compound parameter should not be directly mapped"; } else { currentRow.ErrorText = ""; } } private void parameterMappingGridView_CellValidated(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != valueColumn.Ordinal) return; mappingTable.AcceptChanges(); object value = mappingGridView[valueColumn.Ordinal, e.RowIndex].Value; object key = mappingGridView[keyColumn.Ordinal, e.RowIndex].Value; if (value != DBNull.Value) Content.Map[(string)key] = (string)value; else Content.Map[(string)key] = null; } private void mappingGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { } } }