Changed textfield to only accept Integers, Using scheduled Executor as Timer

This commit is contained in:
Martin Beyer
2021-04-01 11:42:41 +02:00
parent 1c01228778
commit 92de05b3db
5 changed files with 20 additions and 34 deletions

View File

@@ -11,14 +11,7 @@ import com.google.common.io.BaseEncoding;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.property.*;
import java.nio.file.Path;
import java.util.Objects;
@@ -38,7 +31,7 @@ public class VaultSettings {
public static final int DEFAULT_FILENAME_LENGTH_LIMIT = -1;
public static final WhenUnlocked DEFAULT_ACTION_AFTER_UNLOCK = WhenUnlocked.ASK;
public static final boolean DEFAULT_LOCK_AFTER_TIME = false;
public static final String DEFAULT_LOCK_TIME_IN_MINUTES = "30";
public static final int DEFAULT_LOCK_TIME_IN_MINUTES = 30;
private static final Random RNG = new Random();
@@ -55,7 +48,7 @@ public class VaultSettings {
private final IntegerProperty filenameLengthLimit = new SimpleIntegerProperty(DEFAULT_FILENAME_LENGTH_LIMIT);
private final ObjectProperty<WhenUnlocked> actionAfterUnlock = new SimpleObjectProperty<>(DEFAULT_ACTION_AFTER_UNLOCK);
private final BooleanProperty lockAfterTime = new SimpleBooleanProperty(DEFAULT_LOCK_AFTER_TIME);
private final StringProperty lockTimeInMinutes = new SimpleStringProperty(DEFAULT_LOCK_TIME_IN_MINUTES);
private final IntegerProperty lockTimeInMinutes = new SimpleIntegerProperty(DEFAULT_LOCK_TIME_IN_MINUTES);
private final StringBinding mountName;
public VaultSettings(String id) {
@@ -169,7 +162,7 @@ public class VaultSettings {
return lockAfterTime;
}
public StringProperty lockTimeInMinutes() {
public IntegerProperty lockTimeInMinutes() {
return lockTimeInMinutes;
}

View File

@@ -51,7 +51,7 @@ class VaultSettingsJsonAdapter {
int filenameLengthLimit = VaultSettings.DEFAULT_FILENAME_LENGTH_LIMIT;
WhenUnlocked actionAfterUnlock = VaultSettings.DEFAULT_ACTION_AFTER_UNLOCK;
boolean lockAfterTime = VaultSettings.DEFAULT_LOCK_AFTER_TIME;
String lockTimeInMinutes = VaultSettings.DEFAULT_LOCK_TIME_IN_MINUTES;
int lockTimeInMinutes = VaultSettings.DEFAULT_LOCK_TIME_IN_MINUTES;
in.beginObject();
while (in.hasNext()) {
@@ -71,7 +71,7 @@ class VaultSettingsJsonAdapter {
case "filenameLengthLimit" -> filenameLengthLimit = in.nextInt();
case "actionAfterUnlock" -> actionAfterUnlock = parseActionAfterUnlock(in.nextString());
case "lockAfterTime" -> lockAfterTime = in.nextBoolean();
case "lockTimeInMinutes" -> lockTimeInMinutes = in.nextString();
case "lockTimeInMinutes" -> lockTimeInMinutes = in.nextInt();
default -> {
LOG.warn("Unsupported vault setting found in JSON: " + name);
in.skipValue();

View File

@@ -14,7 +14,6 @@ import org.cryptomator.common.settings.Settings;
import org.cryptomator.common.settings.UiTheme;
import org.cryptomator.common.vaults.Vault;
import org.cryptomator.common.vaults.VaultListManager;
import org.cryptomator.common.vaults.Volume;
import org.cryptomator.integrations.tray.TrayIntegrationProvider;
import org.cryptomator.integrations.uiappearance.Theme;
import org.cryptomator.integrations.uiappearance.UiAppearanceException;
@@ -33,10 +32,11 @@ import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Provider;
import java.awt.desktop.QuitResponse;
import java.util.Date;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@FxApplicationScoped
public class FxApplication extends Application {
@@ -57,9 +57,10 @@ public class FxApplication extends Application {
private final BooleanBinding hasVisibleWindows;
private final UiAppearanceListener systemInterfaceThemeListener = this::systemInterfaceThemeChanged;
private final VaultListManager vaultListManager;
private final ScheduledExecutorService scheduledExecutorService;
@Inject
FxApplication(Settings settings, Lazy<MainWindowComponent> mainWindow, Lazy<PreferencesComponent> preferencesWindow, Provider<UnlockComponent.Builder> unlockWindowBuilderProvider, Provider<LockComponent.Builder> lockWindowBuilderProvider, Lazy<QuitComponent> quitWindow, Optional<TrayIntegrationProvider> trayIntegration, Optional<UiAppearanceProvider> appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager) {
FxApplication(Settings settings, Lazy<MainWindowComponent> mainWindow, Lazy<PreferencesComponent> preferencesWindow, Provider<UnlockComponent.Builder> unlockWindowBuilderProvider, Provider<LockComponent.Builder> lockWindowBuilderProvider, Lazy<QuitComponent> quitWindow, Optional<TrayIntegrationProvider> trayIntegration, Optional<UiAppearanceProvider> appearanceProvider, VaultService vaultService, LicenseHolder licenseHolder, VaultListManager vaultListManager, ScheduledExecutorService scheduledExecutorService) {
this.settings = settings;
this.mainWindow = mainWindow;
this.preferencesWindow = preferencesWindow;
@@ -73,6 +74,7 @@ public class FxApplication extends Application {
this.visibleWindows = Stage.getWindows().filtered(Window::isShowing);
this.hasVisibleWindows = Bindings.isNotEmpty(visibleWindows);
this.vaultListManager = vaultListManager;
this.scheduledExecutorService = scheduledExecutorService;
}
public void start() {
@@ -199,19 +201,12 @@ public class FxApplication extends Application {
}
private void checkAutolock(Vault vault, Optional<Stage> owner){
if (vault.getVaultSettings().lockAfterTime().get()){
private void checkAutolock(Vault vault, Optional<Stage> owner) {
if (vault.getVaultSettings().lockAfterTime().get()) {
LOG.info("Locking after {} minutes.", vault.getVaultSettings().lockTimeInMinutes().get());
new java.util.Timer().schedule(
new java.util.TimerTask() {
@Override
public void run() {
startLockWorkflow(vault, owner);
}
},
new Date(System.currentTimeMillis() + (int)(Double.parseDouble(vault.getVaultSettings().lockTimeInMinutes().get()) * 60 * 1000))
);
scheduledExecutorService.schedule(() -> {
startLockWorkflow(vault, owner);
}, (long) (vault.getVaultSettings().lockTimeInMinutes().get()), TimeUnit.MINUTES);
}
}

View File

@@ -5,12 +5,14 @@ import org.cryptomator.ui.common.FxController;
import javax.inject.Inject;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.util.converter.NumberStringConverter;
import java.util.ResourceBundle;
@@ -31,12 +33,11 @@ public class AutoLockVaultOptionsController implements FxController {
@FXML
public void initialize() {
lockAfterTimeCheckbox.selectedProperty().bindBidirectional(vault.getVaultSettings().lockAfterTime());
lockTimeInMinutesTextField.textProperty().bindBidirectional(vault.getVaultSettings().lockTimeInMinutes());
//force the field to be a double with the correct decimal point
Bindings.bindBidirectional(lockTimeInMinutesTextField.textProperty(), vault.getVaultSettings().lockTimeInMinutes(), new NumberStringConverter());
lockTimeInMinutesTextField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d{0,9}([\\.]\\d{0,9})?")) {
if (!newValue.matches("\\d{0,9}")) {
lockTimeInMinutesTextField.setText(newValue.replaceAll("[^\\d]", ""));
}
}

View File

@@ -294,9 +294,6 @@ vaultOptions.masterkey.showRecoveryKeyBtn=Display Recovery Key
vaultOptions.masterkey.recoverPasswordBtn=Recover Password
## Auto Lock
vaultOptions.autoLock=Auto-Lock
vaultOptions.autoLock.lockOnSleep=Lock on sleep
vaultOptions.autoLock.lockAfterIdleTimePart1=Lock after computer is idle for
vaultOptions.autoLock.lockAfterIdleTimePart2=minutes.
vaultOptions.autoLock.lockAfterTimePart1=Lock after
vaultOptions.autoLock.lockAfterTimePart2=minutes.