Skip to content

Commit 72fdc31

Browse files
authored
Merge pull request #531 from digital-preservation/FilePathSubstitutionRefinements
File path substitution refinements
2 parents 4b6a2cd + da17e73 commit 72fdc31

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

csv-validator-cmd/src/main/scala/uk/gov/nationalarchives/csv/validator/cmd/CsvValidatorCmdApp.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ object CsvValidatorCmdApp extends App {
146146
def getColumnFromCsv(csvFile: TextFile, csvSchemaFile: TextFile, columnName: String): List[String] = Try {
147147
val validator = createValidator(true, Nil, false, false, false)
148148
val csv = validator.loadCsvFile(csvFile, csvSchemaFile)
149-
csv.headOption.map(_.indexOf("identifier")).map { identifierIdx =>
150-
csv.tail.map(arr => arr(identifierIdx))
149+
csv.headOption.map(_.indexOf(columnName)).map { identifierIdx =>
150+
csv.tail.map(row => row(identifierIdx))
151151
}.getOrElse(Nil)
152152
}.getOrElse(Nil)
153153

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ object CsvValidatorUi extends SimpleSwingApplication {
309309

310310
btnChooseCsvFile.reactions += onClick {
311311
setPathToLastCsvPath(csvFileChooser)
312-
chooseFile(csvFileChooser, txtCsvFile, btnChooseCsvFile)
312+
chooseFileOrDir(csvFileChooser, txtCsvFile, btnChooseCsvFile)
313313
updateLastPath(csvFileChooser, path => Settings(path, path, path))
314314
}
315315

@@ -324,7 +324,7 @@ object CsvValidatorUi extends SimpleSwingApplication {
324324
private val btnChooseCsvSchemaFile = new Button("...")
325325
btnChooseCsvSchemaFile.reactions += onClick {
326326
setPathToLastCsvPath(csvSchemaFileChooser)
327-
chooseFile(csvSchemaFileChooser, txtCsvSchemaFile, btnChooseCsvSchemaFile)
327+
chooseFileOrDir(csvSchemaFileChooser, txtCsvSchemaFile, btnChooseCsvSchemaFile)
328328
updateLastPath(csvSchemaFileChooser, path => Settings(path, path, path))
329329
}
330330

@@ -497,34 +497,34 @@ object CsvValidatorUi extends SimpleSwingApplication {
497497
val identifierRows = CsvValidatorCmdApp.getColumnFromCsv(csvFile, schemaFile, "identifier").sorted
498498
val fromPath = identifierRows.headOption.getOrElse("")
499499

500-
val fileTextField = new TextField(30)
501-
val fromPathText = new TextField(fromPath, 30)
500+
val toPathField = new TextField(30)
501+
val fromPathField = new TextField(fromPath, 30)
502502

503503
def pathToUri(path: Path) = {
504504
val uri = path.toUri.toString
505505
if (uri.endsWith("/")) uri else s"$uri/"
506506
}
507507

508508
def updateFileText(path: Path): Try[String] = {
509-
fileTextField.text = pathToUri(path)
509+
toPathField.text = pathToUri(path)
510510
Success("Text updated")
511511
}
512512

513-
val okButton = new Button("OK")
514513
val fileButton = new Button("...")
515514
fileButton.reactions += {
516515
case ev: ButtonClicked =>
517-
val startingDir = if(fileTextField.text.isEmpty) userDir.toFile else Path.of(fileTextField.text).toFile
518-
val helpText = s"Select the ${fromPath.split("/").last} folder"
516+
val startingDir = if(toPathField.text.isEmpty) userDir.toFile else Path.of(toPathField.text).toFile
517+
val fromFolderName = Path.of(fromPathField.text).getFileName
518+
val helpText = s"Select the ${fromFolderName} folder"
519519
val fileChooser = new FileChooser(startingDir)
520520
fileChooser.title = helpText
521-
fileChooser.fileSelectionMode = SelectionMode.FilesAndDirectories
522-
chooseFile(fileChooser, f => updateFileText(f), fileButton, helpText)
521+
fileChooser.fileSelectionMode = SelectionMode.DirectoriesOnly
522+
chooseFileOrDir(fileChooser, f => updateFileText(f), fileButton, helpText)
523523
}
524524

525525
val rows = List(
526-
Row("From", List(fromPathText)),
527-
Row("To", List(fileTextField, fileButton))
526+
Row("From", List(fromPathField)),
527+
Row("To", List(toPathField, fileButton))
528528
)
529529
addToTableDialog(parentFrame, "Add Path Substitution...", rows, tblPathSubstitutions.addRow)
530530
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,39 @@ import scala.util.{Failure, Success, Try}
2424
* the use of scala.swing
2525
*/
2626
object ScalaSwingHelpers {
27-
private def handleSelectedFile(dialogAction: Result.Value, fileChooser: FileChooser, result: Path => Try[String], fileAction: String): Unit =
27+
private def handleSelectedFileOrDir(dialogAction: Result.Value, fileOrDirChooser: FileChooser, result: Path => Try[String], fileAction: String): Unit =
2828
dialogAction match {
2929
case Result.Approve =>
30-
result(fileChooser.selectedFile.toPath) match {
30+
result(fileOrDirChooser.selectedFile.toPath) match {
3131
case Failure(e) =>
3232
e.printStackTrace()
33-
Dialog.showMessage(fileChooser, s"${e.getClass.getName}: ${e.getMessage}", s"Unable to ${fileAction.toLowerCase()} file", Message.Error)
33+
Dialog.showMessage(fileOrDirChooser, s"${e.getClass.getName}: ${e.getMessage}", s"Unable to ${fileAction.toLowerCase()} file", Message.Error)
3434
case _ => ()
3535
}
3636
case _ => ()
3737
}
3838

3939
/**
40-
* Opens a FileChooser and sets the path of the chosen file as the text of a Text Component
40+
* Opens a FileChooser and sets the path of the chosen file/dir as the text of a Text Component
4141
*
42-
* @param fileChooser FileChooser which is Used to open file dialogs
42+
* @param fileOrDirChooser FileChooser which is Used to open file/dir dialogs
4343
* @param output A text component which displays the absolute path of the chosen file
4444
* @param locateOver A component over which the FileChooser dialog should be located
4545
*/
46-
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit =
47-
chooseFile(fileChooser, f => Try(output.setText(f.toAbsolutePath.toString)).map(_ => "Path updated"), locateOver)
46+
def chooseFileOrDir(fileOrDirChooser: FileChooser, output: JTextField, locateOver: Component) : Unit =
47+
chooseFileOrDir(fileOrDirChooser, f => Try(output.setText(f.toAbsolutePath.toString)).map(_ => "Path updated"), locateOver)
4848

4949
/**
5050
* Opens a FileChooser and sends the result to a function
5151
*
52-
* @param fileChooser FileChooser which is Used to open file dialogs
52+
* @param fileOrDirChooser FileChooser which is Used to open file/dir dialogs
5353
* @param result A function which takes the chosen file
5454
* @param locateOver A component over which the FileChooser dialog should be located
5555
* @param approveBtnText The text to appear on the approval button of the dialog box
5656
*/
57-
def chooseFile(fileChooser: FileChooser, result: Path => Try[String], locateOver: Component, approveBtnText: String = "OK") : Unit = {
58-
val showDialogAction = fileChooser.showDialog(locateOver, approveBtnText)
59-
handleSelectedFile(showDialogAction, fileChooser, result, "get")
57+
def chooseFileOrDir(fileOrDirChooser: FileChooser, result: Path => Try[String], locateOver: Component, approveBtnText: String = "OK") : Unit = {
58+
val showDialogAction = fileOrDirChooser.showDialog(locateOver, approveBtnText)
59+
handleSelectedFileOrDir(showDialogAction, fileOrDirChooser, result, "get")
6060
}
6161

6262
/**
@@ -69,7 +69,7 @@ object ScalaSwingHelpers {
6969
*/
7070
def saveFile(fileChooser: FileChooser, result: Path => Try[String], locateOver: Component, approveBtnText: String) : Unit = {
7171
val saveDialogAction = fileChooser.showSaveDialog(locateOver)
72-
handleSelectedFile(saveDialogAction, fileChooser, result, approveBtnText)
72+
handleSelectedFileOrDir(saveDialogAction, fileChooser, result, approveBtnText)
7373
}
7474

7575
/**

0 commit comments

Comments
 (0)