@@ -17,20 +17,21 @@ import java.nio.file.Path
1717import scala .swing .Dialog .Message
1818import java .io .IOException
1919import 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 */
2526object ScalaSwingHelpers {
26- private def handleSelectedFile (dialogAction : Result .Value , fileChooser : FileChooser , result : Path => Option [ IOException ], dialogText : String ): Unit =
27+ private def handleSelectedFile (dialogAction : Result .Value , fileChooser : FileChooser , result : Path => Try [ String ], dialogText : String ): Unit =
2728 dialogAction match {
2829 case Result .Approve =>
2930 result(fileChooser.selectedFile.toPath) match {
30- case Some (ioe ) =>
31- ioe .printStackTrace()
32- Dialog .showMessage(fileChooser, s " ${ioe .getClass.getName}: ${ioe .getMessage}" , s " Unable to ${dialogText.toLowerCase()} file " , Message .Error )
33- case None =>
31+ case Failure (e ) =>
32+ e .printStackTrace()
33+ Dialog .showMessage(fileChooser, s " ${e .getClass.getName}: ${e .getMessage}" , s " Unable to ${dialogText.toLowerCase()} file " , Message .Error )
34+ case Success (_) =>
3435 }
3536 case Result .Cancel =>
3637 }
@@ -42,9 +43,8 @@ object ScalaSwingHelpers {
4243 * @param output A text component which displays the absolute path of the chosen file
4344 * @param locateOver A component over which the FileChooser dialog should be located
4445 */
45- def chooseFile (fileChooser : FileChooser , output : JTextField , locateOver : Component ) : Unit = {
46- chooseFile(fileChooser, {f => output.setText(f.toAbsolutePath.toString); None }, locateOver)
47- }
46+ def chooseFile (fileChooser : FileChooser , output : JTextField , locateOver : Component ) : Unit =
47+ chooseFile(fileChooser, f => Try (output.setText(f.toAbsolutePath.toString)).map(_ => " Path updated" ), locateOver)
4848
4949 /**
5050 * Opens a FileChooser and sends the result to a function
@@ -53,7 +53,7 @@ object ScalaSwingHelpers {
5353 * @param result A function which takes the chosen file
5454 * @param locateOver A component over which the FileChooser dialog should be located
5555 */
56- def chooseFile (fileChooser : FileChooser , result : Path => Option [ IOException ], locateOver : Component , dialogText : String = " OK" ) : Unit = {
56+ def chooseFile (fileChooser : FileChooser , result : Path => Try [ String ], locateOver : Component , dialogText : String = " OK" ) : Unit = {
5757 val showDialogAction = fileChooser.showDialog(locateOver, dialogText)
5858 handleSelectedFile(showDialogAction, fileChooser, result, dialogText)
5959 }
@@ -65,7 +65,7 @@ object ScalaSwingHelpers {
6565 * @param result A function which writes the report out
6666 * @param locateOver A component over which the FileChooser dialog should be located
6767 */
68- def saveFile (fileChooser : FileChooser , result : Path => Option [ IOException ], locateOver : Component , dialogText : String ) : Unit = {
68+ def saveFile (fileChooser : FileChooser , result : Path => Try [ String ], locateOver : Component , dialogText : String ) : Unit = {
6969 val saveDialogAction = fileChooser.showSaveDialog(locateOver)
7070 handleSelectedFile(saveDialogAction, fileChooser, result, dialogText)
7171 }
0 commit comments