From 7fcbb57ab1f54394e84f60756904872b4abeba4b Mon Sep 17 00:00:00 2001 From: Jan-Peter Klein Date: Tue, 22 Oct 2024 17:07:42 +0200 Subject: [PATCH] used builder pattern --- .../ui/controls/CustomDialogBuilder.java | 80 ++++++++++++++----- .../ui/controls/CustomDialogController.java | 53 ++++++------ .../ui/mainwindow/VaultListController.java | 24 +++--- .../SupporterCertificateController.java | 20 +++-- src/main/resources/fxml/custom_dialog.fxml | 8 +- 5 files changed, 119 insertions(+), 66 deletions(-) diff --git a/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java b/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java index 3dbb9f68d..180dfee1d 100644 --- a/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java +++ b/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java @@ -1,49 +1,93 @@ package org.cryptomator.ui.controls; import org.cryptomator.ui.common.FxmlFile; -import org.cryptomator.ui.quit.QuitComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.stage.Modality; import javafx.stage.Stage; -import java.net.URL; -import java.util.ResourceBundle; +import java.util.function.Consumer; public class CustomDialogBuilder { - public CustomDialogBuilder() { + private static final Logger LOG = LoggerFactory.getLogger(CustomDialogBuilder.class); + + private String title; + private String message; + private String description; + private FontAwesome5Icon icon; + private String okButtonText = "OK"; + private String cancelButtonText = "Cancel"; + private Consumer okAction; + private Consumer cancelAction; + + public CustomDialogBuilder setTitle(String title) { + this.title = title; + return this; } - public void showDialog(ResourceBundle resourceBundle, Stage owner, FontAwesome5Icon icon, String title, String message, String description, Runnable okAction, String okText) { + public CustomDialogBuilder setMessage(String message) { + this.message = message; + return this; + } + + public CustomDialogBuilder setDescription(String description) { + this.description = description; + return this; + } + + public CustomDialogBuilder setIcon(FontAwesome5Icon icon) { + this.icon = icon; + return this; + } + + public CustomDialogBuilder setOkButtonText(String okButtonText) { + this.okButtonText = okButtonText; + return this; + } + + public CustomDialogBuilder setCancelButtonText(String cancelButtonText) { + this.cancelButtonText = cancelButtonText; + return this; + } + + public CustomDialogBuilder setOkAction(Consumer okAction) { + this.okAction = okAction; + return this; + } + + public CustomDialogBuilder setCancelAction(Consumer cancelAction) { + this.cancelAction = cancelAction; + return this; + } + + + public void buildAndShow(Stage owner) { try { - FXMLLoader loader = new FXMLLoader(getResource(FxmlFile.CUSTOM_DIALOG), resourceBundle); + FXMLLoader loader = new FXMLLoader(getClass().getResource(FxmlFile.CUSTOM_DIALOG.getRessourcePathString())); Pane pane = loader.load(); CustomDialogController controller = loader.getController(); - controller.setIcon(icon); controller.setMessage(message); controller.setDescription(description); - controller.setOkButtonText(okText); - controller.setOkAction(okAction); + controller.setIcon(icon); + controller.setOkButtonText(okButtonText); + controller.setCancelButtonText(cancelButtonText); Stage dialogStage = new Stage(); dialogStage.setTitle(title); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.initOwner(owner); dialogStage.setScene(new Scene(pane)); - dialogStage.setResizable(false); - controller.setDialogStage(dialogStage); + controller.setOkAction(() -> okAction.accept(dialogStage)); + controller.setCancelAction(() -> cancelAction.accept(dialogStage)); dialogStage.showAndWait(); } catch (Exception e) { - e.printStackTrace(); // Handle loading errors + LOG.error("Failed to build and show dialog stage.", e); } } - - private URL getResource(FxmlFile fxmlFile) { - return getClass().getResource(fxmlFile.getRessourcePathString()); - } - -} +} \ No newline at end of file diff --git a/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java b/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java index fb1536fa6..7308bb0b3 100644 --- a/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java +++ b/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java @@ -1,48 +1,51 @@ package org.cryptomator.ui.controls; -import org.cryptomator.ui.common.FxController; - import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.stage.Stage; -public class CustomDialogController implements FxController { +public class CustomDialogController { @FXML - private Label message; + private Label messageLabel; @FXML - private Label description; + private Label descriptionLabel; @FXML - private FontAwesome5IconView icon; + private FontAwesome5IconView iconView; @FXML private Button okButton; + @FXML + private Button cancelButton; - private Stage dialogStage; private Runnable okAction; - - public void setDialogStage(Stage stage) { - this.dialogStage = stage; - } - - public void setIcon(FontAwesome5Icon glyph) { - icon.setGlyph(glyph); - } + private Runnable cancelAction; public void setMessage(String message) { - this.message.setText(message); + messageLabel.setText(message); } - public void setDescription(String desc) { - this.description.setText(desc); + public void setDescription(String description) { + descriptionLabel.setText(description); + } + + public void setIcon(FontAwesome5Icon icon) { + iconView.setGlyph(icon); + } + + public void setOkButtonText(String text) { + okButton.setText(text); + } + + public void setCancelButtonText(String text) { + cancelButton.setText(text); } public void setOkAction(Runnable action) { this.okAction = action; } - public void setOkButtonText(String text) { - okButton.setText(text); + public void setCancelAction(Runnable action) { + this.cancelAction = action; } @FXML @@ -50,12 +53,12 @@ public class CustomDialogController implements FxController { if (okAction != null) { okAction.run(); } - dialogStage.close(); } @FXML private void handleCancel() { - dialogStage.close(); + if (cancelAction != null) { + cancelAction.run(); + } } - -} +} \ No newline at end of file diff --git a/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java b/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java index 5ed607c4d..7a75186ce 100644 --- a/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java +++ b/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java @@ -13,7 +13,6 @@ import org.cryptomator.ui.controls.CustomDialogBuilder; import org.cryptomator.ui.controls.FontAwesome5Icon; import org.cryptomator.ui.fxapp.FxApplicationWindows; import org.cryptomator.ui.preferences.SelectedPreferencesTab; -import org.cryptomator.ui.removevault.RemoveVaultComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,7 +67,6 @@ public class VaultListController implements FxController { private final VaultListCellFactory cellFactory; private final AddVaultWizardComponent.Builder addVaultWizard; private final BooleanBinding emptyVaultList; - private final RemoveVaultComponent.Builder removeVaultDialogue; private final VaultListManager vaultListManager; private final BooleanProperty draggingVaultOver = new SimpleBooleanProperty(); private final ResourceBundle resourceBundle; @@ -88,7 +86,6 @@ public class VaultListController implements FxController { VaultListCellFactory cellFactory, // VaultService vaultService, // AddVaultWizardComponent.Builder addVaultWizard, // - RemoveVaultComponent.Builder removeVaultDialogue, // VaultListManager vaultListManager, // ResourceBundle resourceBundle, // FxApplicationWindows appWindows, // @@ -99,7 +96,6 @@ public class VaultListController implements FxController { this.cellFactory = cellFactory; this.vaultService = vaultService; this.addVaultWizard = addVaultWizard; - this.removeVaultDialogue = removeVaultDialogue; this.vaultListManager = vaultListManager; this.resourceBundle = resourceBundle; this.appWindows = appWindows; @@ -210,16 +206,20 @@ public class VaultListController implements FxController { private void pressedShortcutToRemoveVault() { final var vault = selectedVault.get(); if (vault != null && EnumSet.of(LOCKED, MISSING, ERROR, NEEDS_MIGRATION).contains(vault.getState())) { - new CustomDialogBuilder().showDialog(resourceBundle, mainWindow, // - FontAwesome5Icon.CROWN, // - String.format(resourceBundle.getString("removeVault.title"), vault.getDisplayName()), // - resourceBundle.getString("removeVault.message"), // - resourceBundle.getString("removeVault.description"), // - () -> { + new CustomDialogBuilder() // + .setTitle(String.format(resourceBundle.getString("removeVault.title"), vault.getDisplayName())) // + .setMessage(resourceBundle.getString("removeVault.message")) // + .setDescription(resourceBundle.getString("removeVault.description")) // + .setIcon(FontAwesome5Icon.QUESTION) // + .setOkButtonText(resourceBundle.getString("removeVault.confirmBtn")) // + .setCancelButtonText(resourceBundle.getString("generic.button.cancel")) // + .setOkAction(v -> { vaults.remove(vault); LOG.debug("Removing vault {}.", vault.getDisplayName()); - }, // - resourceBundle.getString("removeVault.confirmBtn")); + v.close(); + }) // + .setCancelAction(Stage::close) // + .buildAndShow(mainWindow); } } diff --git a/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java b/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java index 6ed5d045f..b7e5872ea 100644 --- a/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java +++ b/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java @@ -86,13 +86,19 @@ public class SupporterCertificateController implements FxController { @FXML void didClickRemoveCert() { - new CustomDialogBuilder().showDialog(resourceBundle, window, // - FontAwesome5Icon.QUESTION, // - resourceBundle.getString("removeCert.title"), // - resourceBundle.getString("removeCert.message"), // - resourceBundle.getString("removeCert.description"), // - () -> settings.licenseKey.set(null), // - resourceBundle.getString("removeCert.confirmBtn")); + new CustomDialogBuilder() + .setTitle(resourceBundle.getString("removeCert.title")) + .setMessage(resourceBundle.getString("removeCert.message")) + .setDescription(resourceBundle.getString("removeCert.description")) + .setIcon(FontAwesome5Icon.BUG) + .setOkButtonText(resourceBundle.getString("removeCert.confirmBtn")) + .setCancelButtonText(resourceBundle.getString("generic.button.cancel")) + .setOkAction(v -> { + settings.licenseKey.set(null); + v.close(); + }) + .setCancelAction(Stage::close) + .buildAndShow(window); } public LicenseHolder getLicenseHolder() { diff --git a/src/main/resources/fxml/custom_dialog.fxml b/src/main/resources/fxml/custom_dialog.fxml index 1a3b0c464..4660872bc 100644 --- a/src/main/resources/fxml/custom_dialog.fxml +++ b/src/main/resources/fxml/custom_dialog.fxml @@ -28,21 +28,21 @@ - + -