From a22fbea467dce68e3f969b73a49b1d61fd38d256 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Thu, 5 Nov 2020 11:55:25 +0100 Subject: [PATCH] The display/vault name is now constrained: * should not be empty * is stripped of leading & trailing whitespaces * is truncated to 50 characters --- .../GeneralVaultOptionsController.java | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/GeneralVaultOptionsController.java b/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/GeneralVaultOptionsController.java index 4425139f3..cf356c0c8 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/GeneralVaultOptionsController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/vaultoptions/GeneralVaultOptionsController.java @@ -1,20 +1,26 @@ package org.cryptomator.ui.vaultoptions; +import com.google.common.base.Strings; import org.cryptomator.common.settings.WhenUnlocked; import org.cryptomator.common.vaults.Vault; import org.cryptomator.ui.common.FxController; import javax.inject.Inject; +import javafx.beans.Observable; import javafx.fxml.FXML; import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; import javafx.scene.control.TextField; +import javafx.stage.Stage; import javafx.util.StringConverter; import java.util.ResourceBundle; @VaultOptionsScoped public class GeneralVaultOptionsController implements FxController { + private static final int VAULTNAME_TRUNCATE_THRESHOLD = 50; + + private final Stage window; private final Vault vault; private final ResourceBundle resourceBundle; @@ -23,20 +29,37 @@ public class GeneralVaultOptionsController implements FxController { public ChoiceBox actionAfterUnlockChoiceBox; @Inject - GeneralVaultOptionsController(@VaultOptionsWindow Vault vault, ResourceBundle resourceBundle) { + GeneralVaultOptionsController(@VaultOptionsWindow Stage window, @VaultOptionsWindow Vault vault, ResourceBundle resourceBundle) { + this.window = window; this.vault = vault; this.resourceBundle = resourceBundle; } @FXML public void initialize() { - vaultName.textProperty().bindBidirectional(vault.getVaultSettings().displayName()); + vaultName.textProperty().set(vault.getVaultSettings().displayName().get()); + vaultName.focusedProperty().addListener(this::checkTrimAndTuncateVaultName); unlockOnStartupCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().unlockAfterStartup()); actionAfterUnlockChoiceBox.getItems().addAll(WhenUnlocked.values()); actionAfterUnlockChoiceBox.valueProperty().bindBidirectional(vault.getVaultSettings().actionAfterUnlock()); actionAfterUnlockChoiceBox.setConverter(new WhenUnlockedConverter(resourceBundle)); } + private void checkTrimAndTuncateVaultName(Observable observable, Boolean oldValue, Boolean newValue) { + if (!vaultName.isFocused()) { //only check if TextField loses focus + String currentContent = vaultName.getText().trim(); + if (!Strings.isNullOrEmpty(currentContent)) { + if (currentContent.length() > 50) { + currentContent = currentContent.substring(0, VAULTNAME_TRUNCATE_THRESHOLD); + } + vaultName.textProperty().set(currentContent); + vault.getVaultSettings().displayName().setValue(currentContent); + } else { + vaultName.textProperty().set(vault.getVaultSettings().displayName().get()); + } + } + } + private static class WhenUnlockedConverter extends StringConverter { private final ResourceBundle resourceBundle;