From ec0ce4ea3744279a75aab6a884af0f5b19361743 Mon Sep 17 00:00:00 2001 From: lieght <49651652+BAFGdeJong@users.noreply.github.com> Date: Sat, 29 Nov 2025 12:23:17 +0100 Subject: [PATCH] Added function input for enabling/disabling localization p/text --- .../java/org/toop/app/widget/Primitive.java | 41 +++++++++++++------ .../org/toop/app/widget/view/ServerView.java | 4 +- .../main/java/org/toop/local/AppContext.java | 19 ++++++--- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/org/toop/app/widget/Primitive.java b/app/src/main/java/org/toop/app/widget/Primitive.java index 7dee6c1..ce68e4b 100644 --- a/app/src/main/java/org/toop/app/widget/Primitive.java +++ b/app/src/main/java/org/toop/app/widget/Primitive.java @@ -4,7 +4,6 @@ import javafx.scene.image.ImageView; import org.toop.framework.resource.resources.ImageAsset; import org.toop.local.AppContext; -import java.awt.*; import java.io.File; import java.util.function.Consumer; @@ -23,30 +22,38 @@ import javafx.scene.text.Text; import javafx.util.StringConverter; public final class Primitive { - public static Text header(String key) { + public static Text header(String key, boolean localize) { var header = new Text(); header.getStyleClass().add("header"); if (!key.isEmpty()) { - header.setText(AppContext.getString(key)); - header.textProperty().bind(AppContext.bindToKey(key)); + if (localize) header.setText(AppContext.getString(key)); else header.setText(key); + header.textProperty().bind(AppContext.bindToKey(key, localize)); } return header; } - public static Text text(String key) { + public static Text header(String key) { + return header(key, true); + } + + public static Text text(String key, boolean localize) { var text = new Text(); text.getStyleClass().add("text"); if (!key.isEmpty()) { - text.setText(AppContext.getString(key)); - text.textProperty().bind(AppContext.bindToKey(key)); + if (localize) text.setText(AppContext.getString(key)); else text.setText(key); + text.textProperty().bind(AppContext.bindToKey(key, localize)); } return text; } + public static Text text(String key) { + return text(key, true); + } + public static ImageView image(File file) { ImageAsset imageAsset = new ImageAsset(file); ImageView imageView = new ImageView(imageAsset.getImage()); @@ -57,13 +64,13 @@ public final class Primitive { return imageView; } - public static Button button(String key, Runnable onAction) { + public static Button button(String key, Runnable onAction, boolean localize) { var button = new Button(); button.getStyleClass().add("button"); if (!key.isEmpty()) { - button.setText(AppContext.getString(key)); - button.textProperty().bind(AppContext.bindToKey(key)); + if (localize) button.setText(AppContext.getString(key)); else button.setText(key); + button.textProperty().bind(AppContext.bindToKey(key, localize)); } if (onAction != null) { @@ -74,13 +81,17 @@ public final class Primitive { return button; } - public static TextField input(String promptKey, String text, Consumer onValueChanged) { + public static Button button(String key, Runnable onAction) { + return button(key, onAction, true); + } + + public static TextField input(String promptKey, String text, Consumer onValueChanged, boolean localize) { var input = new TextField(); input.getStyleClass().add("input"); if (!promptKey.isEmpty()) { - input.setPromptText(AppContext.getString(promptKey)); - input.promptTextProperty().bind(AppContext.bindToKey(promptKey)); + if (localize) input.setPromptText(AppContext.getString(promptKey)); else input.setPromptText(promptKey); + input.promptTextProperty().bind(AppContext.bindToKey(promptKey, localize)); } input.setText(text); @@ -93,6 +104,10 @@ public final class Primitive { return input; } + public static TextField input(String promptKey, String text, Consumer onValueChanged) { + return input(promptKey, text, onValueChanged, true); + } + public static Slider slider(int min, int max, int value, Consumer onValueChanged) { var slider = new Slider(); slider.getStyleClass().add("slider"); diff --git a/app/src/main/java/org/toop/app/widget/view/ServerView.java b/app/src/main/java/org/toop/app/widget/view/ServerView.java index d69c036..94da1e2 100644 --- a/app/src/main/java/org/toop/app/widget/view/ServerView.java +++ b/app/src/main/java/org/toop/app/widget/view/ServerView.java @@ -29,7 +29,7 @@ public final class ServerView extends ViewWidget { } private void setupLayout() { - var playerHeader = Primitive.header(user); + var playerHeader = Primitive.header(user, false); var playerListSection = Primitive.vbox( playerHeader, @@ -52,7 +52,7 @@ public final class ServerView extends ViewWidget { listView.getItems().clear(); for (String player : players) { - var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player)); + var playerButton = Primitive.button(player, () -> onPlayerClicked.accept(player), false); listView.getItems().add(playerButton); } }); diff --git a/app/src/main/java/org/toop/local/AppContext.java b/app/src/main/java/org/toop/local/AppContext.java index 0097473..37e9040 100644 --- a/app/src/main/java/org/toop/local/AppContext.java +++ b/app/src/main/java/org/toop/local/AppContext.java @@ -58,10 +58,19 @@ public class AppContext { return "MISSING RESOURCE"; } - public static StringBinding bindToKey(String key) { - return Bindings.createStringBinding( - () -> localization.getString(key, locale), - localeProperty - ); + public static StringBinding bindToKey(String key, boolean localize) { + if (localize) return Bindings.createStringBinding( + () -> localization.getString(key, locale), + localeProperty + ); + + return Bindings.createStringBinding( + () -> key + ); + } + + public static StringBinding bindToKey(String key) { + return bindToKey(key, true); + } } \ No newline at end of file