Skip to content

Commit b1e9695

Browse files
committed
Revert File Chooser to its previous behaviour
File Chooser will once again remember where it was for each file type but in addition, it will set the file path of other file types if one isn't already set for them or if the file path is the default path (where CSV Validator is run)
1 parent 4b6a2cd commit b1e9695

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

csv-validator-ui/src/main/scala/uk/gov/nationalarchives/csv/validator/ui/CsvValidatorUi.scala

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -295,37 +295,44 @@ object CsvValidatorUi extends SimpleSwingApplication {
295295
}
296296

297297
private def lastCsvPath: File =
298-
loadSettings match {
299-
case Some(settings) => settings.lastCsvPath.toFile
300-
case None => userDir.toFile
301-
}
298+
loadSettings.flatMap { settings =>
299+
List(settings.lastCsvPath.toFile, settings.lastCsvSchemaPath.toFile, settings.lastReportPath.toFile).find(_ != userDir.toFile)
300+
}.getOrElse(userDir.toFile)
302301

303302
private val csvFileChooser = new FileChooser(lastCsvPath)
304303
csvFileChooser.title = "Select a .csv file"
305304
csvFileChooser.fileFilter = new FileNameExtensionFilter("CSV file (*.csv)", "csv")
306305
private val btnChooseCsvFile = new Button("...")
307306

308-
private def setPathToLastCsvPath(fileChooser: FileChooser): Unit = fileChooser.selectedFile = lastCsvPath
309-
310307
btnChooseCsvFile.reactions += onClick {
311-
setPathToLastCsvPath(csvFileChooser)
308+
csvFileChooser.selectedFile = lastCsvPath
312309
chooseFile(csvFileChooser, txtCsvFile, btnChooseCsvFile)
313-
updateLastPath(csvFileChooser, path => Settings(path, path, path))
310+
updateLastPath(csvFileChooser, path => loadSettings match {
311+
case Some(s) => s.copy(lastCsvPath = path)
312+
case None => Settings(path, path, path)
313+
})
314314
}
315315

316316
private val lblCsvSchemaFile = new Label("CSV Schema file:")
317317

318318
txtCsvSchemaFile.setTransferHandler(fileHandler)
319-
private val csvSchemaFileChooser = new FileChooser(lastCsvPath)
319+
private def lastCsvSchemaPath: File =
320+
loadSettings.flatMap { settings =>
321+
List(settings.lastCsvSchemaPath.toFile, settings.lastCsvPath.toFile, settings.lastReportPath.toFile).find(_ != userDir.toFile)
322+
}.getOrElse(userDir.toFile)
323+
private val csvSchemaFileChooser = new FileChooser(lastCsvSchemaPath)
320324
csvSchemaFileChooser.title = "Select a .csvs file"
321325
csvSchemaFileChooser.fileFilter = new FileNameExtensionFilter("CSV Schema file (*.csvs)", "csvs" +
322326
"")
323327

324328
private val btnChooseCsvSchemaFile = new Button("...")
325329
btnChooseCsvSchemaFile.reactions += onClick {
326-
setPathToLastCsvPath(csvSchemaFileChooser)
330+
csvSchemaFileChooser.selectedFile = lastCsvSchemaPath
327331
chooseFile(csvSchemaFileChooser, txtCsvSchemaFile, btnChooseCsvSchemaFile)
328-
updateLastPath(csvSchemaFileChooser, path => Settings(path, path, path))
332+
updateLastPath(csvSchemaFileChooser, path => loadSettings match {
333+
case Some(s) => s.copy(lastCsvSchemaPath = path)
334+
case None => Settings(path, path, path)
335+
})
329336
}
330337

331338
private val separator1 = new Separator
@@ -413,17 +420,25 @@ object CsvValidatorUi extends SimpleSwingApplication {
413420
private val btnClose = new Button("Close")
414421
btnClose.reactions += onClick(quit())
415422

416-
private val reportFileChooser = new FileChooser(lastCsvPath)
417-
val dateFormat = new SimpleDateFormat("dd-mm-yy_HH-mm-ss")
418-
reportFileChooser.selectedFile = new File(s"csv_validator_report_${dateFormat.format(new Date())}.txt")
423+
private def lastReportPath: File =
424+
loadSettings.flatMap { settings =>
425+
List(settings.lastReportPath.toFile, settings.lastCsvSchemaPath.toFile, settings.lastCsvPath.toFile).find(_ != userDir.toFile)
426+
}.getOrElse(userDir.toFile)
427+
428+
private val reportFileChooser = new FileChooser(lastReportPath)
419429

420430
val saveLabel = "Save Results"
421431
reportFileChooser.title = saveLabel
422432
private val btnSave = new Button(saveLabel)
433+
434+
val dateFormat = new SimpleDateFormat("dd-mm-yy_HH-mm-ss")
423435
btnSave.reactions += onClick {
424-
setPathToLastCsvPath(reportFileChooser)
436+
reportFileChooser.selectedFile = Paths.get(lastReportPath.toString, s"csv_validator_report_${dateFormat.format(new Date())}.txt").toFile
425437
saveFile(reportFileChooser, saveToFile(txtArReport.text, _), btnSave, btnSave.text)
426-
updateLastPath(reportFileChooser, path => Settings(path, path, path))
438+
updateLastPath(reportFileChooser, path => loadSettings match {
439+
case Some(s) => s.copy(lastReportPath = path)
440+
case None => Settings(path, path, path)
441+
})
427442
}
428443

429444
private val lblVersion = new Label(s"Version: ${getShortVersion}")

0 commit comments

Comments
 (0)