将电子邮件附件拖放到javafx 8应用程序中

时间:2015-08-07 21:11:23

标签: email drag-and-drop javafx-8

我正在尝试实现将电子邮件中附加的文档拖放到jdk 8b45上运行的JavaFX 8应用程序中的要求。我可以从计算机上的任何文件夹拖放,但不能从电子邮件附件拖放。

// MY VBOX WHERE I WANT TO DROP FILES INTO
VBox bkgrndDocsVBox = new VBox(10.0);
bkgrndDocsVBox.setPadding(new Insets(15, 10, 5, 10));

bkgrndDocsVBox.setStyle("-fx-border-color: transparent;");
bkgrndDocsVBox.setOnDragOver((final DragEvent event) -> {
    mouseDragOver(event, bkgrndDocsVBox);
});
bkgrndDocsVBox.setOnDragDropped((final DragEvent event) -> {
    mouseDragDropped(event, backgroundDocsDataTable);
});
bkgrndDocsVBox.setOnDragExited((final DragEvent event) -> {
    bkgrndDocsVBox.setStyle("-fx-border-color: transparent;");
});

..............................
..............................

private void mouseDragOver(DragEvent dragEvent, VBox bkgrndDocsVBox) {
    final Dragboard dragboard = dragEvent.getDragboard();

    System.out.println("dragboard.hasFiles()::"+dragboard.hasFiles());

    if (dragboard.hasFiles()) {
        bkgrndDocsVBox.setStyle("-fx-border-color: green;");
        dragEvent.acceptTransferModes(TransferMode.ANY);
    } else {
        dragEvent.consume();
    }
}

..............................
..............................
private void mouseDragDropped(DragEvent dragEvent, TableView<BgDocBean> bgDocsTable) {

    System.out.println("ENTER mouseDragDropped");
    final Dragboard dragBoard = dragEvent.getDragboard();
    boolean success = false;
    boolean isAccepted = false;

// SAVE the FILES into the DATABASE
.......... .......... .......... .......... 
.......... .......... .......... ..........
}

当我尝试从Windows文件夹中拖放文件时,上面的代码有效。但是,当我尝试从电子邮件附件中拖放文件时,控制台上会显示“ dragboard.hasFiles():: false ”,但功能不起作用。

请参阅下面的全功能POC:

import javafx.util.Duration;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class HelloDragAndDrop extends Application {

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

        VBox root = new VBox();
        root.setStyle("-fx-border-color: transparent;");
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 400, 200);
        scene.setFill(Color.WHITESMOKE);

        Text target = new Text("DROP HERE");
        target.setScaleX(2.0);
        target.setScaleY(2.0);

        root.setOnDragOver((DragEvent event) -> {
            System.out.println("onDragOver");
            System.out.println("event.getDragboard().hasFiles()::" +  event.getDragboard().hasFiles());
            if (event.getGestureSource() != root && event.getDragboard().hasFiles()) {
                event.acceptTransferModes(TransferMode.ANY);
            }
            event.consume();
        });

        root.setOnDragEntered((DragEvent event) -> {
            System.out.println("onDragEntered");
            if (event.getGestureSource() != root && event.getDragboard().hasFiles()) {
                root.setStyle("-fx-border-color: green;");
            }
            event.consume();
        });

        root.setOnDragExited((DragEvent event) -> {
            root.setStyle("-fx-border-color: transparent;");
            event.consume();
        });

        root.setOnDragDropped((DragEvent event) -> {
            System.out.println("onDragDropped");
            Dragboard db = event.getDragboard();
            System.out.println("db.hasFiles()::" + db.hasFiles());
            boolean success = false;
            if (db.hasFiles()) {
                target.setText("SUCCESSFULLY DROPPED");
                success = true;
            }
            event.setDropCompleted(success);
            event.consume();

            Timeline timeline = new Timeline(
                new KeyFrame(Duration.seconds(2), (ActionEvent actionEvent) -> {
                        target.setText("DROP HERE");
            }),
                    new KeyFrame(Duration.seconds(5))
            );
            timeline.setCycleCount(1);
            timeline.play();

        });

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

    public static void main(String[] args) {
        Application.launch(args);
    }
}

在我的控制台上,如果我从Microsoft Outlook Professional Plus 2010拖放附件时始终显示'event.getDragboard()。hasFiles():: false'。

我非常感谢有关如何成功实施这一点的任何提示。感谢。

0 个答案:

没有答案