<commit_before>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.ui

import com.almasb.fxgl.core.util.EmptyRunnable
import com.almasb.fxgl.localization.LocalizationService
import javafx.beans.binding.StringBinding
import javafx.beans.property.DoubleProperty
import javafx.beans.value.ChangeListener
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.control.Button
import javafx.scene.control.ProgressBar
import javafx.scene.control.ProgressIndicator
import javafx.scene.control.TextField
import javafx.scene.layout.HBox
import javafx.scene.layout.Pane
import javafx.scene.layout.StackPane
import javafx.scene.layout.VBox
import javafx.scene.text.Text
import java.util.function.Consumer
import java.util.function.Predicate

/**
 *
 *
 * @author <NAME> (<EMAIL>)
 */
class FXGLDialogFactoryServiceProvider : DialogFactoryService() {

    private lateinit var uiFactory: UIFactoryService

    private lateinit var local: LocalizationService

    /**
     * Creates a rectangular wrapper around the content node.
     */
    private fun wrap(n: Node): Pane {
        val wrapper = StackPane(n)
        wrapper.minWidth = 600.0
        wrapper.padding = Insets(20.0)
        wrapper.styleClass.add("dialog-border")

        return wrapper
    }

    override fun messageDialog(message: String): Pane {
        return messageDialog(message, EmptyRunnable)
    }

    override fun messageDialog(message: String, callback: Runnable): Pane {
        val text = createMessage(message)

        val btnOK = uiFactory.newButton(localizedStringProperty("dialog.ok"))
        btnOK.setOnAction {
            callback.run()
        }

        val vbox = VBox(50.0, text, btnOK)
        vbox.setAlignment(Pos.CENTER)

        return wrap(vbox)
    }

    override fun confirmationDialog(message: String, callback: Consumer<Boolean>): Pane {
        val text = createMessage(message)

        val btnYes = uiFactory.newButton(localizedStringProperty("dialog.yes"))
        btnYes.setOnAction {
            callback.accept(true)
        }

        val btnNo = uiFactory.newButton(localizedStringProperty("dialog.no"))
        btnNo.setOnAction {
            callback.accept(false)
        }

        val hbox = HBox(btnYes, btnNo)
        hbox.alignment = Pos.CENTER

        val vbox = VBox(50.0, text, hbox)
        vbox.setAlignment(Pos.CENTER)

        return wrap(vbox)
    }

    override fun inputDialog(message: String, callback: Consumer<String>): Pane {
        return inputDialog(message, Predicate { true }, callback)
    }

    override fun inputDialog(message: String, filter: Predicate<String>, callback: Consumer<String>): Pane {
        val text = createMessage(message)

        val field = TextField()
        field.maxWidth = Math.max(text.layoutBounds.width, 200.0)
        field.font = uiFactory.newFont(18.0)

        field.focusedProperty().addListener { _, _, isFocused ->
            if (!isFocused && field.scene != null) {
                field.requestFocus()
            }
        }

        field.sceneProperty().addListener { _, _, scene ->
            if (scene != null) {
                field.requestFocus()
            }
        }

        field.setOnAction {
            callback.accept(field.text)
        }

        val btnOK = uiFactory.newButton(localizedStringProperty("dialog.ok"))

        field.textProperty().addListener { _, _, newInput ->
            btnOK.isDisable = newInput.isEmpty() || !filter.test(newInput)
        }

        btnOK.isDisable = true
        btnOK.setOnAction {<commit_msg>only accept user input when valid<commit_after>/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (<EMAIL>).
 * See LICENSE for details.
 */

package com.almasb.fxgl.ui

import com.almasb.fxgl.core.util.EmptyRunnable
import com.almasb.fxgl.localization.LocalizationService
import javafx.beans.binding.StringBinding
import javafx.beans.property.DoubleProperty
import javafx.beans.value.ChangeListener
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Node
import javafx.scene.control.Button
import javafx.scene.control.ProgressBar
import javafx.scene.control.ProgressIndicator
import javafx.scene.control.TextField
import javafx.scene.layout.HBox
import javafx.scene.layout.Pane
import javafx.scene.layout.StackPane
import javafx.scene.layout.VBox
import javafx.scene.text.Text
import java.util.function.Consumer
import java.util.function.Predicate

/**
 *
 *
 * @author <NAME> (<EMAIL>)
 */
class FXGLDialogFactoryServiceProvider : DialogFactoryService() {

    private lateinit var uiFactory: UIFactoryService

    private lateinit var local: LocalizationService

    /**
     * Creates a rectangular wrapper around the content node.
     */
    private fun wrap(n: Node): Pane {
        val wrapper = StackPane(n)
        wrapper.minWidth = 600.0
        wrapper.padding = Insets(20.0)
        wrapper.styleClass.add("dialog-border")

        return wrapper
    }

    override fun messageDialog(message: String): Pane {
        return messageDialog(message, EmptyRunnable)
    }

    override fun messageDialog(message: String, callback: Runnable): Pane {
        val text = createMessage(message)

        val btnOK = uiFactory.newButton(localizedStringProperty("dialog.ok"))
        btnOK.setOnAction {
            callback.run()
        }

        val vbox = VBox(50.0, text, btnOK)
        vbox.setAlignment(Pos.CENTER)

        return wrap(vbox)
    }

    override fun confirmationDialog(message: String, callback: Consumer<Boolean>): Pane {
        val text = createMessage(message)

        val btnYes = uiFactory.newButton(localizedStringProperty("dialog.yes"))
        btnYes.setOnAction {
            callback.accept(true)
        }

        val btnNo = uiFactory.newButton(localizedStringProperty("dialog.no"))
        btnNo.setOnAction {
            callback.accept(false)
        }

        val hbox = HBox(btnYes, btnNo)
        hbox.alignment = Pos.CENTER

        val vbox = VBox(50.0, text, hbox)
        vbox.setAlignment(Pos.CENTER)

        return wrap(vbox)
    }

    override fun inputDialog(message: String, callback: Consumer<String>): Pane {
        return inputDialog(message, Predicate { true }, callback)
    }

    override fun inputDialog(message: String, filter: Predicate<String>, callback: Consumer<String>): Pane {
        val text = createMessage(message)

        val field = TextField()
        field.maxWidth = Math.max(text.layoutBounds.width, 200.0)
        field.font = uiFactory.newFont(18.0)

        field.focusedProperty().addListener { _, _, isFocused ->
            if (!isFocused && field.scene != null) {
                field.requestFocus()
            }
        }

        field.sceneProperty().addListener { _, _, scene ->
            if (scene != null) {
                field.requestFocus()
            }
        }

        field.setOnAction {
            val newInput = field.text

            if (newInput.isEmpty() || !filter.test(newInput))
                return@setOnAction
                
            callback.accept(newInput)
        }

        val btnOK = uiFactory.newButton(localizedStringProperty("dialog.ok"))

        field.textProperty().addListener { _, _, newInput ->
            btnOK.isDisable = newInput.isEmpty() || !filter.test(newInput)
        }

        btnOK.isDisable = true
        btnOK.setOnAction {
{'fraction_non_alphanumeric': 0.10198111953197712, 'fraction_numerical': 0.005318441696582901, 'mean_word_length': 2.6426150121065377, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3959513', 'n_tokens_mistral': 2211, 'n_tokens_neox': 2106, 'n_words': 450}