changed resolveText() to throw Exceptions

removed fxApplication scope
This commit is contained in:
Jan-Peter Klein
2024-11-14 16:19:50 +01:00
parent 18ca563865
commit c471289fb5
2 changed files with 10 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.util.IllegalFormatException;
import java.util.ResourceBundle;
import java.util.function.Consumer;
@@ -19,7 +20,7 @@ public class CustomDialog {
private static final Logger LOG = LoggerFactory.getLogger(CustomDialog.class);
private final ResourceBundle resourceBundle;
private Stage dialogStage;
private final Stage dialogStage;
CustomDialog(Builder builder) {
this.resourceBundle = builder.resourceBundle;
@@ -56,10 +57,14 @@ public class CustomDialog {
}
private String resolveText(String key, String[] args) {
String text = (key == null || key.isEmpty() || !resourceBundle.containsKey(key))
? String.format("N/A - Key '%s' not found", key)
: resourceBundle.getString(key);
return args != null && args.length > 0 ? String.format(text, (Object[]) args) : text;
if (key == null || key.isEmpty() || !resourceBundle.containsKey(key)) {
throw new IllegalArgumentException(String.format("Invalid key: '%s'. Key not found in ResourceBundle.", key)); }
String text = resourceBundle.getString(key);
try {
return args != null && args.length > 0 ? String.format(text, (Object[]) args) : text;
} catch (IllegalFormatException e) {
throw new IllegalArgumentException("Formatting error: Check if arguments match placeholders in the text.", e);
}
}
public static class Builder {
@@ -77,7 +82,6 @@ public class CustomDialog {
private Consumer<Stage> okAction = Stage::close;
private Consumer<Stage> cancelAction = Stage::close;
@Inject
public Builder(ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
}

View File

@@ -73,7 +73,6 @@ abstract class FxApplicationModule {
}
@Provides
@FxApplicationScoped
static CustomDialog.Builder provideCustomDialog(ResourceBundle resourceBundle) {
return new CustomDialog.Builder(resourceBundle);
}