diff --git a/EpcQrCodeJavaFxApp/build.gradle.kts b/EpcQrCodeJavaFxApp/build.gradle.kts index c98fb6c..c24e7bf 100644 --- a/EpcQrCodeJavaFxApp/build.gradle.kts +++ b/EpcQrCodeJavaFxApp/build.gradle.kts @@ -32,7 +32,7 @@ tasks.withType() { dependencies { implementation(kotlin("stdlib-jdk8")) - implementation(project(":EpcQrCode")) + api(project(":EpcQrCode")) implementation("net.dankito.utils:java-fx-utils:1.0.8") } diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt index 1b97ea2..3c9168c 100644 --- a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt @@ -1,32 +1,137 @@ package net.codinux.banking.epcqrcode.windows.main import javafx.beans.property.SimpleObjectProperty +import javafx.beans.property.SimpleStringProperty +import javafx.geometry.Pos +import javafx.scene.control.TextFormatter import javafx.scene.image.Image -import tornadofx.* -import java.io.ByteArrayInputStream import net.codinux.banking.epcqrcode.* import org.slf4j.LoggerFactory +import tornadofx.* +import java.io.ByteArrayInputStream +import java.text.DecimalFormat +import java.text.ParsePosition class MainWindow : View(FX.messages["application.title"]) { companion object { + private const val FieldHeight = 31.0 + + private val DecimalFormat = DecimalFormat( "#.0" ) + private val logger = LoggerFactory.getLogger(MainWindow::class.java) } - private val qrCodeContent = EpcQrCodeCreator().generateAsString(CreatorParam("Mahatma Gandhi", "IN00123456789876543210", null, 1234.56, "Struggle for independence")) // TODO: remove again + private val receiver = SimpleStringProperty("") + + private val iban = SimpleStringProperty("") + + private val bic = SimpleStringProperty("") + + private val amount = SimpleStringProperty("") + + private val reference = SimpleStringProperty("") + private val qrCodeGenerator = QrCodeGenerator() - private val qrCode = SimpleObjectProperty(generateQrCode(qrCodeContent)) + private val generatedQrCode = SimpleObjectProperty() override val root = vbox { - imageview(qrCode) + prefWidth = 350.0 + + form { + fieldset { + field(messages["Receiver"]) { + textfield(receiver) { + prefHeight = FieldHeight + } + } + + field(messages["IBAN"]) { + textfield(iban) { + prefHeight = FieldHeight + } + } + + field(messages["BIC"]) { + textfield(bic) { + prefHeight = FieldHeight + } + } + + field(messages["Amount"]) { + textfield(amount) { + prefHeight = FieldHeight + + setTextFormatter(TextFormatter { change -> + isDecimalNumberEntered(change) + }) + } + } + + field(messages["Reference"]) { + textfield(reference) { + prefHeight = FieldHeight + } + } + } + + hbox(alignment = Pos.CENTER_RIGHT) { + button(messages["Generate"]) { + prefWidth = 200.0 + minHeight = 36.0 + + action { generateQrCode() } + } + } + } + + hbox(alignment = Pos.CENTER) { + imageview(generatedQrCode) { + hboxConstraints { + marginTop = 4.0 + } + } + } } + private fun isDecimalNumberEntered(change: TextFormatter.Change): TextFormatter.Change? { + val newText = change.controlNewText + + if (newText.isEmpty()) { + return change + } + + val parsePosition = ParsePosition(0) + val parsedNumber = DecimalFormat.parse(newText, parsePosition) + + return if (parsedNumber == null || parsePosition.index < newText.length) { + null + } + else { + change + } + } + + + private fun generateQrCode() { + val param = CreatorParam(receiver.value, iban.value, bic.value, map(amount.value), reference.value) + val qrCodeContent = EpcQrCodeCreator().generateAsString(param) + + generatedQrCode.value = generateQrCode(qrCodeContent) + + currentStage?.sizeToScene() + } + + private fun map(amount: String): Double? { + return DecimalFormat.parse(amount, ParsePosition(0))?.toDouble() + } + private fun generateQrCode(informationToEncode: String): Image? { try { val qrCodeBytes = qrCodeGenerator.generateQrCode(informationToEncode) diff --git a/EpcQrCodeJavaFxApp/src/main/resources/Messages.properties b/EpcQrCodeJavaFxApp/src/main/resources/Messages.properties index 057c0e9..86e5366 100755 --- a/EpcQrCodeJavaFxApp/src/main/resources/Messages.properties +++ b/EpcQrCodeJavaFxApp/src/main/resources/Messages.properties @@ -1,4 +1,4 @@ -application.title=EPC QR Code +application.title=EPC QR Code Generator ok=OK @@ -6,4 +6,12 @@ cancel=Cancel main.window.menu.file=File -main.window.menu.file.quit=Quit \ No newline at end of file +main.window.menu.file.quit=Quit + +Receiver=Receiver +IBAN=IBAN +BIC=BIC +Amount=Amount +Reference=Reference + +Generate=Generate \ No newline at end of file diff --git a/EpcQrCodeJavaFxApp/src/main/resources/Messages_de.properties b/EpcQrCodeJavaFxApp/src/main/resources/Messages_de.properties index 90377ab..1e358be 100755 --- a/EpcQrCodeJavaFxApp/src/main/resources/Messages_de.properties +++ b/EpcQrCodeJavaFxApp/src/main/resources/Messages_de.properties @@ -6,4 +6,12 @@ cancel=Abbrechen main.window.menu.file=Datei -main.window.menu.file.quit=Beenden \ No newline at end of file +main.window.menu.file.quit=Beenden + +Receiver=Empfänger +IBAN=IBAN +BIC=BIC +Amount=Betrag +Reference=Verwendungszweck + +Generate=Erstellen \ No newline at end of file