Skip to content

Commit 7a7439b

Browse files
committed
Dictionary Configuration Import: enhancements and refactoring
Also enhance the extension method StringBuilder.AppendLineFormat
1 parent e99daa2 commit 7a7439b

2 files changed

Lines changed: 26 additions & 40 deletions

File tree

Src/xWorks/Archiving/ArchivingExtensions.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015 SIL International
1+
// Copyright (c) 2015-2026 SIL International
22
// This software is licensed under the LGPL, version 2.1 or later
33
// (http://www.gnu.org/licenses/lgpl-2.1.html)
44

@@ -11,25 +11,19 @@ namespace SIL.FieldWorks.XWorks.Archiving
1111
public static class ArchivingExtensions
1212
{
1313
/// <summary>
14-
/// Combines the functionality fo StringBuilder.AppendFormat and StringBuilder.AppendLine.
15-
/// Also allows for the delimiter to be specified. If the delimiter is null, Environment.NewLine
16-
/// will be used.
14+
/// Appends a formatted line to the StringBuilder, preceded by a delimiter if the StringBuilder already has content.
15+
/// Allows specifying a custom delimiter; defaults to Environment.NewLine.
1716
/// </summary>
18-
/// <param name="sb"></param>
19-
/// <param name="format"></param>
20-
/// <param name="args"></param>
21-
/// <param name="delimiter"></param>
22-
public static void AppendLineFormat(this StringBuilder sb, string format, object[] args, string delimiter)
17+
public static StringBuilder AppendLineFormat(this StringBuilder sb, string format, object[] args, string delimiter = null)
2318
{
2419
if (delimiter == null) delimiter = Environment.NewLine;
2520
if (sb.Length != 0) sb.Append(delimiter);
26-
sb.AppendFormat(format, args);
21+
return sb.AppendFormat(format, args);
2722
}
2823

2924
/// <summary>
3025
/// Finds the ISO3 code for the given writing system.
3126
/// </summary>
32-
/// <param name="ws"></param>
3327
/// <returns>The ISO3 code, or <value>mis</value> if the code is not found.</returns>
3428
public static string GetIso3Code(this CoreWritingSystemDefinition ws)
3529
{

Src/xWorks/DictionaryConfigurationImportController.cs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.IO;
99
using System.Linq;
10+
using System.Text;
1011
using System.Windows.Forms;
1112
using System.Xml.Linq;
1213
using System.Xml.XPath;
@@ -113,17 +114,8 @@ internal void DoImport()
113114

114115
ImportCustomFields(_importLiftLocation);
115116

116-
// REVIEW (Hasso) 2026.01: should this be calculated closer to where it is used?
117-
// If the configuration to import has the same label as an existing configuration in the project folder
118-
// then overwrite the existing configuration.
119-
var existingConfigurationInTheWay = _configurations.FirstOrDefault(config => config.Label == NewConfigToImport.Label &&
120-
Path.GetDirectoryName(config.FilePath) == _projectConfigDir);
117+
NewConfigToImport.Publications.ForEach(publication => AddPublicationTypeIfNotPresent(publication, _cache));
121118

122-
NewConfigToImport.Publications.ForEach(
123-
publication =>
124-
{
125-
AddPublicationTypeIfNotPresent(publication, _cache);
126-
});
127119
try
128120
{
129121
ImportStyles(_importStylesLocation);
@@ -147,6 +139,10 @@ internal void DoImport()
147139
_view.importButton.Enabled = false;
148140
}
149141

142+
// If the configuration to import has the same label as an existing configuration in the project folder
143+
// then overwrite the existing configuration.
144+
var existingConfigurationInTheWay = _configurations.FirstOrDefault(config => config.Label == NewConfigToImport.Label &&
145+
Path.GetDirectoryName(config.FilePath) == _projectConfigDir);
150146
// We have re-loaded the model from disk to preserve custom field state so the Label must be set here
151147
NewConfigToImport.FilePath = _temporaryImportConfigLocation;
152148
NewConfigToImport.Load(_cache);
@@ -163,7 +159,8 @@ internal void DoImport()
163159
NewConfigToImport.Label = _proposedNewConfigLabel;
164160
}
165161

166-
// Set a filename for the new configuration. Use a unique filename that isn't either registered with another configuration, or existing on disk. Note that in this way, we ignore what the original filename was of the configuration file in the .zip file.
162+
// Set a filename for the new configuration. Use a unique filename that isn't either registered with another configuration, or existing on disk.
163+
// Note that in this way, we ignore what the original filename was of the configuration file in the .zip file.
167164
DictionaryConfigurationManagerController.GenerateFilePath(_projectConfigDir, _configurations, NewConfigToImport);
168165

169166
var outputConfigPath = existingConfigurationInTheWay != null ? existingConfigurationInTheWay.FilePath : NewConfigToImport.FilePath;
@@ -356,7 +353,6 @@ private IEnumerable<string> CustomFieldsInLiftFile(string liftFilePath)
356353
/// <summary>
357354
/// Connect to and show a view for the user to perform an import.
358355
/// </summary>
359-
/// <param name="dialog"></param>
360356
public void DisplayView(DictionaryConfigurationImportDlg dialog)
361357
{
362358
_view = dialog;
@@ -397,50 +393,46 @@ public void OnBrowse()
397393

398394
public void RefreshStatusDisplay()
399395
{
400-
string mainStatus;
401-
var publicationStatus = string.Empty;
402-
var customFieldStatus = string.Empty;
396+
var statusBldr = new StringBuilder();
403397
_view.explanationLabel.Text = "";
404398

405399
if (NewConfigToImport == null)
406400
{
407-
string invalidConfigFileMsg = string.Empty;
408401
if (_isInvalidConfigFile)
409402
{
410403
var configType = Path.GetFileName(_projectConfigDir) == DictionaryConfigurationListener.DictConfigDirName
411-
? xWorksStrings.ReversalIndex : xWorksStrings.Dictionary;
412-
invalidConfigFileMsg = string.Format(xWorksStrings.DictionaryConfigurationMismatch, configType)
413-
+ Environment.NewLine;
404+
? xWorksStrings.ReversalIndex : xWorksStrings.Dictionary;
405+
statusBldr.AppendFormat(xWorksStrings.DictionaryConfigurationMismatch, configType).AppendLine();
414406
}
415-
_view.explanationLabel.Text = invalidConfigFileMsg + xWorksStrings.kstidCannotImport;
407+
_view.explanationLabel.Text = statusBldr.Append(xWorksStrings.kstidCannotImport).ToString();
416408
return;
417409
}
418410

419411
if (_originalConfigLabel == _proposedNewConfigLabel)
420412
{
421-
mainStatus = string.Format(xWorksStrings.kstidImportingConfig, NewConfigToImport.Label);
413+
statusBldr.AppendFormat(xWorksStrings.kstidImportingConfig, NewConfigToImport.Label).AppendLine();
422414
}
423415
else
424416
{
425-
mainStatus = string.Format(NewConfigToImport.Label == _proposedNewConfigLabel
417+
statusBldr.AppendFormat(NewConfigToImport.Label == _proposedNewConfigLabel
426418
? xWorksStrings.kstidImportingConfigNewName
427419
: xWorksStrings.kstidImportingAndOverwritingConfiguration,
428-
NewConfigToImport.Label);
420+
NewConfigToImport.Label).AppendLine();
429421
}
430422

431423
if (_newPublications != null && _newPublications.Any())
432424
{
433-
publicationStatus = xWorksStrings.kstidPublicationsWillBeAdded + Environment.NewLine + string.Join(", ", _newPublications);
425+
statusBldr.AppendLine().AppendLine(xWorksStrings.kstidPublicationsWillBeAdded).AppendLine(string.Join(", ", _newPublications));
434426
}
435427

436428
if (_customFieldsToImport != null && _customFieldsToImport.Any())
437429
{
438-
customFieldStatus = xWorksStrings.kstidCustomFieldsWillBeAdded + Environment.NewLine + string.Join(", ", _customFieldsToImport);
430+
statusBldr.AppendLine().AppendLine(xWorksStrings.kstidCustomFieldsWillBeAdded).AppendLine(string.Join(", ", _customFieldsToImport));
439431
}
440-
// TODO (Hasso) 2026-01: WSs
441-
_view.explanationLabel.Text = string.Format("{0}{1}{2}{1}{3}{1}{4}",
442-
mainStatus, Environment.NewLine + Environment.NewLine, publicationStatus, customFieldStatus,
443-
xWorksStrings.DictionaryConfigurationDictionaryConfigurationUser_StyleOverwriteWarning);
432+
433+
statusBldr.AppendLine().Append(xWorksStrings.DictionaryConfigurationDictionaryConfigurationUser_StyleOverwriteWarning);
434+
435+
_view.explanationLabel.Text = statusBldr.ToString();
444436
_view.Refresh();
445437
}
446438

0 commit comments

Comments
 (0)