Parcourir la source

refactor dialogs with injected resourceBundle for builder creation

Jan-Peter Klein il y a 2 mois
Parent
commit
7cbf1e10e5

+ 26 - 14
src/main/java/org/cryptomator/ui/dialogs/Dialogs.java

@@ -6,17 +6,30 @@ import org.cryptomator.ui.controls.FontAwesome5Icon;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.inject.Inject;
 import javafx.collections.ObservableList;
 import javafx.stage.Stage;
+import java.util.ResourceBundle;
 import java.util.function.Consumer;
 
 
 public class Dialogs {
 
+	private final ResourceBundle resourceBundle;
+
+	@Inject
+	public Dialogs(ResourceBundle resourceBundle) {
+		this.resourceBundle = resourceBundle;
+	}
+
 	private static final Logger LOG = LoggerFactory.getLogger(Dialogs.class);
 
-	public static SimpleDialog buildRemoveVaultDialog(SimpleDialog.Builder simpleDialogBuilder, Stage window, Vault vault, ObservableList<Vault> vaults) {
-		return simpleDialogBuilder.setOwner(window) //
+	private SimpleDialog.Builder createDialogBuilder() {
+		return new SimpleDialog.Builder(resourceBundle);
+	}
+
+	public SimpleDialog.Builder prepareRemoveVaultDialog(Stage window, Vault vault, ObservableList<Vault> vaults) {
+		return createDialogBuilder().setOwner(window) //
 				.setTitleKey("removeVault.title", vault.getDisplayName()) //
 				.setMessageKey("removeVault.message") //
 				.setDescriptionKey("removeVault.description") //
@@ -27,27 +40,27 @@ public class Dialogs {
 					LOG.debug("Removing vault {}.", vault.getDisplayName());
 					vaults.remove(vault);
 					stage.close();
-				}) //
-				.build();
+				});
 	}
 
-	public static SimpleDialog buildRemoveCertDialog(SimpleDialog.Builder simpleDialogBuilder, Stage window, Settings settings) {
-		return simpleDialogBuilder.setOwner(window) //
+	public SimpleDialog.Builder prepareRemoveCertDialog(Stage window, Settings settings) {
+		return createDialogBuilder() //
+				.setOwner(window) //
 				.setTitleKey("removeCert.title") //
 				.setMessageKey("removeCert.message") //
 				.setDescriptionKey("removeCert.description") //
 				.setIcon(FontAwesome5Icon.QUESTION) //
 				.setOkButtonKey("removeCert.confirmBtn") //
-				.setCancelButtonKey("generic.button.cancel").setOkAction(stage -> {
+				.setCancelButtonKey("generic.button.cancel") //
+				.setOkAction(stage -> {
 					settings.licenseKey.set(null);
 					stage.close();
-				}) //
-				.setCancelAction(Stage::close) //
-				.build();
+				});
 	}
 
-	public static SimpleDialog buildDokanySupportEndDialog(SimpleDialog.Builder simpleDialogBuilder, Stage window, Consumer<Stage> cancelAction) {
-		return simpleDialogBuilder.setOwner(window) //
+	public SimpleDialog.Builder prepareDokanySupportEndDialog(Stage window, Consumer<Stage> cancelAction) {
+		return createDialogBuilder() //
+				.setOwner(window) //
 				.setTitleKey("dokanySupportEnd.title") //
 				.setMessageKey("dokanySupportEnd.message") //
 				.setDescriptionKey("dokanySupportEnd.description") //
@@ -55,8 +68,7 @@ public class Dialogs {
 				.setOkButtonKey("generic.button.close") //
 				.setCancelButtonKey("dokanySupportEnd.preferencesBtn") //
 				.setOkAction(Stage::close) //
-				.setCancelAction(cancelAction) //
-				.build();
+				.setCancelAction(cancelAction);
 	}
 
 }

+ 4 - 3
src/main/java/org/cryptomator/ui/fxapp/FxApplicationModule.java

@@ -7,7 +7,7 @@ package org.cryptomator.ui.fxapp;
 
 import dagger.Module;
 import dagger.Provides;
-import org.cryptomator.ui.dialogs.SimpleDialog;
+import org.cryptomator.ui.dialogs.Dialogs;
 import org.cryptomator.ui.error.ErrorComponent;
 import org.cryptomator.ui.health.HealthCheckComponent;
 import org.cryptomator.ui.lock.LockComponent;
@@ -69,8 +69,9 @@ abstract class FxApplicationModule {
 	}
 
 	@Provides
-	static SimpleDialog.Builder provideSimpleDialog(ResourceBundle resourceBundle) {
-		return new SimpleDialog.Builder(resourceBundle);
+	@FxApplicationScoped
+	static Dialogs provideDialogs(ResourceBundle resourceBundle) {
+		return new Dialogs(resourceBundle);
 	}
 
 }

+ 10 - 14
src/main/java/org/cryptomator/ui/fxapp/FxApplicationWindows.java

@@ -6,7 +6,6 @@ import org.cryptomator.common.vaults.Vault;
 import org.cryptomator.common.vaults.VaultState;
 import org.cryptomator.integrations.tray.TrayIntegrationProvider;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 import org.cryptomator.ui.error.ErrorComponent;
 import org.cryptomator.ui.lock.LockComponent;
 import org.cryptomator.ui.mainwindow.MainWindowComponent;
@@ -57,7 +56,7 @@ public class FxApplicationWindows {
 	private final VaultOptionsComponent.Factory vaultOptionsWindow;
 	private final ShareVaultComponent.Factory shareVaultWindow;
 	private final FilteredList<Window> visibleWindows;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
+	private final Dialogs dialogs;
 
 	@Inject
 	public FxApplicationWindows(@PrimaryStage Stage primaryStage, //
@@ -72,7 +71,7 @@ public class FxApplicationWindows {
 								VaultOptionsComponent.Factory vaultOptionsWindow, //
 								ShareVaultComponent.Factory shareVaultWindow, //
 								ExecutorService executor, //
-								Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+								Provider<Dialogs> dialogsProvider) {
 		this.primaryStage = primaryStage;
 		this.trayIntegration = trayIntegration;
 		this.mainWindow = mainWindow;
@@ -86,7 +85,7 @@ public class FxApplicationWindows {
 		this.vaultOptionsWindow = vaultOptionsWindow;
 		this.shareVaultWindow = shareVaultWindow;
 		this.visibleWindows = Window.getWindows().filtered(Window::isShowing);
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 	}
 
 	public void initialize() {
@@ -149,16 +148,13 @@ public class FxApplicationWindows {
 	}
 
 	public void showDokanySupportEndWindow() {
-		CompletableFuture.runAsync(() -> {
-			Dialogs.buildDokanySupportEndDialog(
-					simpleDialogBuilder.get(),
-					mainWindow.get().window(),
-					stage -> {
-						showPreferencesWindow(SelectedPreferencesTab.VOLUME);
-						stage.close();
-					}
-			).showAndWait();
-		}, Platform::runLater);
+		CompletableFuture.runAsync(() -> dialogs.prepareDokanySupportEndDialog(
+				mainWindow.get().window(),
+				stage -> {
+					showPreferencesWindow(SelectedPreferencesTab.VOLUME);
+					stage.close();
+				}
+		).build().showAndWait(), Platform::runLater);
 	}
 
 	public CompletionStage<Void> startUnlockWorkflow(Vault vault, @Nullable Stage owner) {

+ 4 - 5
src/main/java/org/cryptomator/ui/mainwindow/VaultDetailMissingVaultController.java

@@ -4,7 +4,6 @@ import org.cryptomator.common.vaults.Vault;
 import org.cryptomator.common.vaults.VaultListManager;
 import org.cryptomator.ui.common.FxController;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 
 import javax.inject.Inject;
 import javax.inject.Provider;
@@ -25,19 +24,19 @@ public class VaultDetailMissingVaultController implements FxController {
 	private final ObservableList<Vault> vaults;
 	private final ResourceBundle resourceBundle;
 	private final Stage window;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
+	private final Dialogs dialogs;
 
 	@Inject
 	public VaultDetailMissingVaultController(ObjectProperty<Vault> vault, //
 											 ObservableList<Vault> vaults, //
 											 ResourceBundle resourceBundle, //
 											 @MainWindow Stage window, //
-											 Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+											 Provider<Dialogs> dialogsProvider) {
 		this.vault = vault;
 		this.vaults = vaults;
 		this.resourceBundle = resourceBundle;
 		this.window = window;
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 	}
 
 	@FXML
@@ -47,7 +46,7 @@ public class VaultDetailMissingVaultController implements FxController {
 
 	@FXML
 	void didClickRemoveVault() {
-		Dialogs.buildRemoveVaultDialog(simpleDialogBuilder.get(),window,vault.get(),vaults).showAndWait();
+		dialogs.prepareRemoveVaultDialog(window, vault.get(), vaults).build().showAndWait();
 	}
 
 	@FXML

+ 4 - 5
src/main/java/org/cryptomator/ui/mainwindow/VaultDetailUnknownErrorController.java

@@ -4,7 +4,6 @@ import org.cryptomator.common.vaults.Vault;
 import org.cryptomator.common.vaults.VaultListManager;
 import org.cryptomator.ui.common.FxController;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 import org.cryptomator.ui.fxapp.FxApplicationWindows;
 
 import javax.inject.Inject;
@@ -23,7 +22,7 @@ public class VaultDetailUnknownErrorController implements FxController {
 	private final Stage errorWindow;
 	private final ObservableList<Vault> vaults;
 	private final Stage mainWindow;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
+	private final Dialogs dialogs;
 
 	@Inject
 	public VaultDetailUnknownErrorController(@MainWindow Stage mainWindow, //
@@ -31,13 +30,13 @@ public class VaultDetailUnknownErrorController implements FxController {
 											 ObservableList<Vault> vaults, //
 											 FxApplicationWindows appWindows, //
 											 @Named("errorWindow") Stage errorWindow, //
-											 Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+											 Provider<Dialogs> dialogsProvider) {
 		this.mainWindow = mainWindow;
 		this.vault = vault;
 		this.vaults = vaults;
 		this.appWindows = appWindows;
 		this.errorWindow = errorWindow;
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 	}
 
 	@FXML
@@ -52,6 +51,6 @@ public class VaultDetailUnknownErrorController implements FxController {
 
 	@FXML
 	void didClickRemoveVault() {
-		Dialogs.buildRemoveVaultDialog(simpleDialogBuilder.get(),mainWindow,vault.get(),vaults).showAndWait();
+		dialogs.prepareRemoveVaultDialog(mainWindow, vault.get(), vaults).build().showAndWait();
 	}
 }

+ 4 - 6
src/main/java/org/cryptomator/ui/mainwindow/VaultListContextMenuController.java

@@ -6,7 +6,6 @@ import org.cryptomator.common.vaults.VaultState;
 import org.cryptomator.ui.common.FxController;
 import org.cryptomator.ui.common.VaultService;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 import org.cryptomator.ui.fxapp.FxApplicationWindows;
 import org.cryptomator.ui.vaultoptions.SelectedVaultOptionsTab;
 import org.cryptomator.ui.vaultoptions.VaultOptionsComponent;
@@ -43,8 +42,7 @@ public class VaultListContextMenuController implements FxController {
 	private final ObservableValue<Boolean> selectedVaultUnlockable;
 	private final ObservableValue<Boolean> selectedVaultLockable;
 	private final ObservableList<Vault> vaults;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
-
+	private final Dialogs dialogs;
 
 	@Inject
 	VaultListContextMenuController(ObjectProperty<Vault> selectedVault, //
@@ -54,7 +52,7 @@ public class VaultListContextMenuController implements FxController {
 								   VaultService vaultService, //
 								   KeychainManager keychain, //
 								   VaultOptionsComponent.Factory vaultOptionsWindow, //
-								   Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+								   Provider<Dialogs> dialogsProvider) {
 		this.selectedVault = selectedVault;
 		this.vaults = vaults;
 		this.mainWindow = mainWindow;
@@ -62,7 +60,7 @@ public class VaultListContextMenuController implements FxController {
 		this.vaultService = vaultService;
 		this.keychain = keychain;
 		this.vaultOptionsWindow = vaultOptionsWindow;
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 
 		this.selectedVaultState = selectedVault.flatMap(Vault::stateProperty).orElse(null);
 		this.selectedVaultPassphraseStored = selectedVault.map(this::isPasswordStored).orElse(false);
@@ -78,7 +76,7 @@ public class VaultListContextMenuController implements FxController {
 	@FXML
 	public void didClickRemoveVault() {
 		var vault = Objects.requireNonNull(selectedVault.get());
-		Dialogs.buildRemoveVaultDialog(simpleDialogBuilder.get(),mainWindow,vault,vaults).showAndWait();
+		dialogs.prepareRemoveVaultDialog(mainWindow, vault, vaults).build().showAndWait();
 	}
 
 	@FXML

+ 4 - 5
src/main/java/org/cryptomator/ui/mainwindow/VaultListController.java

@@ -10,7 +10,6 @@ import org.cryptomator.ui.addvaultwizard.AddVaultWizardComponent;
 import org.cryptomator.ui.common.FxController;
 import org.cryptomator.ui.common.VaultService;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 import org.cryptomator.ui.fxapp.FxApplicationWindows;
 import org.cryptomator.ui.preferences.SelectedPreferencesTab;
 import org.slf4j.Logger;
@@ -73,7 +72,7 @@ public class VaultListController implements FxController {
 	private final ResourceBundle resourceBundle;
 	private final FxApplicationWindows appWindows;
 	private final ObservableValue<Double> cellSize;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
+	private final Dialogs dialogs;
 
 	public ListView<Vault> vaultList;
 	public StackPane root;
@@ -93,7 +92,7 @@ public class VaultListController implements FxController {
 						ResourceBundle resourceBundle, //
 						FxApplicationWindows appWindows, //
 						Settings settings, //
-						Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+						Provider<Dialogs> dialogsProvider) {
 		this.mainWindow = mainWindow;
 		this.vaults = vaults;
 		this.selectedVault = selectedVault;
@@ -103,7 +102,7 @@ public class VaultListController implements FxController {
 		this.vaultListManager = vaultListManager;
 		this.resourceBundle = resourceBundle;
 		this.appWindows = appWindows;
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 
 		this.emptyVaultList = Bindings.isEmpty(vaults);
 
@@ -211,7 +210,7 @@ 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())) {
-			Dialogs.buildRemoveVaultDialog(simpleDialogBuilder.get(),mainWindow,vault,vaults).showAndWait();
+			dialogs.prepareRemoveVaultDialog(mainWindow, vault, vaults).build().showAndWait();
 		}
 	}
 

+ 6 - 5
src/main/java/org/cryptomator/ui/preferences/SupporterCertificateController.java

@@ -6,7 +6,6 @@ import org.cryptomator.common.settings.Settings;
 import org.cryptomator.common.settings.UiTheme;
 import org.cryptomator.ui.common.FxController;
 import org.cryptomator.ui.dialogs.Dialogs;
-import org.cryptomator.ui.dialogs.SimpleDialog;
 
 import javax.inject.Inject;
 import javax.inject.Provider;
@@ -17,6 +16,7 @@ import javafx.scene.control.TextArea;
 import javafx.scene.control.TextFormatter;
 import javafx.stage.Stage;
 
+
 @PreferencesScoped
 public class SupporterCertificateController implements FxController {
 
@@ -28,7 +28,7 @@ public class SupporterCertificateController implements FxController {
 	private final Stage window;
 	private final LicenseHolder licenseHolder;
 	private final Settings settings;
-	private final Provider<SimpleDialog.Builder> simpleDialogBuilder;
+	private final Dialogs dialogs;
 
 	@FXML
 	private TextArea supporterCertificateField;
@@ -38,12 +38,12 @@ public class SupporterCertificateController implements FxController {
 								   @PreferencesWindow Stage window,  //
 								   LicenseHolder licenseHolder, //
 								   Settings settings, //
-								   Provider<SimpleDialog.Builder> simpleDialogBuilder) {
+								   Provider<Dialogs> dialogsProvider) {
 		this.application = application;
 		this.window = window;
 		this.licenseHolder = licenseHolder;
 		this.settings = settings;
-		this.simpleDialogBuilder = simpleDialogBuilder;
+		this.dialogs = dialogsProvider.get();
 	}
 
 	@FXML
@@ -90,10 +90,11 @@ public class SupporterCertificateController implements FxController {
 
 	@FXML
 	void didClickRemoveCert() {
-		Dialogs.buildRemoveCertDialog(simpleDialogBuilder.get(),window,settings).showAndWait();
+		dialogs.prepareRemoveCertDialog(window, settings).build().showAndWait();
 	}
 
 	public LicenseHolder getLicenseHolder() {
 		return licenseHolder;
 	}
+
 }