Skip to content

Commit

Permalink
Fixed issue #178: when FileChooser.showOpenMultipleDialog was used an…
Browse files Browse the repository at this point in the history
…d user cancelled selection an NPE was thrown.

Also clatify in Scaladoc that null may be returned.
  • Loading branch information
jpsacha committed Jan 26, 2015
1 parent ad60d5f commit fc0873e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 3 additions & 1 deletion scalafx/src/main/scala/scalafx/stage/DirectoryChooser.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011-2014, ScalaFX Project
* Copyright (c) 2011-2015, ScalaFX Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -59,6 +59,8 @@ class DirectoryChooser(override val delegate: jfxs.DirectoryChooser = new jfxs.D

/**
* Shows a new directory selection dialog.
*
* @return the selected directory or null if no directory has been selected
*/
def showDialog(ownerWindow: Window): File = delegate.showDialog(ownerWindow)
}
11 changes: 10 additions & 1 deletion scalafx/src/main/scala/scalafx/stage/FileChooser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,25 @@ class FileChooser(override val delegate: jfxs.FileChooser = new jfxs.FileChooser

/**
* Shows a new file open dialog.
*
* @return the selected file or null if no file has been selected
*/
def showOpenDialog(ownerWindow: Window): File = delegate.showOpenDialog(ownerWindow)

/**
* Shows a new file open dialog in which multiple files can be selected.
*
* @return the selected files or null if no file has been selected
*/
def showOpenMultipleDialog(ownerWindow: Window): Seq[File] = delegate.showOpenMultipleDialog(ownerWindow)
def showOpenMultipleDialog(ownerWindow: Window): Seq[File] = {
val selection = delegate.showOpenMultipleDialog(ownerWindow)
if (selection != null) selection else null.asInstanceOf[Seq[File]]
}

/**
* Shows a new file save dialog.
*
* @return the selected file or null if no file has been selected
*/
def showSaveDialog(ownerWindow: Window): File = delegate.showSaveDialog(ownerWindow)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2011-2015, ScalaFX Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the ScalaFX Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE SCALAFX PROJECT OR ITS CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package issues.issue178

import scala.language.implicitConversions
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.VBox
import scalafx.stage.FileChooser

/**
* Demo for Issue #178: FileChooser does not handle a the value returned when the user cancels file selection.
* If FileChooser.showOpenMultipleDialog was used and user cancelled selection an NPE was thrown.
*
*/
object MultipleFileChooserDemo extends JFXApp {

stage = new PrimaryStage {
scene = new Scene {
title = "Demo for Issue #178"
root = new VBox {
padding = Insets(12)
children = new Button {
text = "Open file chooser and select multiple files or Cancel"
onAction = handle {
val fc = new FileChooser()
val selection = fc.showOpenMultipleDialog(stage)

println("Selection: " + selection)
}
}
}
}
}

}

0 comments on commit fc0873e

Please sign in to comment.