Example usage for javafx.scene.input TransferMode COPY_OR_MOVE

List of usage examples for javafx.scene.input TransferMode COPY_OR_MOVE

Introduction

In this page you can find the example usage for javafx.scene.input TransferMode COPY_OR_MOVE.

Prototype

TransferMode[] COPY_OR_MOVE

To view the source code for javafx.scene.input TransferMode COPY_OR_MOVE.

Click Source Link

Document

Array containing transfer modes COPY and MOVE.

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    stage.setTitle("Hello Drag And Drop");

    Group root = new Group();
    Scene scene = new Scene(root, 400, 200);
    scene.setFill(Color.LIGHTGREEN);

    final Text source = new Text(50, 100, "DRAG ME");
    source.setScaleX(2.0);/*www  .  java 2 s .c  o  m*/
    source.setScaleY(2.0);

    final Text target = new Text(250, 100, "DROP HERE");
    target.setScaleX(2.0);
    target.setScaleY(2.0);

    source.setOnDragDetected(new EventHandler<MouseEvent>() {
        public void handle(MouseEvent event) {
            /* drag was detected, start drag-and-drop gesture*/
            System.out.println("onDragDetected");

            /* allow any transfer mode */
            Dragboard db = source.startDragAndDrop(TransferMode.ANY);

            /* put a string on dragboard */
            ClipboardContent content = new ClipboardContent();
            content.putString(source.getText());
            db.setContent(content);

            event.consume();
        }
    });

    target.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data is dragged over the target */
            System.out.println("onDragOver");

            /* accept it only if it is  not dragged from the same node 
             * and if it has a string data */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                /* allow for both copying and moving, whatever user chooses */
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }

            event.consume();
        }
    });

    target.setOnDragEntered(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture entered the target */
            System.out.println("onDragEntered");
            /* show to the user that it is an actual gesture target */
            if (event.getGestureSource() != target && event.getDragboard().hasString()) {
                target.setFill(Color.GREEN);
            }

            event.consume();
        }
    });

    target.setOnDragExited(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* mouse moved away, remove the graphical cues */
            target.setFill(Color.BLACK);

            event.consume();
        }
    });

    target.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* data dropped */
            System.out.println("onDragDropped");
            /* if there is a string data on dragboard, read it and use it */
            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasString()) {
                target.setText(db.getString());
                success = true;
            }
            /* let the source know whether the string was successfully 
             * transferred and used */
            event.setDropCompleted(success);

            event.consume();
        }
    });

    source.setOnDragDone(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture ended */
            System.out.println("onDragDone");
            /* if the data was successfully moved, clear it */
            if (event.getTransferMode() == TransferMode.MOVE) {
                source.setText("");
            }

            event.consume();
        }
    });

    root.getChildren().add(source);
    root.getChildren().add(target);
    stage.setScene(scene);
    stage.show();
}

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;
        }/* w  w  w. j  a v a 2  s  . c  o m*/
        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.rockhoppertech.symchords.fx.SymChordsController.java

protected void setupDragonDrop() {
    Image image = new Image(getClass().getResourceAsStream("/images/rocky-32-trans.png"));
    dragImageView = new ImageView(image);
    dragImageView.setFitHeight(32);// w ww.  j a va  2s .  c  o m
    dragImageView.setFitWidth(32);

    grandStaff.setOnDragDetected(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent me) {

            if (!root.getChildren().contains(dragImageView)) {
                root.getChildren().add(dragImageView);
            }

            // dragImageView.setOpacity(0.5);
            dragImageView.toFront();
            dragImageView.setMouseTransparent(true);
            dragImageView.setVisible(true);
            dragImageView.relocate((int) (me.getSceneX() - dragImageView.getBoundsInLocal().getWidth() / 2),
                    (int) (me.getSceneY() - dragImageView.getBoundsInLocal().getHeight() / 2));

            Dragboard db = grandStaff.startDragAndDrop(TransferMode.ANY);

            // TODO remove the custom image nonsense in javafx 8
            // javafx 8
            // db.setDragView(dragImageView);

            ClipboardContent content = new ClipboardContent();
            // MIDITrack track = grandStaff.getMIDITrack();
            MIDITrack track = model.getMIDITrack();
            content.put(midiTrackDataFormat, track);
            db.setContent(content);
            me.consume();
        }
    });

    grandStaff.setOnDragDone(new EventHandler<DragEvent>() {
        public void handle(DragEvent e) {
            dragImageView.setVisible(false);
            e.consume();
        }
    });

    // Parent root = grandStaff.getScene().getRoot();
    // stage.getScene().getRoot();

    if (root != null) {
        root.setOnDragOver(new EventHandler<DragEvent>() {
            public void handle(DragEvent e) {
                Point2D localPoint = grandStaff.getScene().getRoot()
                        .sceneToLocal(new Point2D(e.getSceneX(), e.getSceneY()));
                dragImageView.relocate(
                        (int) (localPoint.getX() - dragImageView.getBoundsInLocal().getWidth() / 2),
                        (int) (localPoint.getY() - dragImageView.getBoundsInLocal().getHeight() / 2));
                e.consume();
            }
        });
    }

    trackList.setOnDragOver(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /*
             * data is dragged over the target; accept it only if it is not
             * dragged from the same node and if it has MIDITrack data
             */
            if (event.getGestureSource() != trackList && event.getDragboard().hasContent(midiTrackDataFormat)) {
                logger.debug("drag over");
                /* allow for both copying and moving, whatever user chooses */
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }

            // Don't consume the event. Let the layers below process the
            // DragOver event as well so that the
            // translucent container image will follow the cursor.
            // event.consume();
        }
    });

    trackList.setOnDragEntered(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* the drag-and-drop gesture entered the target */
            /* show to the user that it is an actual gesture target */
            logger.debug("drag entered");
            if (event.getGestureSource() != trackList && event.getDragboard().hasContent(midiTrackDataFormat)) {
                DropShadow dropShadow = new DropShadow();
                dropShadow.setRadius(5.0);
                dropShadow.setOffsetX(3.0);
                dropShadow.setOffsetY(3.0);
                dropShadow.setColor(Color.color(0.4, 0.5, 0.5));
                trackList.setEffect(dropShadow);
            }
            event.consume();
        }
    });

    trackList.setOnDragExited(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {
            /* mouse moved away, remove the graphical cues */
            trackList.setEffect(null);
            event.consume();
        }
    });

    trackList.setOnDragDropped(new EventHandler<DragEvent>() {
        public void handle(DragEvent event) {

            Dragboard db = event.getDragboard();
            boolean success = false;
            if (db.hasContent(midiTrackDataFormat)) {
                MIDITrack track = (MIDITrack) db.getContent(midiTrackDataFormat);
                trackList.getItems().add(track);
                success = true;

            }
            /*
             * let the source know whether the data was successfully
             * transferred and used
             */
            event.setDropCompleted(success);
            event.consume();
        }
    });

    logger.debug("jvm mime {}", DataFlavor.javaJVMLocalObjectMimeType);
}

From source file:org.pdfsam.ui.io.BrowsableDirectoryField.java

private Consumer<DragEvent> onDragOverConsumer() {
    return (DragEvent e) -> {
        e.acceptTransferModes(TransferMode.COPY_OR_MOVE);
    };
}

From source file:org.ykc.usbcx.MainWindowController.java

@FXML
void openOnDragOver(DragEvent event) {
    Dragboard db = event.getDragboard();
    if (db.hasFiles()) {
        event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
    } else {/*w  w w.  j a va 2s .co m*/
        event.consume();
    }
}