Example usage for javafx.scene.input Dragboard getFiles

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

Introduction

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

Prototype

public final List<File> getFiles() 

Source Link

Document

Gets the List of Files from the clipboard which had previously been registered.

Usage

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 {}");
            }//  www  .java  2s  .  c o m
        }
    }
    event.setDropCompleted(success);
    event.consume();
}

From source file:edu.mit.lib.handbag.Controller.java

public void initialize() {

    Image wfIm = new Image(getClass().getResourceAsStream("/SiteMap.png"));
    workflowLabel.setGraphic(new ImageView(wfIm));
    workflowLabel.setContentDisplay(ContentDisplay.BOTTOM);

    workflowChoiceBox.addEventHandler(ActionEvent.ACTION, event -> {
        if (workflowChoiceBox.getItems().size() != 0) {
            return;
        }/*from   w w w. jav a  2 s  .  c om*/
        ObservableList<Workflow> wflowList = FXCollections.observableArrayList(loadWorkflows());
        workflowChoiceBox.setItems(wflowList);
        workflowChoiceBox.getSelectionModel().selectedIndexProperty().addListener((ov, value, new_value) -> {
            int newVal = (int) new_value;
            if (newVal != -1) {
                Workflow newSel = workflowChoiceBox.getItems().get(newVal);
                if (newSel != null) {
                    workflowLabel.setText(newSel.getName());
                    generateBagName(newSel.getBagNameGenerator());
                    bagLabel.setText(bagName);
                    sendButton.setText(newSel.getDestinationName());
                    setMetadataList(newSel);
                }
            }
        });
    });

    Image bagIm = new Image(getClass().getResourceAsStream("/Bag.png"));
    bagLabel.setGraphic(new ImageView(bagIm));
    bagLabel.setContentDisplay(ContentDisplay.BOTTOM);

    Image sendIm = new Image(getClass().getResourceAsStream("/Cabinet.png"));
    sendButton.setGraphic(new ImageView(sendIm));
    sendButton.setContentDisplay(ContentDisplay.BOTTOM);
    sendButton.setDisable(true);
    sendButton.setOnAction(e -> transmitBag());

    Image trashIm = new Image(getClass().getResourceAsStream("/Bin.png"));
    trashButton.setGraphic(new ImageView(trashIm));
    trashButton.setContentDisplay(ContentDisplay.BOTTOM);
    trashButton.setDisable(true);
    trashButton.setOnAction(e -> reset(false));

    TreeItem<PathRef> rootItem = new TreeItem<>(new PathRef("", Paths.get("data")));
    rootItem.setExpanded(true);
    payloadTreeView.setRoot(rootItem);
    payloadTreeView.setOnDragOver(event -> {
        if (event.getGestureSource() != payloadTreeView && event.getDragboard().getFiles().size() > 0) {
            event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
        }
        event.consume();
    });
    payloadTreeView.setOnDragDropped(event -> {
        Dragboard db = event.getDragboard();
        boolean success = false;
        if (db.getFiles().size() > 0) {
            for (File dragFile : db.getFiles()) {
                if (dragFile.isDirectory()) {
                    // explode directory and add expanded relative paths to tree
                    relPathSB = new StringBuilder();
                    try {
                        Files.walkFileTree(dragFile.toPath(), new SimpleFileVisitor<Path>() {
                            @Override
                            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                                    throws IOException {
                                relPathSB.append(dir.getFileName()).append("/");
                                return FileVisitResult.CONTINUE;
                            }

                            @Override
                            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                                    throws IOException {
                                payloadTreeView.getRoot().getChildren()
                                        .add(new TreeItem<>(new PathRef(relPathSB.toString(), file)));
                                bagSize += Files.size(file);
                                return FileVisitResult.CONTINUE;
                            }
                        });
                    } catch (IOException ioe) {
                    }
                } else {
                    payloadTreeView.getRoot().getChildren()
                            .add(new TreeItem<>(new PathRef("", dragFile.toPath())));
                    bagSize += dragFile.length();
                }
            }
            bagSizeLabel.setText(scaledSize(bagSize, 0));
            success = true;
            updateButtons();
        }
        event.setDropCompleted(success);
        event.consume();
    });

    metadataPropertySheet.setModeSwitcherVisible(false);
    metadataPropertySheet.setDisable(false);
    metadataPropertySheet.addEventHandler(KeyEvent.ANY, event -> {
        checkComplete(metadataPropertySheet.getItems());
        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  .  jav  a 2s. 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: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 www.ja v a 2  s.  c  om
        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: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 av a2  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 v  a  2s . co  m
 */
@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:dpfmanager.shell.interfaces.gui.component.dessign.DessignView.java

private void addConfigFiles() {
    String previous = null;/* w  w  w .j a va 2s.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:de.pixida.logtest.designer.MainWindow.java

private void initDragAndDropForRootScene(final Scene scene) {
    scene.setOnDragOver(event -> {//  www.  ja v  a  2  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:net.rptools.tokentool.controller.ManageOverlays_Controller.java

@FXML
void overlayViewFlowPane_DragDropped(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasImage()) {
        try {//ww w . j av  a 2s .co 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);
    }
}

From source file:AudioPlayer3.java

private void initSceneDragAndDrop(Scene scene) {
    scene.setOnDragOver(new EventHandler<DragEvent>() {
        @Override/*from  w  ww.ja v  a 2  s .co  m*/
        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();
        }
    });
}