From 8e25dcd396b303160bbf6523cb63f117f9af9987 Mon Sep 17 00:00:00 2001 From: Jan-Peter Klein Date: Tue, 22 Oct 2024 14:56:56 +0200 Subject: [PATCH] implement CustomDialogBuilder --- .../org/cryptomator/ui/common/FxmlFile.java | 1 + .../ui/controls/CustomDialogBuilder.java | 49 +++++++++++++++ .../ui/controls/CustomDialogController.java | 61 +++++++++++++++++++ .../ui/mainwindow/VaultListController.java | 13 +++- .../SupporterCertificateController.java | 18 ++++-- src/main/resources/fxml/custom_dialog.fxml | 51 ++++++++++++++++ 6 files changed, 187 insertions(+), 6 deletions(-) create mode 100644 src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java create mode 100644 src/main/java/org/cryptomator/ui/controls/CustomDialogController.java create mode 100644 src/main/resources/fxml/custom_dialog.fxml diff --git a/src/main/java/org/cryptomator/ui/common/FxmlFile.java b/src/main/java/org/cryptomator/ui/common/FxmlFile.java index 6c5068684..2ad072992 100644 --- a/src/main/java/org/cryptomator/ui/common/FxmlFile.java +++ b/src/main/java/org/cryptomator/ui/common/FxmlFile.java @@ -12,6 +12,7 @@ public enum FxmlFile { CONVERTVAULT_HUBTOPASSWORD_START("/fxml/convertvault_hubtopassword_start.fxml"), // CONVERTVAULT_HUBTOPASSWORD_CONVERT("/fxml/convertvault_hubtopassword_convert.fxml"), // CONVERTVAULT_HUBTOPASSWORD_SUCCESS("/fxml/convertvault_hubtopassword_success.fxml"), // + CUSTOM_DIALOG("/fxml/custom_dialog.fxml"), // DOKANY_SUPPORT_END("/fxml/dokany_support_end.fxml"), // ERROR("/fxml/error.fxml"), // FORGET_PASSWORD("/fxml/forget_password.fxml"), // diff --git a/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java b/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java new file mode 100644 index 000000000..3dbb9f68d --- /dev/null +++ b/src/main/java/org/cryptomator/ui/controls/CustomDialogBuilder.java @@ -0,0 +1,49 @@ +package org.cryptomator.ui.controls; + +import org.cryptomator.ui.common.FxmlFile; +import org.cryptomator.ui.quit.QuitComponent; + +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; + +public class CustomDialogBuilder { + + public CustomDialogBuilder() { + } + + public void showDialog(ResourceBundle resourceBundle, Stage owner, FontAwesome5Icon icon, String title, String message, String description, Runnable okAction, String okText) { + try { + FXMLLoader loader = new FXMLLoader(getResource(FxmlFile.CUSTOM_DIALOG), resourceBundle); + Pane pane = loader.load(); + + CustomDialogController controller = loader.getController(); + controller.setIcon(icon); + controller.setMessage(message); + controller.setDescription(description); + controller.setOkButtonText(okText); + controller.setOkAction(okAction); + + 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); + + dialogStage.showAndWait(); + } catch (Exception e) { + e.printStackTrace(); // Handle loading errors + } + } + + private URL getResource(FxmlFile fxmlFile) { + return getClass().getResource(fxmlFile.getRessourcePathString()); + } + +} diff --git a/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java b/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java new file mode 100644 index 000000000..fb1536fa6 --- /dev/null +++ b/src/main/java/org/cryptomator/ui/controls/CustomDialogController.java @@ -0,0 +1,61 @@ +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 { + + @FXML + private Label message; + @FXML + private Label description; + @FXML + private FontAwesome5IconView icon; + @FXML + private Button okButton; + + private Stage dialogStage; + private Runnable okAction; + + public void setDialogStage(Stage stage) { + this.dialogStage = stage; + } + + public void setIcon(FontAwesome5Icon glyph) { + icon.setGlyph(glyph); + } + + public void setMessage(String message) { + this.message.setText(message); + } + + public void setDescription(String desc) { + this.description.setText(desc); + } + + public void setOkAction(Runnable action) { + this.okAction = action; + } + + public void setOkButtonText(String text) { + okButton.setText(text); + } + + @FXML + private void handleOk() { + if (okAction != null) { + okAction.run(); + } + dialogStage.close(); + } + + @FXML + private void handleCancel() { + dialogStage.close(); + } + +} diff --git a/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java b/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java index 37b9841d0..5ed607c4d 100644 --- a/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java +++ b/src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java @@ -9,6 +9,8 @@ import org.cryptomator.cryptofs.DirStructure; import org.cryptomator.ui.addvaultwizard.AddVaultWizardComponent; import org.cryptomator.ui.common.FxController; import org.cryptomator.ui.common.VaultService; +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; @@ -208,7 +210,16 @@ 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())) { - removeVaultDialogue.vault(vault).build().showRemoveVault(); + new CustomDialogBuilder().showDialog(resourceBundle, mainWindow, // + FontAwesome5Icon.CROWN, // + String.format(resourceBundle.getString("removeVault.title"), vault.getDisplayName()), // + resourceBundle.getString("removeVault.message"), // + resourceBundle.getString("removeVault.description"), // + () -> { + vaults.remove(vault); + LOG.debug("Removing vault {}.", vault.getDisplayName()); + }, // + resourceBundle.getString("removeVault.confirmBtn")); } } diff --git a/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java b/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java index e587e5f27..6ed5d045f 100644 --- a/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java +++ b/src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java @@ -5,7 +5,8 @@ import org.cryptomator.common.LicenseHolder; import org.cryptomator.common.settings.Settings; import org.cryptomator.common.settings.UiTheme; import org.cryptomator.ui.common.FxController; -import org.cryptomator.ui.removecert.RemoveCertComponent; +import org.cryptomator.ui.controls.CustomDialogBuilder; +import org.cryptomator.ui.controls.FontAwesome5Icon; import javax.inject.Inject; import javafx.application.Application; @@ -14,6 +15,7 @@ import javafx.fxml.FXML; import javafx.scene.control.TextArea; import javafx.scene.control.TextFormatter; import javafx.stage.Stage; +import java.util.ResourceBundle; @PreferencesScoped public class SupporterCertificateController implements FxController { @@ -26,18 +28,18 @@ public class SupporterCertificateController implements FxController { private final Stage window; private final LicenseHolder licenseHolder; private final Settings settings; - private final RemoveCertComponent.Builder removeCert; + private final ResourceBundle resourceBundle; @FXML private TextArea supporterCertificateField; @Inject - SupporterCertificateController(Application application, @PreferencesWindow Stage window, LicenseHolder licenseHolder, Settings settings, RemoveCertComponent.Builder removeCert) { + SupporterCertificateController(Application application, @PreferencesWindow Stage window, LicenseHolder licenseHolder, Settings settings, ResourceBundle resourceBundle) { this.application = application; this.window = window; this.licenseHolder = licenseHolder; this.settings = settings; - this.removeCert = removeCert; + this.resourceBundle = resourceBundle; } @FXML @@ -84,7 +86,13 @@ public class SupporterCertificateController implements FxController { @FXML void didClickRemoveCert() { - removeCert.build().showRemoveCert(window); + 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")); } public LicenseHolder getLicenseHolder() { diff --git a/src/main/resources/fxml/custom_dialog.fxml b/src/main/resources/fxml/custom_dialog.fxml new file mode 100644 index 000000000..1a3b0c464 --- /dev/null +++ b/src/main/resources/fxml/custom_dialog.fxml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +