From 1b832e2851d85762c70b3c5fb439b02761c99b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20V=C3=A9lez=20Palacios?= Date: Sat, 21 May 2016 21:56:20 +0200 Subject: [PATCH] drag and drop, main argument autoload, autofixes --- src/main/java/es/kauron/estraba/App.java | 10 ++-- .../estraba/controller/SplashController.java | 47 ++++++++++++++++--- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/java/es/kauron/estraba/App.java b/src/main/java/es/kauron/estraba/App.java index c691ae8..24ed1d5 100644 --- a/src/main/java/es/kauron/estraba/App.java +++ b/src/main/java/es/kauron/estraba/App.java @@ -25,6 +25,7 @@ package es.kauron.estraba; +import es.kauron.estraba.controller.SplashController; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -32,6 +33,7 @@ import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; +import java.io.File; import java.util.ResourceBundle; import static java.util.ResourceBundle.getBundle; @@ -59,11 +61,13 @@ public class App extends Application { stage.setResizable(false); stage.setScene(new Scene(root)); - // Begin awesomewm code + if (getParameters().getUnnamed().size() == 1) { + loader.getController().loadGPXFile(new File(getParameters().getUnnamed().get(0))); + } + // fix rogue wm :) stage.setMinHeight(500); stage.setMinWidth(300); - stage.setResizable(false); - // End awesomewm code + stage.show(); } } diff --git a/src/main/java/es/kauron/estraba/controller/SplashController.java b/src/main/java/es/kauron/estraba/controller/SplashController.java index 4796cc5..7673dac 100644 --- a/src/main/java/es/kauron/estraba/controller/SplashController.java +++ b/src/main/java/es/kauron/estraba/controller/SplashController.java @@ -40,6 +40,8 @@ import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; +import javafx.scene.input.Dragboard; +import javafx.scene.input.TransferMode; import javafx.scene.layout.AnchorPane; import javafx.stage.FileChooser; import javafx.stage.Stage; @@ -78,11 +80,6 @@ public class SplashController implements Initializable{ @FXML private void loadGPXFile(ActionEvent event) throws Exception { - buttonLoad.setVisible(false); - labelWelcome.setVisible(false); - spinner.setVisible(true); - snackbar.registerSnackbarContainer(root); - snackbar.show("Loading file", 5000); FileChooser fileChooser = new FileChooser(); fileChooser.getExtensionFilters().add( new FileChooser.ExtensionFilter(App.GENERAL_BUNDLE.getString("app.extension"), "*.gpx")); @@ -91,7 +88,15 @@ public class SplashController implements Initializable{ errorLoading(); return; } + loadGPXFile(file); + } + public void loadGPXFile(File file) { + buttonLoad.setVisible(false); + labelWelcome.setVisible(false); + spinner.setVisible(true); + snackbar.registerSnackbarContainer(root); + snackbar.show("Loading file", 5000); Thread th = new Thread(new Task() { @Override protected DataBundle call() throws Exception { @@ -114,11 +119,16 @@ public class SplashController implements Initializable{ errorLoading(); } } + + @Override + protected void failed() { + super.failed(); + errorLoading(); + } }); th.setDaemon(true); th.start(); - } @Override @@ -133,6 +143,31 @@ public class SplashController implements Initializable{ ioe.printStackTrace(); } }); + + root.getScene().setOnDragOver(e -> { + Dragboard db = e.getDragboard(); + if (db.hasFiles()) { + e.acceptTransferModes(TransferMode.COPY); + } else { + e.consume(); + } + }); + + // Dropping over surface + root.getScene().setOnDragDropped(e -> { + Dragboard db = e.getDragboard(); + boolean success = false; + if (db.hasFiles()) { + success = true; + String filePath = null; + for (File file : db.getFiles()) { + filePath = file.getAbsolutePath(); + System.out.println(filePath); + } + } + e.setDropCompleted(success); + e.consume(); + }); } private void errorLoading() {