Skip to content

Commit 4475595

Browse files
authored
Fixes #2834 Separator Selector is styled differently and pops-under pk-sim (#2835)
* Fixes #2834 Separator Selector is styled differently and pops-under pk-sim * pr feedback
1 parent 763ddbe commit 4475595

File tree

9 files changed

+55
-38
lines changed

9 files changed

+55
-38
lines changed

src/OSPSuite.Infrastructure.Import/Core/DataSourceFile.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OSPSuite.Infrastructure.Import.Core
1212
/// </summary>
1313
public interface IDataSourceFile
1414
{
15-
string Path { get; set; }
15+
string Path { get; }
1616
IDataFormat Format { get; set; }
1717

1818
IList<IDataFormat> AvailableFormats { get; set; }
@@ -22,6 +22,11 @@ public interface IDataSourceFile
2222
//as active when initialized
2323
string FormatCalculatedFrom { get; set; }
2424
DataSheetCollection DataSheets { get; }
25+
26+
/// <summary>
27+
/// Loads the file at <paramref name="path" /> into the data source.
28+
/// </summary>
29+
void LoadFromFile(string path);
2530
}
2631

2732
public abstract class DataSourceFile : IDataSourceFile
@@ -52,29 +57,31 @@ protected DataSourceFile(IImportLogger logger, IHeavyWorkManager heavyWorkManage
5257
_heavyWorkManager = heavyWorkManager;
5358
}
5459

55-
private string _path;
60+
public string Path { get; private set; }
61+
62+
protected abstract void DoLoadWork(string path, CancellationToken cancellationToken = default);
5663

57-
public string Path
64+
public virtual void LoadFromFile(string path)
5865
{
59-
get => _path;
60-
set
66+
67+
var cts = new CancellationTokenSource();
68+
if(heavyWorkSucceeds(path, cts))
69+
Path = path;
70+
}
71+
72+
private bool heavyWorkSucceeds(string path, CancellationTokenSource cts)
73+
{
74+
return _heavyWorkManager.Start(() =>
6175
{
62-
_path = value;
63-
var cts = new CancellationTokenSource();
64-
_heavyWorkManager.Start(() =>
76+
try
6577
{
66-
try
67-
{
68-
LoadFromFile(_path, cts.Token);
69-
}
70-
catch (OperationCanceledException)
71-
{
72-
//Nothing to do, just not throw exception.
73-
}
74-
}, "Importing data...", cts);
75-
}
78+
DoLoadWork(path, cts.Token);
79+
}
80+
catch (OperationCanceledException)
81+
{
82+
//Nothing to do, just not throw exception.
83+
}
84+
}, "Importing data...", cts);
7685
}
77-
78-
protected abstract void LoadFromFile(string path, CancellationToken cancellationToken = default);
7986
}
8087
}

src/OSPSuite.Infrastructure.Import/Core/DataSourceFileParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ public IDataSourceFile For(string path)
2828
var lowerCasePath = path.ToLower();
2929
if (_csvExtensions.Any(lowerCasePath.EndsWith))
3030
{
31-
_csvDataSourceFile.Path = path;
31+
_csvDataSourceFile.LoadFromFile(path);
3232
return _csvDataSourceFile;
3333
}
3434

3535
if (_excelExtensions.Any(lowerCasePath.EndsWith))
3636
{
37-
_excelDataSourceFile.Path = path;
37+
_excelDataSourceFile.LoadFromFile(path);
3838
return _excelDataSourceFile;
3939
}
4040

src/OSPSuite.Infrastructure.Import/Core/DataSourceFileReaders/CsvDataSourceFile.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ public interface ICsvDataSourceFile : IDataSourceFile
1414
public class CsvDataSourceFile : DataSourceFile, ICsvDataSourceFile
1515
{
1616
private readonly ICsvSeparatorSelector _csvSeparatorSelector;
17+
private CSVSeparators _csvSeparators;
1718

1819
public CsvDataSourceFile(IImportLogger logger, ICsvSeparatorSelector csvSeparatorSelector, IHeavyWorkManager heavyWorkManager) : base(logger, heavyWorkManager)
1920
{
2021
_csvSeparatorSelector = csvSeparatorSelector;
2122
}
2223

23-
protected override void LoadFromFile(string path, CancellationToken c)
24+
public override void LoadFromFile(string path)
2425
{
25-
var csvSeparators = _csvSeparatorSelector.GetCsvSeparator(path);
26+
_csvSeparators = _csvSeparatorSelector.GetCsvSeparator(path);
2627

2728
//if separator selection dialog was cancelled, abort
28-
if (csvSeparators == null)
29+
if (_csvSeparators == null)
2930
return;
3031

32+
base.LoadFromFile(path);
33+
}
34+
35+
protected override void DoLoadWork(string path, CancellationToken cancellationToken = default)
36+
{
3137
//we keep a copy of the already loaded sheets, in case the reading fails
3238
var alreadyLoadedDataSheets = DataSheets.Clone();
3339
DataSheets.Clear();
3440

3541
try
3642
{
37-
using (var reader = new CsvReaderFromFile(path, csvSeparators.ColumnSeparator))
43+
using (var reader = new CsvReaderFromFile(path, _csvSeparators.ColumnSeparator))
3844
{
3945
var csv = reader.Csv;
4046
var headers = csv.GetFieldHeaders();
@@ -52,7 +58,7 @@ protected override void LoadFromFile(string path, CancellationToken c)
5258
{
5359
csv.CopyCurrentRecordTo(currentRow);
5460
var currentCultureDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
55-
var rowList = currentRow.Select(x => x.Replace(csvSeparators.DecimalSeparator, currentCultureDecimalSeparator)).ToList();
61+
var rowList = currentRow.Select(x => x.Replace(_csvSeparators.DecimalSeparator, currentCultureDecimalSeparator)).ToList();
5662
var levels = getMeasurementLevels(rowList);
5763
dataSheet.CalculateColumnDescription(levels);
5864
dataSheet.AddRow(rowList);

src/OSPSuite.Infrastructure.Import/Core/DataSourceFileReaders/ExcelDataSourceFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ExcelDataSourceFile(IImportLogger logger, IHeavyWorkManager heavyWorkMana
1818
{
1919
}
2020

21-
protected override void LoadFromFile(string path, CancellationToken cancellationToken = default)
21+
protected override void DoLoadWork(string path, CancellationToken cancellationToken = default)
2222
{
2323
//we keep a copy of the already loaded sheets, in case the reading fails
2424
var alreadyLoadedDataSheets = DataSheets.Clone();

src/OSPSuite.Presentation/Presenters/Importer/ImporterDataPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void RemoveTab(string tabName)
216216

217217
public void ReopenAllSheets()
218218
{
219-
_dataSourceFile.Path = _dataSourceFile.Path;
219+
_dataSourceFile.LoadFromFile(_dataSourceFile.Path);
220220
RefreshTabs();
221221
}
222222

src/OSPSuite.UI/Views/Importer/CsvSeparatorSelectorView.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DevExpress.XtraLayout.Utils;
44
using OSPSuite.Presentation.Extensions;
55
using OSPSuite.Presentation.Presenters.Importer;
6+
using OSPSuite.Presentation.Views;
67
using OSPSuite.Presentation.Views.Importer;
78
using static OSPSuite.Assets.Captions.Importer;
89

@@ -16,7 +17,12 @@ public partial class CSVSeparatorSelectorView : BaseModalView, ICSVSeparatorSele
1617
private readonly List<char> _columnSeparatorList = new List<char> { Comma, ' ', ';', Period };
1718
private readonly List<char> _decimalSeparatorList = new List<char> { Period, Comma };
1819

19-
public CSVSeparatorSelectorView()
20+
//only for design time
21+
public CSVSeparatorSelectorView() : this(null)
22+
{
23+
}
24+
25+
public CSVSeparatorSelectorView(IShell shell) : base(shell)
2026
{
2127
InitializeComponent();
2228
fillSeparatorComboBox();

tests/OSPSuite.Presentation.Tests/Importer/Core/DataSourceFileReaders/CsvDataSourceFileSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected override void Context()
4242

4343
protected override void Because()
4444
{
45-
sut.Path = _csvFilePath;
45+
sut.LoadFromFile(_csvFilePath);
4646
}
4747

4848
[TestCase]
@@ -123,7 +123,7 @@ protected override void Context()
123123
[Observation]
124124
public void duplicate_header_file_throws_exception()
125125
{
126-
Assert.Throws<InvalidObservedDataFileException>(() => sut.Path = _csvFilePath);
126+
Assert.Throws<InvalidObservedDataFileException>(() => sut.LoadFromFile(_csvFilePath));
127127
}
128128
}
129129
}

tests/OSPSuite.Presentation.Tests/Importer/Core/DataSourceFileReaders/ExcelDataSourceFileSpecs.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ protected override void Context()
2525
{
2626
workManager = new HeavyWorkManagerForSpecs();
2727

28-
sut = new ExcelDataSourceFile(A.Fake<IImportLogger>(), workManager)
29-
{
30-
Path = _excelFilePath
31-
};
28+
sut = new ExcelDataSourceFile(A.Fake<IImportLogger>(), workManager);
29+
sut.LoadFromFile(_excelFilePath);
3230
}
3331

3432
public override void GlobalContext()
@@ -43,7 +41,7 @@ public class When_reading_excel : ConcernForExcelDataSourceFile
4341
[TestCase]
4442
public void headers_are_adjusted_on_empty_columns()
4543
{
46-
sut.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "sample2.xlsx");
44+
sut.LoadFromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "sample2.xlsx"));
4745
var columns = sut.DataSheets.ElementAt(0).GetHeaders();
4846
columns.Count().ShouldBeEqualTo(4);
4947
for (var i = 0; i < 4; i++)

tests/OSPSuite.Presentation.Tests/Importer/Presenters/ImporterPresenterSpecs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ protected override void Context()
238238

239239
_dataSourceFile = new ExcelDataSourceFile(A.Fake<IImportLogger>(), new HeavyWorkManagerForSpecs());
240240
_dataSourceFile.Format = A.Fake<IDataFormat>();
241-
_dataSourceFile.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "IntegrationSampleUnitFromColumn.xlsx");
241+
_dataSourceFile.LoadFromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "IntegrationSampleUnitFromColumn.xlsx"));
242242
A.CallTo(() => _importerDataPresenter.SetDataSource(A<string>.Ignored)).Returns(_dataSourceFile);
243243
_importerDataPresenter.OnImportSheets += Raise.With(new ImportSheetsEventArgs()
244244
{ Filter = "", DataSourceFile = _dataSourceFile, SheetNames = _sheets.Keys.ToList() });

0 commit comments

Comments
 (0)