From c6ad677e2bd977bd1396b7e4dc656428fe75ca06 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Wed, 4 Sep 2019 16:44:59 +0200 Subject: [PATCH] monster commit: Adding drag'n'drop functionality to add already existing vaults to cryptomator --- .../org/cryptomator/ui/common/FxmlFile.java | 3 +- .../ui/mainwindow/MainWindowController.java | 52 ++++++++++++++++- .../ui/mainwindow/MainWindowModule.java | 5 +- .../ui/wrongfilealert/WrongFileAlert.java | 14 +++++ .../WrongFileAlertComponent.java | 33 +++++++++++ .../WrongFileAlertController.java | 21 +++++++ .../wrongfilealert/WrongFileAlertModule.java | 58 +++++++++++++++++++ .../wrongfilealert/WrongFileAlertScoped.java | 13 +++++ .../src/main/resources/fxml/main_window.fxml | 8 +++ .../main/resources/fxml/wrongfilealert.fxml | 35 +++++++++++ .../main/resources/i18n/strings.properties | 5 ++ .../main/resources/i18n/strings_de.properties | 5 ++ 12 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlert.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertComponent.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertController.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertModule.java create mode 100644 main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertScoped.java create mode 100644 main/ui/src/main/resources/fxml/wrongfilealert.fxml diff --git a/main/ui/src/main/java/org/cryptomator/ui/common/FxmlFile.java b/main/ui/src/main/java/org/cryptomator/ui/common/FxmlFile.java index bcbccd419..83ddee800 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/common/FxmlFile.java +++ b/main/ui/src/main/java/org/cryptomator/ui/common/FxmlFile.java @@ -14,7 +14,8 @@ public enum FxmlFile { REMOVE_VAULT("/fxml/remove_vault.fxml"), // UNLOCK("/fxml/unlock2.fxml"), // TODO rename UNLOCK_SUCCESS("/fxml/unlock_success.fxml"), // - VAULT_OPTIONS("/fxml/vault_options.fxml"); + VAULT_OPTIONS("/fxml/vault_options.fxml"), // + WRONGFILEALERT("/fxml/wrongfilealert.fxml"); private final String filename; diff --git a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowController.java b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowController.java index 665f2d757..f4d543c9f 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowController.java +++ b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowController.java @@ -1,19 +1,27 @@ package org.cryptomator.ui.mainwindow; import javafx.beans.binding.BooleanBinding; +import javafx.collections.ObservableList; import javafx.fxml.FXML; +import javafx.scene.input.TransferMode; import javafx.scene.layout.HBox; import javafx.scene.layout.Region; +import javafx.scene.layout.VBox; import javafx.stage.Stage; +import org.cryptomator.common.settings.VaultSettings; +import org.cryptomator.common.vaults.Vault; +import org.cryptomator.common.vaults.VaultFactory; import org.cryptomator.ui.common.FontLoader; import org.cryptomator.ui.common.FxController; import org.cryptomator.ui.fxapp.FxApplication; import org.cryptomator.ui.fxapp.UpdateChecker; +import org.cryptomator.ui.wrongfilealert.WrongFileAlertComponent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import javax.inject.Named; +import java.io.File; @MainWindowScoped public class MainWindowController implements FxController { @@ -26,18 +34,26 @@ public class MainWindowController implements FxController { private final boolean minimizeToSysTray; private final UpdateChecker updateChecker; private final BooleanBinding updateAvailable; + private final ObservableList vaults; + private final VaultFactory vaultFactory; + private final WrongFileAlertComponent.Builder wrongFileAlert; public HBox titleBar; + public VBox dragAndDropRegion; + public VBox dragNDropIndicator; public Region resizer; private double xOffset; private double yOffset; @Inject - public MainWindowController(@MainWindow Stage window, FxApplication application, @Named("trayMenuSupported") boolean minimizeToSysTray, UpdateChecker updateChecker) { + public MainWindowController(@MainWindow Stage window, FxApplication application, @Named("trayMenuSupported") boolean minimizeToSysTray, UpdateChecker updateChecker, ObservableList vaults, VaultFactory vaultFactory, WrongFileAlertComponent.Builder wrongFileAlert) { this.window = window; this.application = application; this.minimizeToSysTray = minimizeToSysTray; this.updateChecker = updateChecker; this.updateAvailable = updateChecker.latestVersionProperty().isNotNull(); + this.vaults = vaults; + this.vaultFactory = vaultFactory; + this.wrongFileAlert = wrongFileAlert; } @FXML @@ -58,6 +74,40 @@ public class MainWindowController implements FxController { window.setHeight(event.getSceneY()); }); updateChecker.automaticallyCheckForUpdatesIfEnabled(); + dragNDropIndicator.setVisible(false); + dragAndDropRegion.setOnDragOver(event -> { + if (event.getGestureSource() != dragAndDropRegion && event.getDragboard().hasFiles()) { + /* allow for both copying and moving, whatever user chooses */ + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); + dragNDropIndicator.setVisible(true); + } + event.consume(); + }); + dragAndDropRegion.setOnDragExited(event -> dragNDropIndicator.setVisible(false)); + dragAndDropRegion.setOnDragDropped(event -> { + if (event.getGestureSource() != dragAndDropRegion && event.getDragboard().hasFiles()) { + /* allow for both copying and moving, whatever user chooses */ + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); + File dropped = event.getDragboard().getFiles().get(0); + if (dropped.getName().endsWith(".cryptomator")) { + addVault(dropped); + } else { + wrongFileAlert.build().showWrongFileAlertWindow(); + } + } + event.consume(); + }); + + } + + private void addVault(final File dropped) { + if (dropped != null) { + VaultSettings vaultSettings = VaultSettings.withRandomId(); + vaultSettings.path().setValue(dropped.toPath().toAbsolutePath().getParent()); + Vault newVault = vaultFactory.get(vaultSettings); + vaults.add(newVault); + //TODO: error handling? + } } private void loadFont(String resourcePath) { diff --git a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowModule.java b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowModule.java index 89c73eb27..eac6d58b4 100644 --- a/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowModule.java +++ b/main/ui/src/main/java/org/cryptomator/ui/mainwindow/MainWindowModule.java @@ -19,6 +19,7 @@ import org.cryptomator.ui.common.FxmlFile; import org.cryptomator.ui.common.FxmlScene; import org.cryptomator.ui.removevault.RemoveVaultComponent; import org.cryptomator.ui.vaultoptions.VaultOptionsComponent; +import org.cryptomator.ui.wrongfilealert.WrongFileAlertComponent; import javax.inject.Named; import javax.inject.Provider; @@ -26,7 +27,7 @@ import java.util.Map; import java.util.Optional; import java.util.ResourceBundle; -@Module(subcomponents = {AddVaultWizardComponent.class, RemoveVaultComponent.class, VaultOptionsComponent.class}) +@Module(subcomponents = {AddVaultWizardComponent.class, RemoveVaultComponent.class, VaultOptionsComponent.class, WrongFileAlertComponent.class}) abstract class MainWindowModule { @Provides @@ -88,4 +89,6 @@ abstract class MainWindowModule { @FxControllerKey(VaultListCellController.class) abstract FxController bindVaultListCellController(VaultListCellController controller); + + } diff --git a/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlert.java b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlert.java new file mode 100644 index 000000000..9a0057090 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlert.java @@ -0,0 +1,14 @@ +package org.cryptomator.ui.wrongfilealert; + +import javax.inject.Qualifier; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Qualifier +@Documented +@Retention(RUNTIME) +public @interface WrongFileAlert { + +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertComponent.java b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertComponent.java new file mode 100644 index 000000000..5b333328c --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertComponent.java @@ -0,0 +1,33 @@ +package org.cryptomator.ui.wrongfilealert; + +import dagger.Lazy; +import dagger.Subcomponent; +import javafx.scene.Scene; +import javafx.stage.Stage; +import org.cryptomator.ui.common.FxmlFile; +import org.cryptomator.ui.common.FxmlScene; + +@WrongFileAlertScoped +@Subcomponent(modules = {WrongFileAlertModule.class}) +public interface WrongFileAlertComponent { + + @WrongFileAlert + Stage window(); + + @FxmlScene(FxmlFile.WRONGFILEALERT) + Lazy scene(); + + default void showWrongFileAlertWindow() { + Stage stage = window(); + stage.setScene(scene().get()); + stage.sizeToScene(); + stage.show(); + } + + @Subcomponent.Builder + interface Builder { + + WrongFileAlertComponent build(); + } + +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertController.java b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertController.java new file mode 100644 index 000000000..52f0e99a4 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertController.java @@ -0,0 +1,21 @@ +package org.cryptomator.ui.wrongfilealert; + +import javafx.stage.Stage; +import org.cryptomator.ui.common.FxController; + +import javax.inject.Inject; + +@WrongFileAlertScoped +public class WrongFileAlertController implements FxController { + + private final Stage window; + + @Inject + public WrongFileAlertController(@WrongFileAlert Stage window) { + this.window = window; + } + + public void close() { + window.close(); + } +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertModule.java b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertModule.java new file mode 100644 index 000000000..d90e83fa4 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertModule.java @@ -0,0 +1,58 @@ +package org.cryptomator.ui.wrongfilealert; + +import dagger.Binds; +import dagger.Module; +import dagger.Provides; +import dagger.multibindings.IntoMap; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.stage.Modality; +import javafx.stage.Stage; +import org.cryptomator.ui.common.FXMLLoaderFactory; +import org.cryptomator.ui.common.FxController; +import org.cryptomator.ui.common.FxControllerKey; +import org.cryptomator.ui.common.FxmlFile; +import org.cryptomator.ui.common.FxmlScene; + +import javax.inject.Named; +import javax.inject.Provider; +import java.util.Map; +import java.util.Optional; +import java.util.ResourceBundle; + +@Module +abstract class WrongFileAlertModule { + + @Provides + @WrongFileAlert + @WrongFileAlertScoped + static FXMLLoaderFactory provideFxmlLoaderFactory(Map, Provider> factories, ResourceBundle resourceBundle) { + return new FXMLLoaderFactory(factories, resourceBundle); + } + + @Provides + @WrongFileAlert + @WrongFileAlertScoped + static Stage provideStage(ResourceBundle resourceBundle, @Named("windowIcon") Optional windowIcon) { + Stage stage = new Stage(); + stage.setTitle(resourceBundle.getString("wrongFileAlert.title")); + stage.setResizable(false); + stage.initModality(Modality.WINDOW_MODAL); + windowIcon.ifPresent(stage.getIcons()::add); + return stage; + } + + @Provides + @FxmlScene(FxmlFile.WRONGFILEALERT) + @WrongFileAlertScoped + static Scene provideWrongFileAlertScene(@WrongFileAlert FXMLLoaderFactory fxmlLoaders) { + return fxmlLoaders.createScene("/fxml/wrongfilealert.fxml"); // TODO rename fxml file + } + + // ------------------ + + @Binds + @IntoMap + @FxControllerKey(WrongFileAlertController.class) + abstract FxController bindWrongFileAlertController(WrongFileAlertController controller); +} diff --git a/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertScoped.java b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertScoped.java new file mode 100644 index 000000000..96c0ebc24 --- /dev/null +++ b/main/ui/src/main/java/org/cryptomator/ui/wrongfilealert/WrongFileAlertScoped.java @@ -0,0 +1,13 @@ +package org.cryptomator.ui.wrongfilealert; + +import javax.inject.Scope; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Scope +@Documented +@Retention(RetentionPolicy.RUNTIME) +public @interface WrongFileAlertScoped { + +} diff --git a/main/ui/src/main/resources/fxml/main_window.fxml b/main/ui/src/main/resources/fxml/main_window.fxml index 3e0aabb34..743e39f40 100644 --- a/main/ui/src/main/resources/fxml/main_window.fxml +++ b/main/ui/src/main/resources/fxml/main_window.fxml @@ -10,8 +10,10 @@ + @@ -48,5 +50,11 @@ + + + + + + diff --git a/main/ui/src/main/resources/fxml/wrongfilealert.fxml b/main/ui/src/main/resources/fxml/wrongfilealert.fxml new file mode 100644 index 000000000..5c8d0ab84 --- /dev/null +++ b/main/ui/src/main/resources/fxml/wrongfilealert.fxml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + +