Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.AlgorithmHost/MappingView.cs @ 6624

Last change on this file since 6624 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.Collections;
10using HeuristicLab.MainForm.WindowsForms;
11using HeuristicLab.Common;
12using HeuristicLab.MainForm;
13
14namespace HeuristicLab.OKB.AlgorithmHost {
15
16  [ViewAttribute("Mapping View")]
17  [Content(typeof(Mapping), true)] 
18  public partial class MappingView : ContentView {
19   
20    public MappingView() {
21      InitializeComponent();
22    }
23
24    public new Mapping Content {
25      get { return (Mapping)base.Content; }
26      set { base.Content = value; }
27    }
28
29    protected override void RegisterContentEvents() {
30      base.RegisterContentEvents();
31      Content.Map.CollectionReset += Content_MapChanged;
32      Content.Map.ItemsAdded += Content_MapChanged;
33      Content.Map.ItemsRemoved += Content_MapChanged;
34      Content.Map.ItemsReplaced += Content_MapChanged;
35      Content.Values.CollectionReset += Content_ValuesChanged;
36      Content.Values.ItemsAdded += Content_ValuesChanged;
37      Content.Values.ItemsRemoved += Content_ValuesChanged;
38    }
39
40    protected override void DeregisterContentEvents() {
41      base.DeregisterContentEvents();
42      Content.Map.CollectionReset -= Content_MapChanged;
43      Content.Map.ItemsAdded -= Content_MapChanged;
44      Content.Map.ItemsRemoved -= Content_MapChanged;
45      Content.Map.ItemsReplaced -= Content_MapChanged;
46      Content.Values.CollectionReset -= Content_ValuesChanged;
47      Content.Values.ItemsAdded -= Content_ValuesChanged;
48      Content.Values.ItemsRemoved -= Content_ValuesChanged;
49    }
50
51    void Content_MapChanged(object sender, CollectionItemsChangedEventArgs<KeyValuePair<string, string>> e) {
52      mappingTable.BeginLoadData();
53      mappingTable.Clear();
54      if (Content != null) {
55        foreach (var mapping in Content.Map) {
56          mappingTable.Rows.Add(new object[] { mapping.Key, mapping.Value });
57        }
58      }
59      mappingTable.EndLoadData();
60      mappingTable.AcceptChanges();
61    }
62
63    void Content_ValuesChanged(object sender, CollectionItemsChangedEventArgs<string> e) {
64      ValueTable.BeginLoadData();
65      ValueTable.Clear();
66      if (Content != null) {
67        foreach (var value in Content.Values) {
68          ValueTable.Rows.Add(new object[] { value });
69        }
70      }
71      ValueTable.EndLoadData();
72      ValueTable.AcceptChanges();
73    }
74
75    protected override void OnContentChanged() {
76      base.OnContentChanged();
77      if (Content == null) {
78        mappingTable.Clear();
79        ValueTable.Clear();
80      } else {
81        Content_MapChanged(this, new CollectionItemsChangedEventArgs<KeyValuePair<string, string>>(Enumerable.Empty<KeyValuePair<string, string>>()));
82        Content_ValuesChanged(this, new CollectionItemsChangedEventArgs<string>(Enumerable.Empty<string>()));
83      }
84    }
85
86    private void mappingGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
87      mappingGridView.EndEdit();
88      if (e.ColumnIndex != valueColumn.Ordinal || e.FormattedValue == DBNull.Value)
89        return;
90      DataGridViewRow currentRow = mappingGridView.Rows[e.RowIndex];
91      foreach (DataGridViewRow row in mappingGridView.Rows) {
92        if (row.Index == e.RowIndex)
93          continue;
94        if (row.Cells[e.ColumnIndex].Value == e.FormattedValue) {
95          currentRow.ErrorText = "ERROR: Duplicate DB parameter!";
96          e.Cancel = true;
97          return;
98        }
99      }
100      if (currentRow.ErrorText == "" && ((string)e.FormattedValue).Contains('.')) {
101        currentRow.ErrorText = "WARNING: Compound parameter should not be directly mapped";
102      } else {
103        currentRow.ErrorText = "";
104      }
105    }
106
107    private void parameterMappingGridView_CellValidated(object sender, DataGridViewCellEventArgs e) {
108      if (e.ColumnIndex != valueColumn.Ordinal)
109        return;
110      mappingTable.AcceptChanges();
111      object value = mappingGridView[valueColumn.Ordinal, e.RowIndex].Value;
112      object key = mappingGridView[keyColumn.Ordinal, e.RowIndex].Value;
113      if (value != DBNull.Value)
114        Content.Map[(string)key] = (string)value;
115      else
116        Content.Map[(string)key] = null;
117    }
118
119    private void mappingGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) {
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.