[1097] | 1 | package org.expeditee.gio.javafx;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 | import java.util.List;
|
---|
| 6 |
|
---|
| 7 | import org.expeditee.core.Point;
|
---|
| 8 | import org.expeditee.gio.DragAndDropManager;
|
---|
| 9 | import org.expeditee.gui.MessageBay;
|
---|
| 10 |
|
---|
| 11 | import javafx.event.EventHandler;
|
---|
| 12 | import javafx.scene.Scene;
|
---|
| 13 | import javafx.scene.input.DataFormat;
|
---|
| 14 | import javafx.scene.input.DragEvent;
|
---|
| 15 | import javafx.scene.input.Dragboard;
|
---|
| 16 | import javafx.scene.input.TransferMode;
|
---|
| 17 |
|
---|
| 18 | public class JavaFXDragAndDropManager extends DragAndDropManager {
|
---|
| 19 |
|
---|
| 20 | /** Singleton instance. */
|
---|
| 21 | private static JavaFXDragAndDropManager _instance;
|
---|
| 22 |
|
---|
| 23 | /** Singleton instantiator. */
|
---|
| 24 | public static JavaFXDragAndDropManager getInstance()
|
---|
| 25 | {
|
---|
| 26 | if (_instance == null) _instance = new JavaFXDragAndDropManager();
|
---|
| 27 |
|
---|
| 28 | return _instance;
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | // GNOME and KDE desktops have a specialized way of DNDing files
|
---|
| 32 | private DataFormat _URIListDataflavorString = new DataFormat("text/uri-list;class=java.lang.String");
|
---|
| 33 | private DataFormat _URIListDataflavorCharArray = new DataFormat("text/uri-list;class=\"[C\"");
|
---|
| 34 |
|
---|
| 35 | private JavaFXDragAndDropManager()
|
---|
| 36 | {
|
---|
| 37 | Scene scene = JavaFXGraphicsManager.ExpediteeApplication.theScene;
|
---|
| 38 |
|
---|
| 39 | scene.setOnDragOver(new EventHandler<DragEvent>() {
|
---|
| 40 | @Override
|
---|
| 41 | public void handle(DragEvent event)
|
---|
| 42 | {
|
---|
| 43 | Dragboard dragboard = event.getDragboard();
|
---|
| 44 | if ( dragboard.hasString() ||
|
---|
| 45 | dragboard.hasFiles() ||
|
---|
| 46 | dragboard.hasContent(_URIListDataflavorString) ||
|
---|
| 47 | dragboard.hasContent(_URIListDataflavorCharArray))
|
---|
| 48 |
|
---|
| 49 | {
|
---|
| 50 | event.acceptTransferModes(TransferMode.COPY);
|
---|
| 51 | }
|
---|
| 52 | event.consume();
|
---|
| 53 | }
|
---|
| 54 | });
|
---|
| 55 |
|
---|
| 56 | scene.setOnDragDropped(new EventHandler<DragEvent>() {
|
---|
| 57 | @Override
|
---|
| 58 | public void handle(DragEvent event)
|
---|
| 59 | {
|
---|
| 60 | Dragboard dragboard = event.getDragboard();
|
---|
| 61 | Point dropPoint = new Point((int) event.getX(), (int) event.getY());
|
---|
| 62 | boolean success = false;
|
---|
| 63 | try {
|
---|
| 64 |
|
---|
| 65 | if (dragboard.hasFiles()) {
|
---|
| 66 | List<File> files = dragboard.getFiles();
|
---|
| 67 | if (files != null && files.size() > 0) {
|
---|
| 68 | importFileList(files, dropPoint);
|
---|
| 69 | }
|
---|
| 70 | } else if (dragboard.hasContent(_URIListDataflavorString)) {
|
---|
| 71 | String data = (String) dragboard.getContent(_URIListDataflavorString);
|
---|
| 72 | List<File> files = textURIListToFileList(data);
|
---|
| 73 | importFileList(files, dropPoint);
|
---|
| 74 | success = true;
|
---|
| 75 | } else if (dragboard.hasContent(_URIListDataflavorCharArray)) {
|
---|
| 76 | char[] data = (char[]) dragboard.getContent(_URIListDataflavorCharArray);
|
---|
| 77 | String uriString = new String(data);
|
---|
| 78 | List<File> files = textURIListToFileList(uriString);
|
---|
| 79 | importFileList(files, dropPoint);
|
---|
| 80 | success = true;
|
---|
| 81 | } else if (dragboard.hasString()) {
|
---|
| 82 | String str = dragboard.getString();
|
---|
| 83 | if (str != null && str.length() > 0) {
|
---|
| 84 | importString(str, dropPoint);
|
---|
| 85 | success = true;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | } catch (IOException e) {
|
---|
| 89 | e.printStackTrace();
|
---|
| 90 | MessageBay.displayMessage("Failed to import data in Expeditee");
|
---|
| 91 | }
|
---|
| 92 | event.setDropCompleted(success);
|
---|
| 93 | event.consume();
|
---|
| 94 | }
|
---|
| 95 | });
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | }
|
---|