From f7759d254755f1db4d355b1a42d23a5f915f3003 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Wed, 17 Jul 2019 16:17:23 +0200 Subject: [PATCH] hooked up all controls in Preferences window --- .../cryptomator/common/settings/Settings.java | 6 +- .../common/settings/SettingsJsonAdapter.java | 14 +++- .../common/settings/WebDavUrlScheme.java | 37 ++++++++++ .../common/settings/SettingsTest.java | 2 +- .../cryptomator/ui/FxApplicationModule.java | 7 -- .../ui/controllers/SettingsController.java | 4 +- .../ui/controls/NumericTextField.java | 20 ++++++ .../cryptomator/ui/model/WebDavVolume.java | 2 +- .../ui/preferences/PreferencesController.java | 68 +++++++++++++++++++ .../src/main/resources/fxml/preferences.fxml | 20 +++--- 10 files changed, 152 insertions(+), 28 deletions(-) create mode 100644 main/commons/src/main/java/org/cryptomator/common/settings/WebDavUrlScheme.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/controls/NumericTextField.java diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/Settings.java b/main/commons/src/main/java/org/cryptomator/common/settings/Settings.java index 7f57122a3..e345f1779 100644 --- a/main/commons/src/main/java/org/cryptomator/common/settings/Settings.java +++ b/main/commons/src/main/java/org/cryptomator/common/settings/Settings.java @@ -24,7 +24,7 @@ public class Settings { public static final boolean DEFAULT_CHECK_FOR_UDPATES = false; public static final int DEFAULT_PORT = 42427; public static final int DEFAULT_NUM_TRAY_NOTIFICATIONS = 3; - public static final String DEFAULT_GVFS_SCHEME = "dav"; + public static final WebDavUrlScheme DEFAULT_GVFS_SCHEME = WebDavUrlScheme.DAV; public static final boolean DEFAULT_DEBUG_MODE = false; public static final VolumeImpl DEFAULT_PREFERRED_VOLUME_IMPL = System.getProperty("os.name").toLowerCase().contains("windows") ? VolumeImpl.DOKANY : VolumeImpl.FUSE; @@ -33,7 +33,7 @@ public class Settings { private final BooleanProperty checkForUpdates = new SimpleBooleanProperty(DEFAULT_CHECK_FOR_UDPATES); private final IntegerProperty port = new SimpleIntegerProperty(DEFAULT_PORT); private final IntegerProperty numTrayNotifications = new SimpleIntegerProperty(DEFAULT_NUM_TRAY_NOTIFICATIONS); - private final StringProperty preferredGvfsScheme = new SimpleStringProperty(DEFAULT_GVFS_SCHEME); + private final ObjectProperty preferredGvfsScheme = new SimpleObjectProperty<>(DEFAULT_GVFS_SCHEME); private final BooleanProperty debugMode = new SimpleBooleanProperty(DEFAULT_DEBUG_MODE); private final ObjectProperty preferredVolumeImpl = new SimpleObjectProperty<>(DEFAULT_PREFERRED_VOLUME_IMPL); @@ -89,7 +89,7 @@ public class Settings { return numTrayNotifications; } - public StringProperty preferredGvfsScheme() { + public ObjectProperty preferredGvfsScheme() { return preferredGvfsScheme; } diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java b/main/commons/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java index 6c0bf84ae..609096834 100644 --- a/main/commons/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java +++ b/main/commons/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java @@ -31,7 +31,7 @@ public class SettingsJsonAdapter extends TypeAdapter { out.name("checkForUpdatesEnabled").value(value.checkForUpdates().get()); out.name("port").value(value.port().get()); out.name("numTrayNotifications").value(value.numTrayNotifications().get()); - out.name("preferredGvfsScheme").value(value.preferredGvfsScheme().get()); + out.name("preferredGvfsScheme").value(value.preferredGvfsScheme().get().name()); out.name("debugMode").value(value.debugMode().get()); out.name("preferredVolumeImpl").value(value.preferredVolumeImpl().get().name()); out.endObject(); @@ -69,7 +69,7 @@ public class SettingsJsonAdapter extends TypeAdapter { settings.numTrayNotifications().set(in.nextInt()); break; case "preferredGvfsScheme": - settings.preferredGvfsScheme().set(in.nextString()); + settings.preferredGvfsScheme().set(parseWebDavUrlSchemePrefix(in.nextString())); break; case "debugMode": settings.debugMode().set(in.nextBoolean()); @@ -90,12 +90,20 @@ public class SettingsJsonAdapter extends TypeAdapter { private VolumeImpl parsePreferredVolumeImplName(String nioAdapterName) { try { - return VolumeImpl.valueOf(nioAdapterName); + return VolumeImpl.valueOf(nioAdapterName.toUpperCase()); } catch (IllegalArgumentException e) { return Settings.DEFAULT_PREFERRED_VOLUME_IMPL; } } + private WebDavUrlScheme parseWebDavUrlSchemePrefix(String webDavUrlSchemeName) { + try { + return WebDavUrlScheme.valueOf(webDavUrlSchemeName.toUpperCase()); + } catch (IllegalArgumentException e) { + return Settings.DEFAULT_GVFS_SCHEME; + } + } + private List readVaultSettingsArray(JsonReader in) throws IOException { List result = new ArrayList<>(); in.beginArray(); diff --git a/main/commons/src/main/java/org/cryptomator/common/settings/WebDavUrlScheme.java b/main/commons/src/main/java/org/cryptomator/common/settings/WebDavUrlScheme.java new file mode 100644 index 000000000..b34956c90 --- /dev/null +++ b/main/commons/src/main/java/org/cryptomator/common/settings/WebDavUrlScheme.java @@ -0,0 +1,37 @@ +package org.cryptomator.common.settings; + +import java.util.Arrays; + +public enum WebDavUrlScheme { + DAV("dav", "dav:// (Gnome, Nautilus, ...)"), + WEBDAV("webdav", "webdav:// (KDE, Dolphin, ...)"); + + private final String prefix; + private final String displayName; + + WebDavUrlScheme(String prefix, String displayName) {this.prefix = prefix; + this.displayName = displayName; + } + + public String getPrefix() { + return prefix; + } + + public String getDisplayName() { + return displayName; + } + + /** + * Finds a WebDavUrlScheme by prefix. + * + * @param prefix Prefix of the WebDavUrlScheme + * @return WebDavUrlScheme with the given prefix. + * @throws IllegalArgumentException if not WebDavUrlScheme with the given prefix was found. + */ + public static WebDavUrlScheme forPrefix(String prefix) throws IllegalArgumentException { + return Arrays.stream(values()) // + .filter(impl -> impl.prefix.equals(prefix)) // + .findAny() // + .orElseThrow(IllegalArgumentException::new); + } +} diff --git a/main/commons/src/test/java/org/cryptomator/common/settings/SettingsTest.java b/main/commons/src/test/java/org/cryptomator/common/settings/SettingsTest.java index 6aadb27b3..a0c3f6adb 100644 --- a/main/commons/src/test/java/org/cryptomator/common/settings/SettingsTest.java +++ b/main/commons/src/test/java/org/cryptomator/common/settings/SettingsTest.java @@ -23,7 +23,7 @@ public class SettingsTest { Mockito.verify(changeListener, Mockito.times(0)).accept(settings); // first change (to property): - settings.preferredGvfsScheme().set("asd"); + settings.preferredGvfsScheme().set(WebDavUrlScheme.DAV); Mockito.verify(changeListener, Mockito.times(1)).accept(settings); // second change (to list): diff --git a/main/ui/src/main/java/org/cryptomator/ui/FxApplicationModule.java b/main/ui/src/main/java/org/cryptomator/ui/FxApplicationModule.java index a57e09ec5..9a997ed9a 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/FxApplicationModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/FxApplicationModule.java @@ -7,18 +7,11 @@ package org.cryptomator.ui; import dagger.Binds; import dagger.Module; -import dagger.Provides; import javafx.application.Application; -import javafx.stage.Stage; -import javafx.stage.StageStyle; - -import javax.inject.Named; @Module(includes = {UiModule.class}) abstract class FxApplicationModule { - - @Binds @FxApplicationScoped abstract Application provideApplication(FxApplication application); diff --git a/main/ui/src/main/java/org/cryptomator/ui/controllers/SettingsController.java b/main/ui/src/main/java/org/cryptomator/ui/controllers/SettingsController.java index b2af10cbe..b84235632 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/controllers/SettingsController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/controllers/SettingsController.java @@ -111,14 +111,14 @@ public class SettingsController implements ViewController { changePortButton.disableProperty().bind(Bindings.createBooleanBinding(this::isPortValid, portField.textProperty()).not()); prefGvfsScheme.getItems().add("dav"); prefGvfsScheme.getItems().add("webdav"); - prefGvfsScheme.setValue(settings.preferredGvfsScheme().get()); + // prefGvfsScheme.setValue(settings.preferredGvfsScheme().get()); prefGvfsSchemeLabel.setVisible(SystemUtils.IS_OS_LINUX); prefGvfsScheme.setVisible(SystemUtils.IS_OS_LINUX); debugModeCheckbox.setSelected(settings.debugMode().get()); settings.checkForUpdates().bind(checkForUpdatesCheckbox.selectedProperty()); - settings.preferredGvfsScheme().bind(prefGvfsScheme.valueProperty()); + // settings.preferredGvfsScheme().bind(prefGvfsScheme.valueProperty()); settings.preferredVolumeImpl().bind(volume.valueProperty()); settings.debugMode().bind(debugModeCheckbox.selectedProperty()); } diff --git a/main/ui/src/main/java/org/cryptomator/ui/controls/NumericTextField.java b/main/ui/src/main/java/org/cryptomator/ui/controls/NumericTextField.java new file mode 100644 index 000000000..d875442c3 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/controls/NumericTextField.java @@ -0,0 +1,20 @@ +package org.cryptomator.ui.controls; + +import javafx.scene.control.TextField; +import javafx.scene.control.TextFormatter; + +import java.util.regex.Pattern; + +public class NumericTextField extends TextField { + + private final static Pattern DIGIT_PATTERN = Pattern.compile("\\d*"); + + public NumericTextField() { + this.setTextFormatter(new TextFormatter<>(this::filterNumericTextChange)); + } + + private TextFormatter.Change filterNumericTextChange(TextFormatter.Change change) { + return DIGIT_PATTERN.matcher(change.getText()).matches() ? change : null; + } + +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/model/WebDavVolume.java b/main/ui/src/main/java/org/cryptomator/ui/model/WebDavVolume.java index 118e3a368..c46ff0ffb 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/model/WebDavVolume.java +++ b/main/ui/src/main/java/org/cryptomator/ui/model/WebDavVolume.java @@ -52,7 +52,7 @@ public class WebDavVolume implements Volume { } MountParams mountParams = MountParams.create() // .withWindowsDriveLetter(vaultSettings.winDriveLetter().get()) // - .withPreferredGvfsScheme(settings.preferredGvfsScheme().get())// + .withPreferredGvfsScheme(settings.preferredGvfsScheme().get().getPrefix())// .withWebdavHostname(getLocalhostAliasOrNull()) // .build(); try { diff --git a/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesController.java b/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesController.java index 63a6fe390..82b6798c1 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/preferences/PreferencesController.java @@ -2,9 +2,14 @@ package org.cryptomator.ui.preferences; import javafx.beans.binding.Bindings; import javafx.beans.binding.BooleanBinding; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; import javafx.scene.control.ChoiceBox; +import javafx.scene.control.TextField; +import javafx.util.StringConverter; import org.cryptomator.common.settings.Settings; import org.cryptomator.common.settings.VolumeImpl; +import org.cryptomator.common.settings.WebDavUrlScheme; import org.cryptomator.ui.FxController; import org.cryptomator.ui.model.Volume; @@ -15,7 +20,12 @@ public class PreferencesController implements FxController { private final Settings settings; private final BooleanBinding showWebDavSettings; + public CheckBox checkForUpdatesCheckbox; + public CheckBox debugModeCheckbox; public ChoiceBox volumeTypeChoicBox; + public TextField webDavPortField; + public Button changeWebDavPortButton; + public ChoiceBox webDavUrlSchemeChoiceBox; @Inject PreferencesController(Settings settings) { @@ -24,10 +34,39 @@ public class PreferencesController implements FxController { } public void initialize() { + checkForUpdatesCheckbox.selectedProperty().bindBidirectional(settings.checkForUpdates()); + + debugModeCheckbox.selectedProperty().bindBidirectional(settings.debugMode()); + volumeTypeChoicBox.getItems().addAll(Volume.getCurrentSupportedAdapters()); volumeTypeChoicBox.valueProperty().bindBidirectional(settings.preferredVolumeImpl()); + volumeTypeChoicBox.setConverter(new VolumeImplConverter()); + + webDavPortField.setText(String.valueOf(settings.port().get())); + changeWebDavPortButton.visibleProperty().bind(settings.port().asString().isNotEqualTo(webDavPortField.textProperty())); + changeWebDavPortButton.disableProperty().bind(Bindings.createBooleanBinding(this::validateWebDavPort, webDavPortField.textProperty()).not()); + + webDavUrlSchemeChoiceBox.getItems().addAll(WebDavUrlScheme.values()); + webDavUrlSchemeChoiceBox.valueProperty().bindBidirectional(settings.preferredGvfsScheme()); + webDavUrlSchemeChoiceBox.setConverter(new WebDavUrlSchemeConverter()); } + private boolean validateWebDavPort() { + try { + int port = Integer.parseInt(webDavPortField.getText()); + return port == 0 // choose port automatically + || port >= Settings.MIN_PORT && port <= Settings.MAX_PORT; // port within range + } catch (NumberFormatException e) { + return false; + } + } + + public void doChangeWebDavPort() { + settings.port().set(Integer.parseInt(webDavPortField.getText())); + } + + /* Property Getters */ + public BooleanBinding showWebDavSettingsProperty() { return showWebDavSettings; } @@ -35,4 +74,33 @@ public class PreferencesController implements FxController { public Boolean getShowWebDavSettings() { return showWebDavSettings.get(); } + + /* Helper classes */ + + private static class WebDavUrlSchemeConverter extends StringConverter { + + @Override + public String toString(WebDavUrlScheme scheme) { + return scheme.getDisplayName(); + } + + @Override + public WebDavUrlScheme fromString(String string) { + throw new UnsupportedOperationException(); + } + } + + private static class VolumeImplConverter extends StringConverter { + + @Override + public String toString(VolumeImpl impl) { + return impl.getDisplayName(); + } + + @Override + public VolumeImpl fromString(String string) { + throw new UnsupportedOperationException(); + } + } + } diff --git a/main/ui/src/main/resources/fxml/preferences.fxml b/main/ui/src/main/resources/fxml/preferences.fxml index 567902706..fe3ebff87 100644 --- a/main/ui/src/main/resources/fxml/preferences.fxml +++ b/main/ui/src/main/resources/fxml/preferences.fxml @@ -1,13 +1,13 @@ - - - - - - + + + + + + -