Example usage for javafx.scene.input Dragboard hasFiles

List of usage examples for javafx.scene.input Dragboard hasFiles

Introduction

In this page you can find the example usage for javafx.scene.input Dragboard hasFiles.

Prototype

public final boolean hasFiles() 

Source Link

Document

Gets whether an List of Files (DataFormat.FILES) has been registered on this Clipboard.

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 551, 400);
    scene.setOnDragOver(new EventHandler<DragEvent>() {
        @Override//from  w  w w  .j ava 2  s. c  o m
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            if (db.hasFiles()) {
                event.acceptTransferModes(TransferMode.COPY);
            } else {
                event.consume();
            }
        }
    });

    // Dropping over surface
    scene.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasFiles()) {
                success = true;
                String filePath = null;
                for (File file : db.getFiles()) {
                    filePath = file.getAbsolutePath();
                    System.out.println(filePath);
                }
            }
            event.setDropCompleted(success);
            event.consume();
        }
    });

    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:at.ac.tuwien.qse.sepm.gui.controller.impl.GridViewImpl.java

private void handleDragDropped(DragEvent event) {
    LOGGER.debug("drag dropped");

    Dragboard dragboard = event.getDragboard();
    boolean success = dragboard.hasFiles();
    if (success) {
        dragboard.getFiles().forEach(f -> LOGGER.debug("dropped file {}", f));
        for (File file : dragboard.getFiles()) {
            try {
                workspaceService.addDirectory(file.toPath());
            } catch (ServiceException ex) {
                LOGGER.error("Couldn't add directory {}");
            }/*from  ww w .  ja  v  a 2 s  .c  o  m*/
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void startDrop(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.ANY);
    }//w  w w .j  a v a  2s.c  o  m
}

From source file:de.pixida.logtest.designer.MainWindow.java

private void initDragAndDropForRootScene(final Scene scene) {
    scene.setOnDragOver(event -> {//  w  ww  .  j a v a2  s  .  c  om
        final Dragboard db = event.getDragboard();
        if (db.hasFiles()) {
            event.acceptTransferModes(TransferMode.COPY);
        } else {
            event.consume();
        }
    });

    scene.setOnDragDropped(event -> {
        final Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.hasFiles()) {
            success = true;
            for (final File file : db.getFiles()) {
                final Type guess = this.guessTypeOfFile(file);
                if (guess != null) {
                    this.handleLoadDocument(guess, file);
                } else {
                    ; // Unrecognized files are currently ignored
                }
            }
        }
        event.setDropCompleted(success);
        event.consume();
    });
}

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void stopDrop(DragEvent event) {
    Dragboard db = event.getDragboard();
    boolean success = false;
    if (db.hasFiles()) {
        success = true;/*from  w w  w.j av  a2 s  . com*/
        for (File file : db.getFiles()) {
            if (file.getName().endsWith(".jar") || file.getName().endsWith(".class")) {
                openedFileController.openFile(file);
            }
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

public boolean acceptedFiles(Dragboard db, List<String> accepted, boolean folder) {
    if (db.hasFiles()) {
        for (File file : db.getFiles()) {
            String ext = getExtension(file.getName());
            if (file.isDirectory() && !folder) {
                return false;
            }/*from   w  ww  .j a  va2 s  . c o m*/
            if (file.isFile() && !accepted.contains(ext)) {
                return false;
            }
        }
        return true;
    }
    return false;
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

/**
 * Drag and drop input//from  w w  w  .  j a va 2  s  .c  om
 */
@FXML
protected void onDragDroppedInput(DragEvent event) throws Exception {
    // Files dropped
    Dragboard db = event.getDragboard();
    boolean success = false;
    if (db.hasFiles()) {
        success = true;
        String filePath = "";
        for (File file : db.getFiles()) {
            if (!filePath.isEmpty()) {
                filePath += ";";
            }
            filePath += file.getAbsolutePath();
        }
        String currentText = inputText.getText();
        if (!currentText.isEmpty() && !currentText.equals(bundle.getString("inputText"))) {
            inputText.setText(currentText + ";" + filePath);
        } else {
            inputText.setText(filePath);
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:AudioPlayer3.java

private void initSceneDragAndDrop(Scene scene) {
    scene.setOnDragOver(new EventHandler<DragEvent>() {
        @Override/* w w w .jav  a 2 s  .com*/
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            if (db.hasFiles() || db.hasUrl()) {
                event.acceptTransferModes(TransferMode.ANY);
            }
            event.consume();
        }
    });

    scene.setOnDragDropped(new EventHandler<DragEvent>() {
        @Override
        public void handle(DragEvent event) {
            Dragboard db = event.getDragboard();
            String url = null;

            if (db.hasFiles()) {
                url = db.getFiles().get(0).toURI().toString();
            } else if (db.hasUrl()) {
                url = db.getUrl();
            }

            if (url != null) {
                songModel.setURL(url);
                songModel.getMediaPlayer().play();
            }

            event.setDropCompleted(url != null);
            event.consume();
        }
    });
}

From source file:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

private void addConfigFiles() {
    String previous = null;//from   www  . j a v  a 2  s  .  c om
    if (selectedButton != null) {
        previous = selectedButton.getText();
    }
    selectedButton = null;
    group = new ToggleGroup();
    vBoxConfig = new VBox();
    vBoxConfig.setId("vBoxConfig");
    vBoxConfig.setSpacing(3);
    vBoxConfig.setPadding(new Insets(5));

    // Default one
    String description = interService.getDescriptionFromDefault();
    if (description != null) {
        addConfigFile(bundle.getString("default"), description,
                bundle.getString("default").equalsIgnoreCase(previous));
    }

    // User configs
    File folder = new File(DPFManagerProperties.getConfigDir());
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isFile()) {
            if (fileEntry.getName().toLowerCase().endsWith(".dpf")) {
                description = getController().readDescription(fileEntry);
                addConfigFile(fileEntry.getName(), description, fileEntry.getName().equalsIgnoreCase(previous));
            }
        }
    }

    // Drag and drop
    configScroll.setOnDragDropped(event -> {
        // Files dropped
        Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.hasFiles()) {
            success = true;
            for (File file : db.getFiles()) {
                getController().addConfigFile(file, false);
            }
        }
        event.setDropCompleted(success);
        event.consume();
    });
    configScroll.setOnDragOver(event -> {
        if (acceptedFiles(event.getDragboard(), Arrays.asList("dpf"), false)) {
            event.acceptTransferModes(TransferMode.MOVE);
        }
        event.consume();
    });
    configScroll.setOnDragEntered(event -> {
        if (acceptedFiles(event.getDragboard(), Arrays.asList("dpf"), false)) {
            configScroll.getStyleClass().add("on-drag");
        }
        event.consume();
    });
    configScroll.setOnDragExited(event -> {
        configScroll.getStyleClass().remove("on-drag");
        event.consume();
    });

    configScroll.setContent(vBoxConfig);
}

From source file:net.rptools.tokentool.controller.ManageOverlays_Controller.java

@FXML
void overlayViewFlowPane_DragDropped(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasImage()) {
        try {//from ww w  .j a v  a2 s.  c  o  m
            // Prompt for name & return file name
            File newOverlayFile = new File(currentDirectory.getCanonicalPath() + "/somefilename.png");
            ImageIO.write(SwingFXUtils.fromFXImage(db.getImage(), null), "png", newOverlayFile);
        } catch (IOException e) {
            log.error("Error writing new overlay image.", e);
        }

        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    } else if (db.hasFiles()) {
        db.getFiles().forEach(file -> {
            FileSaveUtil.copyFile(file, currentDirectory);
        });
        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    } else if (db.hasUrl()) {
        FileSaveUtil.copyFile(new File(db.getUrl()), currentDirectory);
        loadImages(overlayTreeView.getSelectionModel().getSelectedItem());
        event.setDropCompleted(true);
    }
}