From e050f076923c2379c5527b14a6df579916566de1 Mon Sep 17 00:00:00 2001 From: Sarah Gerweck Date: Sat, 4 Jun 2016 00:34:16 -0700 Subject: [PATCH] A helper for making dialog button bars JavaFX & ScalaFX already have one of these, but this handles padding and automatically set up the apply, ok, cancel trilogy (though you can disable any of these). --- .../scalafx/util/control/DialogButtons.scala | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/main/scala/org/gerweck/scalafx/util/control/DialogButtons.scala diff --git a/src/main/scala/org/gerweck/scalafx/util/control/DialogButtons.scala b/src/main/scala/org/gerweck/scalafx/util/control/DialogButtons.scala new file mode 100644 index 0000000..d6a12b9 --- /dev/null +++ b/src/main/scala/org/gerweck/scalafx/util/control/DialogButtons.scala @@ -0,0 +1,87 @@ +package org.gerweck.scalafx.util +package control + +import org.log4s._ + +import scalafx.Includes._ +import scalafx.event.ActionEvent +import scalafx.event.ActionEvent +import scalafx.geometry.Insets +import scalafx.scene.control._ +import scalafx.scene.layout._ +import scalafx.stage.Stage +import javafx.scene.{ Node => JNode } +import javafx.scene.layout.{ Region => JRegion } +import scalafx.delegate.SFXDelegate + +/** A standard button bar with the major dialog buttons. + * + * This will show the Okay, Cancel and Apply buttons, as well as optionally + * including a separator. + */ +class DialogButtons(stage: Stage, showOk: Boolean = true, showApply: Boolean = true, showCancel: Boolean = true) extends Region(new DialogButtons.DialogButtonsJFXRegion) { + protected[this] val logger = getLogger + + def insetSize = 10d + def buttonInsets = Insets(insetSize) + + def applyChanges(): Unit = {} + + protected[this] def typedButton(bt: ButtonType) = { + val b = new Button(bt.text) { + padding = buttonInsets + } + ButtonBar.setButtonData(b, bt.buttonData) + b + } + lazy val okButton = { + val b = typedButton(ButtonType.OK) + b.onAction = { ae: ActionEvent => + logger.debug(s"OK button pressed") + applyChanges() + stage.close() + } + b + } + lazy val cancelButton = { + val b = typedButton(ButtonType.Close) + b.onAction = { ae: ActionEvent => + logger.debug("Cancel button pressed") + stage.close() + } + b + } + lazy val applyButton = { + val b = typedButton(ButtonType.Apply) + b.onAction = { ae: ActionEvent => + logger.debug("Apply button pressed") + applyChanges() + } + b + } + private[this] def seqIf[A](gate: Boolean, a: => A): Seq[A] = { + if (gate) Seq(a) else Seq.empty + } + def bar = new ButtonBar { + padding = buttonInsets + buttons = { + seqIf(showApply, applyButton) ++ + seqIf(showOk, okButton) ++ + seqIf(showCancel, cancelButton) + } + } + + protected[this] def mainLayout = new VBox(Separators.horizontal, bar) { + hgrow = Priority.Always + } + + delegate.asInstanceOf[DialogButtons.DialogButtonsJFXRegion].addChild(mainLayout) +} + +private object DialogButtons { + class DialogButtonsJFXRegion extends JRegion { + protected[control] def addChild(n: JNode) = { + getChildren.add(n) + } + } +}