-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathParserParametersDlg.cs
More file actions
374 lines (340 loc) · 12.9 KB
/
Copy pathParserParametersDlg.cs
File metadata and controls
374 lines (340 loc) · 12.9 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) 2003-2025 SIL International
// This software is licensed under the LGPL, version 2.1 or later
// (http://www.gnu.org/licenses/lgpl-2.1.html)
//
// <remarks>
// Implementation of:
// ParserParametersDlg - Dialog for editing XML representation of parser parameters
// (MoMorphData : ParserParameters)
// </remarks>
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Data;
using System.Xml.Linq;
using SIL.FieldWorks.Common.FwUtils;
using SIL.LCModel;
namespace SIL.FieldWorks.LexText.Controls
{
/// <summary>
/// Summary description for ParserParametersDlg.
/// </summary>
public class ParserParametersDlg : ParserParametersBase
{
private const string HelpTopic = "khtpParserParamters";
private const string HC = "HC";
private const string DelReapps = "DelReapps";
private const string NotOnClitics = "NotOnClitics";
private const string NoDefaultCompounding = "NoDefaultCompounding";
private const string AcceptUnspecifiedGraphemes = "AcceptUnspecifiedGraphemes";
private const string GuessRoots = "GuessRoots";
private const string MergeAnalyses = "MergeAnalyses";
private const string Strata = "Strata";
private const string XAmple = "XAmple";
private const string MaxNulls = "MaxNulls";
private const string MaxPrefixes = "MaxPrefixes";
private const string MaxSuffixes = "MaxSuffixes";
private const string MaxInfixes = "MaxInfixes";
private const string MaxInterfixes = "MaxInterfixes";
private const string MaxRoots = "MaxRoots";
private const string MaxAnalysesToReturn = "MaxAnalysesToReturn";
#region Data members
private readonly IHelpTopicProvider m_helpTopicProvider;
private Label m_label1;
private Label m_label2;
private Button m_btnHCMaxCompoundRuleApps;
private Button m_btnOk;
private Button m_btnCancel;
private Button m_btnHelp;
private DataGrid m_dataGrid1;
private DataGrid m_dataGrid2;
private Label m_label3;
private DataSet m_dsParserParameters;
private ILcmOwningSequence<IMoCompoundRule> m_compoundRules;
#endregion Data members
private ParserParametersDlg()
{
InitializeComponent();
AccessibleName = GetType().Name;
}
public ParserParametersDlg(IHelpTopicProvider helpTopicProvider) : this()
{
m_helpTopicProvider = helpTopicProvider;
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
Debug.WriteLineIf(!disposing, "****** Missing Dispose() call for " + GetType().Name + ". ****** ");
// Must not be run more than once.
if (IsDisposed)
return;
if (disposing)
{
if (m_dsParserParameters != null)
{
m_dsParserParameters.Dispose();
m_dsParserParameters = null;
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ParserParametersDlg));
this.m_label1 = new System.Windows.Forms.Label();
this.m_label2 = new System.Windows.Forms.Label();
this.m_btnHCMaxCompoundRuleApps = new System.Windows.Forms.Button();
this.m_btnOk = new System.Windows.Forms.Button();
this.m_btnCancel = new System.Windows.Forms.Button();
this.m_dataGrid1 = new System.Windows.Forms.DataGrid();
this.m_btnHelp = new System.Windows.Forms.Button();
this.m_dataGrid2 = new System.Windows.Forms.DataGrid();
this.m_label3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.m_dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.m_dataGrid2)).BeginInit();
this.SuspendLayout();
//
// label1
//
resources.ApplyResources(this.m_label1, "m_label1");
this.m_label1.Name = "m_label1";
//
// label2
//
resources.ApplyResources(this.m_label2, "m_label2");
this.m_label2.Name = "m_label2";
//
// btnHCMaxCompoundRuleApps
//
resources.ApplyResources(this.m_btnHCMaxCompoundRuleApps, "m_btnHCMaxCompoundRuleApps");
this.m_btnHCMaxCompoundRuleApps.Name = "m_btnHCMaxCompoundRuleApps";
this.m_btnHCMaxCompoundRuleApps.Click += new System.EventHandler(this.btnHCMaxCompoundRuleApps_Click);
//
// btnOK
//
this.m_btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
resources.ApplyResources(this.m_btnOk, "m_btnOk");
this.m_btnOk.Name = "m_btnOk";
this.m_btnOk.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.m_btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
resources.ApplyResources(this.m_btnCancel, "m_btnCancel");
this.m_btnCancel.Name = "m_btnCancel";
//
// dataGrid1
//
resources.ApplyResources(this.m_dataGrid1, "m_dataGrid1");
this.m_dataGrid1.DataMember = global::SIL.FieldWorks.LexText.Controls.ParserUIStrings.ksIdle_;
this.m_dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.m_dataGrid1.Name = "m_dataGrid1";
//
// btnHelp
//
resources.ApplyResources(this.m_btnHelp, "m_btnHelp");
this.m_btnHelp.Name = "m_btnHelp";
this.m_btnHelp.UseVisualStyleBackColor = true;
this.m_btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
//
// dataGrid2
//
resources.ApplyResources(this.m_dataGrid2, "m_dataGrid2");
this.m_dataGrid2.DataMember = global::SIL.FieldWorks.LexText.Controls.ParserUIStrings.ksIdle_;
this.m_dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.m_dataGrid2.Name = "m_dataGrid2";
//
// label3
//
resources.ApplyResources(this.m_label3, "m_label3");
this.m_label3.Name = "m_label3";
//
// ParserParametersDlg
//
this.AcceptButton = this.m_btnOk;
resources.ApplyResources(this, "$this");
this.CancelButton = this.m_btnCancel;
this.Controls.Add(this.m_label3);
this.Controls.Add(this.m_dataGrid2);
this.Controls.Add(this.m_btnHelp);
this.Controls.Add(this.m_dataGrid1);
this.Controls.Add(this.m_btnCancel);
this.Controls.Add(this.m_btnHCMaxCompoundRuleApps);
this.Controls.Add(this.m_btnOk);
this.Controls.Add(this.m_label2);
this.Controls.Add(this.m_label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "ParserParametersDlg";
((System.ComponentModel.ISupportInitialize)(this.m_dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.m_dataGrid2)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private void btnHelp_Click(object sender, EventArgs e)
{
ShowHelp.ShowHelpTopic(m_helpTopicProvider, HelpTopic);
}
private void btnOK_Click(object sender, EventArgs e)
{
XElement newParserParamsElem = XElement.Parse(m_dsParserParameters.GetXml());
XElement oldParserParamsElem = XElement.Parse(XmlRep);
newParserParamsElem.Add(oldParserParamsElem.Element("ActiveParser"));
newParserParamsElem.Add(oldParserParamsElem.Element("CompoundRules"));
XmlRep = newParserParamsElem.ToString();
ValidateValues(newParserParamsElem);
}
private void btnHCMaxCompoundRuleApps_Click(object sender, EventArgs e)
{
// create and show compound rule max apps dialog
var dlg = new HCMaxCompoundRulesDlg();
dlg.SetDlgInfo("MaxApps", XmlRep, m_compoundRules);
dlg.ShowDialog(this);
XmlRep = dlg.XmlRep;
}
private void ValidateValues(XElement elem)
{
EnforceValidValue(elem, XAmple, MaxNulls, 0, 10, false);
EnforceValidValue(elem, XAmple, MaxPrefixes, 0, 25, false);
EnforceValidValue(elem, XAmple, MaxSuffixes, 0, 25, false);
EnforceValidValue(elem, XAmple, MaxInfixes, 0, 7, false);
EnforceValidValue(elem, XAmple, MaxInterfixes, 0, 7, false);
EnforceValidValue(elem, XAmple, MaxRoots, 0, 10, false);
EnforceValidValue(elem, XAmple, MaxAnalysesToReturn, -1, 10000, true);
EnforceValidValue(elem, HC, DelReapps, 0, 10, false);
// For Hermit Crab, the maximum number of roots/stems allowed is between one and ten.
// The default is two in order to allow for compounding (which requires there be at least two roots/stems).
EnforceValidValue(elem, HC, MaxRoots, 1, 10, false);
}
private void EnforceValidValue(XElement elem, string parser, string item, int min, int max, bool useMinIfZero)
{
XElement valueElem = elem.Elements(parser).Elements(item).FirstOrDefault();
if (valueElem != null)
{
var val = (int) valueElem;
if (val < min || (useMinIfZero && val == 0))
{
valueElem.SetValue(min);
XmlRep = elem.ToString();
ReportChangeOfValue(item, val, min, min, max);
}
else if (val > max)
{
valueElem.SetValue(max);
XmlRep = elem.ToString();
ReportChangeOfValue(item, val, max, min, max);
}
}
}
/// <summary>
/// Set up the dlg in preparation to showing it.
/// </summary>
public void SetDlgInfo(string title, string parserParameters, ILcmOwningSequence<IMoCompoundRule> compoundRules)
{
CheckDisposed();
XmlRep = parserParameters;
Text = title;
m_dsParserParameters = new DataSet { DataSetName = "ParserParameters" };
DataTable tblXAmple = CreateXAmpleDataTable();
m_dsParserParameters.Tables.Add(tblXAmple);
DataTable tblHC = CreateHCDataTable();
m_dsParserParameters.Tables.Add(tblHC);
LoadParserData(m_dsParserParameters);
PopulateDataGrid(m_dataGrid1, XAmple);
PopulateDataGrid(m_dataGrid2, HC);
m_dataGrid2.TableStyles[0].GridColumnStyles[2].Width = 130;
m_dataGrid2.TableStyles[0].GridColumnStyles[4].Width = 160;
m_dataGrid2.TableStyles[0].GridColumnStyles[6].Width = 90;
m_dataGrid2.TableStyles[0].GridColumnStyles[7].Width = 400;
m_compoundRules = compoundRules;
if (m_compoundRules?.Count > 0)
m_btnHCMaxCompoundRuleApps.Enabled = true;
else
m_btnHCMaxCompoundRuleApps.Enabled= false;
}
private void LoadParserData(DataSet dsParserParameters)
{
var parserParamsElem = XElement.Parse(XmlRep);
// set default values for HC
XElement hcElem = parserParamsElem.Element(HC);
if (hcElem == null)
{
hcElem = new XElement(HC);
parserParamsElem.Add(hcElem);
}
if (hcElem.Element(DelReapps) == null)
hcElem.Add(new XElement(DelReapps, 0));
if (hcElem.Element(NoDefaultCompounding) == null)
hcElem.Add(new XElement(NoDefaultCompounding, false));
if (hcElem.Element(MaxRoots) == null)
hcElem.Add(new XElement(MaxRoots, 2));
if (hcElem.Element(NotOnClitics) == null)
hcElem.Add(new XElement(NotOnClitics, true));
if (hcElem.Element(AcceptUnspecifiedGraphemes) == null)
hcElem.Add(new XElement(AcceptUnspecifiedGraphemes, false));
if (hcElem.Element(GuessRoots) == null)
hcElem.Add(new XElement(GuessRoots, true));
if (hcElem.Element(MergeAnalyses) == null)
hcElem.Add(new XElement(MergeAnalyses, true));
if (hcElem.Element(Strata) == null)
hcElem.Add(new XElement(Strata, ""));
using (XmlReader reader = parserParamsElem.CreateReader())
dsParserParameters.ReadXml(reader, XmlReadMode.IgnoreSchema);
}
private void PopulateDataGrid(DataGrid dataGrid, string parser)
{
dataGrid.SetDataBinding(m_dsParserParameters, parser);
DataView view = CreateDataView(m_dsParserParameters.Tables[parser]);
dataGrid.DataSource = view;
dataGrid.TableStyles.Add(new DataGridTableStyle { MappingName = parser, RowHeadersVisible = false, AllowSorting = false });
foreach (DataGridBoolColumn col in dataGrid.TableStyles[0].GridColumnStyles.OfType<DataGridBoolColumn>())
col.AllowNull = false;
foreach (DataGridTextBoxColumn col in dataGrid.TableStyles[0].GridColumnStyles.OfType<DataGridTextBoxColumn>())
{
TextBox textBox1 = col.TextBox;
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.WordWrap = true;
dataGrid.TableStyles[0].PreferredRowHeight = 50;
}
}
private DataView CreateDataView(DataTable table)
{
return new DataView(table) { AllowNew = false };
}
private DataTable CreateXAmpleDataTable()
{
var tblXAmple = new DataTable(XAmple);
tblXAmple.Columns.Add(MaxNulls, typeof(int));
tblXAmple.Columns.Add(MaxPrefixes, typeof(int));
tblXAmple.Columns.Add(MaxInfixes, typeof(int));
tblXAmple.Columns.Add(MaxRoots, typeof(int));
tblXAmple.Columns.Add(MaxSuffixes, typeof(int));
tblXAmple.Columns.Add(MaxInterfixes, typeof(int));
tblXAmple.Columns.Add(MaxAnalysesToReturn, typeof(int));
return tblXAmple;
}
private DataTable CreateHCDataTable()
{
var tblHC = new DataTable(HC);
tblHC.Columns.Add(DelReapps, typeof(int));
tblHC.Columns.Add(NotOnClitics, typeof(bool));
tblHC.Columns.Add(NoDefaultCompounding, typeof(bool));
tblHC.Columns.Add(MaxRoots, typeof(int));
tblHC.Columns.Add(AcceptUnspecifiedGraphemes, typeof(bool));
tblHC.Columns.Add(GuessRoots, typeof(bool));
tblHC.Columns.Add(MergeAnalyses, typeof(bool));
tblHC.Columns.Add(Strata, typeof(string));
return tblHC;
}
}
}