/
PickListHandler.xaml.cs
207 lines (172 loc) · 6.65 KB
/
PickListHandler.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace MultiTermTBXMapper.Menu
{
/// <summary>
/// Interaction logic for PickListHandler.xaml
/// </summary>
public partial class PickListHandler : UserControl, ISwitchable
{
private MappingDict mapping = new MappingDict();
private List<string> dcs_with_picklists = new List<string>();
private Dictionary<string, bool> mappedPicklists = new Dictionary<string,bool>();
private Dictionary<string, List<string[]>> tbx_picklists = TBXDatabase.getPicklists();
private int index = 0;
#region ISwitchable members
public void UtilizeState<T>(T state)
{
throw new NotImplementedException();
}
public void UtilizeState<T>(ref T r)
{
this.mapping = r as MappingDict;
dcs_with_picklists = mapping.getDCsWithPicklists();
fillMappedPicklistsDict();
display();
}
public void UtilizeState<T1, T2>(ref T1 r, T2 state)
{
throw new NotImplementedException();
}
#endregion
public PickListHandler()
{
InitializeComponent();
}
private void display()
{
clearGrid();
fillGrid();
textblock_head.Text = "Picklists for " + dcs_with_picklists[index].ToString();
editHead();
}
private void editHead()
{
string headText = "Picklist values for " + dcs_with_picklists[index];
textblock_head.Text = headText;
}
private void clearGrid()
{
canvas_pls.Children.RemoveRange(0, canvas_pls.Children.Count);
}
/// <summary>
/// Fills the mappedPicklists Dictionary with a key that is the combination of a data category and a content, and a boolean value.
/// <para>
/// Any content values which are mapped to TBX data categories which do not have picklists will be skipped.
/// </para>
/// <para>
/// <code>{ 'part of speech_noun' : false }</code>
/// </para>
/// <para>
/// This means that the content "noun" of "part of speech" has not been mapped to a TBX picklist value yet.
/// </para>
/// </summary>
private void fillMappedPicklistsDict()
{
dcs_with_picklists.ForEach(delegate (string dc)
{
foreach (string val in mapping.getContentList(dc))
{
string[] keys = Methods.getKeyArray(tbx_picklists.Keys);
if (mapping.getTBXMappingList(dc).Count < 2 || Methods.inArray(ref keys, mapping.getTBXContentMap(dc)?.Get(val)))
{
try
{
mappedPicklists.Add(dc + "_" + val, false);
}
catch (System.ArgumentException e)
{
continue;
}
}
}
});
}
private void fillGrid()
{
Grid grid = new Grid();
grid.HorizontalAlignment = HorizontalAlignment.Stretch;
grid.VerticalAlignment = VerticalAlignment.Stretch;
canvas_pls.Children.Add(grid);
//Cycle through each Picklist
mapping.getContentList(dcs_with_picklists[index])?.ForEach(delegate (string content)
{
string[] keys = Methods.getKeyArray(tbx_picklists.Keys);
if (mapping.getTBXMappingList(dcs_with_picklists[index]).Count < 2 || Methods.inArray(ref keys, mapping.getTBXContentMap(dcs_with_picklists[index])?.Get(content)))
{
createPicklistMapControl(dcs_with_picklists[index], content, ref grid);
}
});
}
private void createPicklistMapControl(string user_dc, string pl_content, ref Grid grid)
{
PickListMapControl plmc = new PickListMapControl(pl_content);
plmc.select += value => setMapping(value[0], value[1]);
string tbx_dc = mapping.getTBXContentMap(user_dc)?.Get(pl_content);
if (tbx_dc == null)
{
tbx_dc = mapping.getTBXMappingList(user_dc)[0];
mapping.getTBXContentMap(user_dc).Add(pl_content, tbx_dc);
}
string tbx_selected = mapping.getPicklistMapValue(user_dc, pl_content);
fillTBXComboBox(ref plmc.combo_tbx_picklist, mapping.getTBXContentMap(user_dc)?.Get(pl_content), tbx_selected);
grid.Children.Add(plmc);
RowDefinition rd = new RowDefinition();
rd.Height = new GridLength(30);
grid.RowDefinitions.Add(rd);
plmc.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1);
}
private void fillTBXComboBox(ref ComboBox box, string tbx_dc_key, string tbx_selected = null)
{
for (int i = 0; i < tbx_picklists[tbx_dc_key]?.Count; i++)
{
ComboBoxItem item = new ComboBoxItem();
item.Content = tbx_picklists[tbx_dc_key][i][0];
if (tbx_selected == tbx_picklists[tbx_dc_key][i][0])
{
item.IsSelected = true;
}
box.Items.Add(item);
}
}
private void setMapping(string user_pl, string tbx_pl)
{
string dc = dcs_with_picklists[index];
mapping.setPicklistMap(dc, user_pl, tbx_pl);
mappedPicklists[dc + "_" + user_pl] = true;
checkCompletion();
}
private void checkCompletion()
{
foreach (bool val in mappedPicklists.Values)
{
if (!val)
{
submit.IsEnabled = false;
return;
}
}
submit.IsEnabled = true;
}
public void UtilizeState(object state)
{
throw new NotImplementedException();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Methods.incrementIndex(ref index, dcs_with_picklists.Count);
display();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Methods.decrementIndex(ref index);
display();
}
private void submit_Click(object sender, RoutedEventArgs e)
{
Switcher.Switch(new QueueDrainHandler(), ref mapping);
}
}
}