Skip to content

Commit 7a8896f

Browse files
authored
Merge pull request #522 from digital-preservation/DR2-2044_replaceSaveButtonWithOk
DR2 2044 replace save button with ok
2 parents c04e571 + 3f3b9f3 commit 7a8896f

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import java.net.URL
2424
import java.nio.charset.Charset
2525
import java.nio.charset.StandardCharsets.UTF_8
2626
import java.nio.file.{Files, Path, Paths, StandardOpenOption}
27+
import java.text.SimpleDateFormat
2728
import java.util
28-
import java.util.Properties
29+
import java.util.{Date, Properties}
2930
import java.util.jar.{Attributes, Manifest}
3031
import javax.swing.SpringLayout.Constraints
3132
import javax.swing._
@@ -187,14 +188,9 @@ object CsvValidatorUi extends SimpleSwingApplication {
187188
* @param s String to save to the file
188189
* @param f File to which the associated string is saved
189190
*/
190-
private def saveToFile(s: String, f: Path) : Option[IOException] = {
191-
val data : Array[Byte] = s.getBytes(UTF_8)
192-
try {
193-
Files.write(f, data, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)
194-
None
195-
} catch {
196-
case ioe: IOException => Some(ioe)
197-
}
191+
private def saveToFile(s: String, f: Path) : Try[String] = {
192+
val data : Array[Byte] = s.getBytes(UTF_8)
193+
Try(Files.write(f, data, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)).map(_ => "s has been written to file")
198194
}
199195

200196
case class Settings(lastCsvPath: Path, lastCsvSchemaPath: Path, lastReportPath: Path)
@@ -293,6 +289,7 @@ object CsvValidatorUi extends SimpleSwingApplication {
293289
case None =>
294290
userDir.toFile
295291
})
292+
csvFileChooser.title = "Select a .csv file"
296293
csvFileChooser.fileFilter = new FileNameExtensionFilter("CSV file (*.csv)", "csv")
297294
private val btnChooseCsvFile = new Button("...")
298295

@@ -318,6 +315,7 @@ object CsvValidatorUi extends SimpleSwingApplication {
318315
case None =>
319316
userDir.toFile
320317
})
318+
csvSchemaFileChooser.title = "Select a .csvs file"
321319
csvSchemaFileChooser.fileFilter = new FileNameExtensionFilter("CSV Schema file (*.csvs)", "csvs" +
322320
"")
323321

@@ -423,9 +421,14 @@ object CsvValidatorUi extends SimpleSwingApplication {
423421
case None =>
424422
userDir.toFile
425423
})
426-
private val btnSave = new Button("Save")
424+
val dateFormat = new SimpleDateFormat("dd-mm-yy_HH-mm-ss")
425+
reportFileChooser.selectedFile = new File(s"csv_validator_report_${dateFormat.format(new Date())}.txt")
426+
427+
val saveLabel = "Save Results"
428+
reportFileChooser.title = saveLabel
429+
private val btnSave = new Button(saveLabel)
427430
btnSave.reactions += onClick {
428-
chooseFile(reportFileChooser, saveToFile(txtArReport.text, _), btnSave)
431+
saveFile(reportFileChooser, saveToFile(txtArReport.text, _), btnSave, btnSave.text)
429432
updateLastPath(reportFileChooser, {
430433
path =>
431434
loadSettings match {
@@ -515,19 +518,21 @@ object CsvValidatorUi extends SimpleSwingApplication {
515518
if (uri.endsWith("/")) uri else s"$uri/"
516519
}
517520

518-
def updateFileText(path: Path): Option[IOException] = {
521+
def updateFileText(path: Path): Try[String] = {
519522
fileTextField.text = pathToUri(path)
520-
None
523+
Success("Text updated")
521524
}
522525

523526
val okButton = new Button("OK")
524527
val fileButton = new Button("...")
525528
fileButton.reactions += {
526529
case ev: ButtonClicked =>
527530
val startingDir = if(fileTextField.text.isEmpty) userDir.toFile else Path.of(fileTextField.text).toFile
531+
val helpText = s"Select the ${fromPath.split("/").last} folder"
528532
val fileChooser = new FileChooser(startingDir)
533+
fileChooser.title = helpText
529534
fileChooser.fileSelectionMode = SelectionMode.FilesAndDirectories
530-
chooseFile(fileChooser, f => updateFileText(f), fileButton, s"Select the ${fromPath.split("/").last} folder")
535+
chooseFile(fileChooser, f => updateFileText(f), fileButton, helpText)
531536
}
532537

533538
val rows = List(

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ import java.nio.file.Path
1717
import scala.swing.Dialog.Message
1818
import java.io.IOException
1919
import javax.swing.JTextField
20+
import scala.util.{Failure, Success, Try}
2021

2122
/**
2223
* Some simple helpers to ease
2324
* the use of scala.swing
2425
*/
2526
object ScalaSwingHelpers {
27+
private def handleSelectedFile(dialogAction: Result.Value, fileChooser: FileChooser, result: Path => Try[String], fileAction: String): Unit =
28+
dialogAction match {
29+
case Result.Approve =>
30+
result(fileChooser.selectedFile.toPath) match {
31+
case Failure(e) =>
32+
e.printStackTrace()
33+
Dialog.showMessage(fileChooser, s"${e.getClass.getName}: ${e.getMessage}", s"Unable to ${fileAction.toLowerCase()} file", Message.Error)
34+
case _ => ()
35+
}
36+
case _ => ()
37+
}
2638

2739
/**
2840
* Opens a FileChooser and sets the path of the chosen file as the text of a Text Component
@@ -31,28 +43,33 @@ object ScalaSwingHelpers {
3143
* @param output A text component which displays the absolute path of the chosen file
3244
* @param locateOver A component over which the FileChooser dialog should be located
3345
*/
34-
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit = {
35-
chooseFile(fileChooser, {f => output.setText(f.toAbsolutePath.toString); None}, locateOver)
36-
}
46+
def chooseFile(fileChooser: FileChooser, output: JTextField, locateOver: Component) : Unit =
47+
chooseFile(fileChooser, f => Try(output.setText(f.toAbsolutePath.toString)).map(_ => "Path updated"), locateOver)
3748

3849
/**
3950
* Opens a FileChooser and sends the result to a function
4051
*
4152
* @param fileChooser FileChooser which is Used to open file dialogs
4253
* @param result A function which takes the chosen file
4354
* @param locateOver A component over which the FileChooser dialog should be located
55+
* @param approveBtnText The text to appear on the approval button of the dialog box
4456
*/
45-
def chooseFile(fileChooser: FileChooser, result: Path => Option[IOException], locateOver: Component, dialogText: String = "Save") : Unit = {
46-
fileChooser.showDialog(locateOver, dialogText) match {
47-
case Result.Approve =>
48-
result(fileChooser.selectedFile.toPath) match {
49-
case Some(ioe) =>
50-
ioe.printStackTrace()
51-
Dialog.showMessage(fileChooser, s"${ioe.getClass.getName}: ${ioe.getMessage}", "Unable to Save file", Message.Error)
52-
case None =>
53-
}
54-
case Result.Cancel =>
55-
}
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")
60+
}
61+
62+
/**
63+
* Opens a FileChooser and sends the result to a function
64+
*
65+
* @param fileChooser FileChooser which is Used to open file dialogs
66+
* @param result A function which writes the report out
67+
* @param locateOver A component over which the FileChooser dialog should be located
68+
* @param approveBtnText The text to appear on the approval button of the dialog box
69+
*/
70+
def saveFile(fileChooser: FileChooser, result: Path => Try[String], locateOver: Component, approveBtnText: String) : Unit = {
71+
val saveDialogAction = fileChooser.showSaveDialog(locateOver)
72+
handleSelectedFile(saveDialogAction, fileChooser, result, approveBtnText)
5673
}
5774

5875
/**
@@ -68,7 +85,7 @@ object ScalaSwingHelpers {
6885
val c = List()
6986
def addToTableDialog(owner: Window, title: String, rows: List[Row], result: Array[String] => Unit) : Unit = {
7087

71-
val btnOk = new Button("Ok")
88+
val btnOk = new Button("OK")
7289

7390
val optionLayout: GridBagPanel = new GridBagPanel {
7491
val c = new Constraints

0 commit comments

Comments
 (0)