From 449f929f08b78c3dcb8011227d0446a039c57f67 Mon Sep 17 00:00:00 2001 From: Andreas Billmann Date: Wed, 19 Oct 2022 12:34:55 +0200 Subject: [PATCH] Support for drag'n'drop --- README.md | 2 +- build.gradle | 2 +- .../smartcsv/fx/SmartCSVController.java | 34 +++++++++++++++++++ .../javafx/smartcsv/fx/application.properties | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e816d94..680b371 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ even in a "normal" CSV editor. So I decided to write this simple JavaFX applicat [Wiki & Documentation](https://github.com/frosch95/SmartCSV.fx/wiki) -binary distribution of the [latest release (1.1.0)](https://github.com/frosch95/SmartCSV.fx/releases/download/1.1.0/SmartCSV.fx-1.1.0.zip) +binary distribution of the [latest release (1.2.0)](https://github.com/frosch95/SmartCSV.fx/releases/download/1.2.0/SmartCSV.fx-1.2.0.zip) ## Talks [Introduction](http://javafx.ninja/talks/introduction/) diff --git a/build.gradle b/build.gradle index e00e354..bce54df 100644 --- a/build.gradle +++ b/build.gradle @@ -48,5 +48,5 @@ test { } group 'ninja.javafx' -version '1.1.0' +version '1.2.0' mainClassName = 'ninja.javafx.smartcsv.Main' diff --git a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java index 2a106ab..ae0f2c1 100644 --- a/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java +++ b/src/main/java/ninja/javafx/smartcsv/fx/SmartCSVController.java @@ -36,6 +36,8 @@ import javafx.event.EventHandler; import javafx.fxml.FXML; import javafx.scene.Node; import javafx.scene.control.*; +import javafx.scene.input.Dragboard; +import javafx.scene.input.TransferMode; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.stage.FileChooser; @@ -255,6 +257,34 @@ public class SmartCSVController extends FXMLController { fileEncodingFile.setFile(ENCODING_FILE); loadCsvPreferencesFromFile(); + + initDragAndDrop(); + } + + private void initDragAndDrop() { + tableWrapper.setOnDragOver(event -> { + Dragboard db = event.getDragboard(); + if (event.getGestureSource() != tableWrapper + && db.hasFiles() + && db.getFiles().size() == 1 + && db.getFiles().get(0).getName().endsWith(".csv")) { + event.acceptTransferModes(TransferMode.COPY); + } + event.consume(); + }); + + tableWrapper.setOnDragDropped(event -> { + Dragboard db = event.getDragboard(); + boolean success = false; + if (db.hasFiles() && db.getFiles().size() == 1) { + File file = db.getFiles().get(0); + openFile(currentCsvFile, file); + success = true; + } + /* let the source know whether the string was successfully + * transferred and used */ + event.setDropCompleted(success); + }); } private void loadEncodingFromFile() { @@ -599,6 +629,10 @@ public class SmartCSVController extends FXMLController { //Show open file dialog File file = fileChooser.showOpenDialog(applicationPane.getScene().getWindow()); + openFile(storageFile, file); + } + + private void openFile(FileStorage storageFile, File file) { if (file != null) { File previousFile = storageFile.getFile(); storageFile.setFile(file); diff --git a/src/main/resources/ninja/javafx/smartcsv/fx/application.properties b/src/main/resources/ninja/javafx/smartcsv/fx/application.properties index aec5e25..2f533ec 100644 --- a/src/main/resources/ninja/javafx/smartcsv/fx/application.properties +++ b/src/main/resources/ninja/javafx/smartcsv/fx/application.properties @@ -1,5 +1,5 @@ application.name = SmartCSV.fx -application.version = 1.0.0 +application.version = 1.2.0 # fxml views fxml.smartcvs.view = /ninja/javafx/smartcsv/fx/smartcsv.fxml