hooked up all controls in Preferences window

This commit is contained in:
Sebastian Stenzel
2019-07-17 16:17:23 +02:00
parent d51e3979ff
commit f7759d2547
10 changed files with 152 additions and 28 deletions

View File

@@ -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<WebDavUrlScheme> preferredGvfsScheme = new SimpleObjectProperty<>(DEFAULT_GVFS_SCHEME);
private final BooleanProperty debugMode = new SimpleBooleanProperty(DEFAULT_DEBUG_MODE);
private final ObjectProperty<VolumeImpl> preferredVolumeImpl = new SimpleObjectProperty<>(DEFAULT_PREFERRED_VOLUME_IMPL);
@@ -89,7 +89,7 @@ public class Settings {
return numTrayNotifications;
}
public StringProperty preferredGvfsScheme() {
public ObjectProperty<WebDavUrlScheme> preferredGvfsScheme() {
return preferredGvfsScheme;
}

View File

@@ -31,7 +31,7 @@ public class SettingsJsonAdapter extends TypeAdapter<Settings> {
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> {
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<Settings> {
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<VaultSettings> readVaultSettingsArray(JsonReader in) throws IOException {
List<VaultSettings> result = new ArrayList<>();
in.beginArray();

View File

@@ -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 <code>prefix</code>.
* @throws IllegalArgumentException if not WebDavUrlScheme with the given <code>prefix</code> was found.
*/
public static WebDavUrlScheme forPrefix(String prefix) throws IllegalArgumentException {
return Arrays.stream(values()) //
.filter(impl -> impl.prefix.equals(prefix)) //
.findAny() //
.orElseThrow(IllegalArgumentException::new);
}
}

View File

@@ -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):

View File

@@ -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);

View File

@@ -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());
}

View File

@@ -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;
}
}

View File

@@ -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 {

View File

@@ -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<VolumeImpl> volumeTypeChoicBox;
public TextField webDavPortField;
public Button changeWebDavPortButton;
public ChoiceBox<WebDavUrlScheme> 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<WebDavUrlScheme> {
@Override
public String toString(WebDavUrlScheme scheme) {
return scheme.getDisplayName();
}
@Override
public WebDavUrlScheme fromString(String string) {
throw new UnsupportedOperationException();
}
}
private static class VolumeImplConverter extends StringConverter<VolumeImpl> {
@Override
public String toString(VolumeImpl impl) {
return impl.getDisplayName();
}
@Override
public VolumeImpl fromString(String string) {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import org.cryptomator.ui.controls.NumericTextField?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.cryptomator.ui.preferences.PreferencesController"
@@ -17,8 +17,6 @@
<Insets bottom="12" left="12" right="12" top="12"/>
</padding>
<children>
<Label text="TODO preferences..."/>
<CheckBox fx:id="checkForUpdatesCheckbox" text="%preferences.autoUpdateCheck"/>
<CheckBox fx:id="debugModeCheckbox" text="%preferences.debugLogging"/>
@@ -30,13 +28,13 @@
<HBox spacing="6" visible="${controller.showWebDavSettings}">
<Label text="TODO WebDAV Port"/>
<TextField fx:id="portField"/>
<Button text="TODO Apply" fx:id="changePortButton" />
<NumericTextField fx:id="webDavPortField"/>
<Button text="TODO Apply" fx:id="changeWebDavPortButton" onAction="#doChangeWebDavPort"/>
</HBox>
<HBox spacing="6" visible="${controller.showWebDavSettings}">
<Label text="TODO WebDAV Scheme"/>
<ChoiceBox fx:id="prefGvfsScheme" maxWidth="Infinity"/>
<ChoiceBox fx:id="webDavUrlSchemeChoiceBox" maxWidth="Infinity"/>
</HBox>
</children>
</VBox>