All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalafx.extras.auto_dialog.AutoDialog.scala Maven / Gradle / Ivy

There is a newer version: 0.10.1
Show newest version
/*
 * Copyright (c) 2011-2023, 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 org.scalafx.extras.auto_dialog

import org.scalafx.extras.auto_dialog.{DialogDecoder, DialogEncoder}
import org.scalafx.extras.generic_pane.GenericDialogFX
import scalafx.stage.Window

/**
 * Automatically created a dialog from a case class.
 * Names of the members of the input `data` will be used as labels for fields corresponding to their values.
 * Initially case with members of `Boolean`, `Int`, `Double`, and `String` are supported.
 *
 * Use methods [[#showDialog]] to display the dialog and retrieve edited content.
 *
 * {{{
 *   case class FilterOptions(kernelSize: Int = 7,
 *                            start: Double = 3.14,
 *                            tag: String = "alpha",
 *                            debugMode: Boolean = false)
 *
 *   val result: Option[FilterOptions] =
 *     new AutoDialog(FilterOptions())
 *       .showDialog(
 *         "Filter Options",
 *         "Fields below were auto generated from `FilterOptions` object")
 *
 *   println(s"Result: result")
 * }}}
 *
 * @param data data that will define the dialog and its initial values
 */
class AutoDialog[A >: Null: DialogEncoder: DialogDecoder](data: A):

  /**
   * Display the dialog and block till the dialog is closed.
   *
   * @param title        dialog title
   * @param header       dialog header
   * @param ownerWindow optional parent window that will be blocked when dialog is displayed
   * @return when dialog was confirmed with OK it will return content of the fields of the dialog.
   *         If dialog was cancelled it will return `None`
   */
  def showDialog(title: String, header: String = "", ownerWindow: Option[Window] = None): Option[A] =
    val gd = new GenericDialogFX(title, header = header, ownerWindow = ownerWindow)

    // Add fields to the dialog
    summon[DialogEncoder[A]].addEditor(gd, "", data)

    gd.showDialog()

    if gd.wasOKed then
      val r = summon[DialogDecoder[A]].decode(gd)
      Option(r)
    else
      None

end AutoDialog




© 2015 - 2025 Weber Informatics LLC | Privacy Policy