Skip to content

Commit

Permalink
Fix bug in InputDialogs.askForDouble()
Browse files Browse the repository at this point in the history
  • Loading branch information
giancosta86 committed Jul 31, 2016
1 parent e6bda04 commit 333ca89
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ apply plugin: 'info.gianlucacosta.aurora'

group = 'info.gianlucacosta.helios'
archivesBaseName = 'helios-fx'
version = '5.0'
version = '5.1'

description = 'Library of ScalaFX utilities'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,30 @@ case object InputDialogs {
* @return Some(true) if the user chooses "Yes", Some(false) if the user chooses "No", None otherwise
*/
def askYesNoCancel(message: String, header: String = ""): Option[Boolean] = {
val yesButton = new ButtonType("Yes")
val noButton = new ButtonType("No")
val cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CancelClose)
val yesButton =
new ButtonType("Yes")

val alert = new Alert(Alert.AlertType.Confirmation) {
headerText = header
val noButton =
new ButtonType("No")

contentText = message
val cancelButton =
new ButtonType("Cancel", ButtonBar.ButtonData.CancelClose)

buttonTypes = List(
yesButton,
noButton,
cancelButton
)
}
val alert =
new Alert(Alert.AlertType.Confirmation) {
headerText = header

contentText = message

buttonTypes = List(
yesButton,
noButton,
cancelButton
)
}

val inputResult = alert.showAndWait()
val inputResult =
alert.showAndWait()

inputResult match {
case Some(`yesButton`) =>
Expand Down Expand Up @@ -81,7 +88,8 @@ case object InputDialogs {
resizable = true
}

val inputResult = inputDialog.showAndWait()
val inputResult =
inputDialog.showAndWait()

inputResult
.map(_.trim)
Expand Down Expand Up @@ -109,26 +117,27 @@ case object InputDialogs {
formatter: Double => String = Numbers.smartString
): Option[Double] = {
while (true) {
val inputString = askForString(
message,
formatter(initialValue),
header
)
val inputString =
askForString(
message,
formatter(initialValue),
header
)

if (inputString.isEmpty) {
return None
}


try {
val value = inputString.get.toDouble
val value =
inputString.get.toDouble

if (value < minValue || value > maxValue) {
Alerts.showWarning(
s"Please, enter a number in the range [${formatter(minValue)}; ${formatter(maxValue)}]", header)
}

return Some(value)
} else
return Some(value)

} catch {
case _: NumberFormatException =>
Expand Down Expand Up @@ -159,13 +168,15 @@ case object InputDialogs {
header: String = ""
): Option[Long] = {
while (true) {
val inputDoubleResult = askForDouble(message, initialValue, minValue, maxValue, header)
val inputDoubleResult =
askForDouble(message, initialValue, minValue, maxValue, header)

if (inputDoubleResult.isEmpty) {
return None
}

val longValueOption = Numbers.asLong(inputDoubleResult.get)
val longValueOption =
Numbers.asLong(inputDoubleResult.get)

longValueOption match {
case Some(longValue) =>
Expand Down Expand Up @@ -193,11 +204,12 @@ case object InputDialogs {
def askForItem[T](message: String, items: Seq[T], initialItem: Option[T] = None, header: String = ""): Option[T] = {
require(items.nonEmpty)

val choiceDialog = new ChoiceDialog[T](initialItem.getOrElse(items.head), items) {
headerText = header
val choiceDialog =
new ChoiceDialog[T](initialItem.getOrElse(items.head), items) {
headerText = header

contentText = message
}
contentText = message
}

choiceDialog.showAndWait()
}
Expand Down

0 comments on commit 333ca89

Please sign in to comment.