[1097] | 1 | package org.expeditee.gio.javafx;
|
---|
| 2 |
|
---|
| 3 | import java.util.Set;
|
---|
| 4 |
|
---|
| 5 | import org.expeditee.core.BlockingRunnable;
|
---|
| 6 | import org.expeditee.gio.ClipboardManager;
|
---|
| 7 | import org.expeditee.gio.EcosystemManager;
|
---|
| 8 |
|
---|
| 9 | import javafx.scene.input.Clipboard;
|
---|
| 10 | import javafx.scene.input.ClipboardContent;
|
---|
| 11 | import javafx.scene.input.DataFormat;
|
---|
| 12 |
|
---|
| 13 | public class JavaFXClipboardManager extends ClipboardManager {
|
---|
| 14 |
|
---|
| 15 | /** Data format for identifying Expeditee-specific data on the clipboard. */
|
---|
| 16 | private static final DataFormat expediteeDataFormat = new DataFormat("expediteeDataFormat");
|
---|
| 17 |
|
---|
| 18 | /** Singleton instance. */
|
---|
| 19 | private static JavaFXClipboardManager _instance;
|
---|
| 20 |
|
---|
| 21 | /** Singleton instantiator. */
|
---|
| 22 | public static JavaFXClipboardManager getInstance()
|
---|
| 23 | {
|
---|
| 24 | if (_instance == null) _instance = new JavaFXClipboardManager();
|
---|
| 25 |
|
---|
| 26 | return _instance;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private Clipboard _clipboard;
|
---|
| 30 |
|
---|
| 31 | private JavaFXClipboardManager()
|
---|
| 32 | {
|
---|
| 33 | // Get a reference to the system clipboard
|
---|
| 34 | EcosystemManager.getMiscManager().runOnGIOThread(new BlockingRunnable() {
|
---|
| 35 | @Override
|
---|
| 36 | public void execute()
|
---|
| 37 | {
|
---|
| 38 | _clipboard = Clipboard.getSystemClipboard();
|
---|
| 39 | }
|
---|
| 40 | });
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | @Override
|
---|
| 44 | public void set(ClipboardData data)
|
---|
| 45 | {
|
---|
| 46 | // Leave the clipboard unchanged if data is null
|
---|
| 47 | if (data == null) return;
|
---|
| 48 |
|
---|
| 49 | // Fill out the data for the clipboard
|
---|
| 50 | ClipboardContent content = new ClipboardContent();
|
---|
| 51 | if (data.data != null) content.put(expediteeDataFormat, data.data);
|
---|
| 52 | if (data.imageRepresentation != null) {
|
---|
| 53 | javafx.scene.image.Image jfxImage = JavaFXMiscManager.getIfUsingJavaFXImageManager().getInternalImage(data.imageRepresentation);
|
---|
| 54 | content.putImage(jfxImage);
|
---|
| 55 | }
|
---|
| 56 | if (data.stringRepresentation != null) content.putString(data.stringRepresentation);
|
---|
| 57 |
|
---|
| 58 | // Set the clipboard contents
|
---|
| 59 | _clipboard.setContent(content);
|
---|
| 60 |
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | @Override
|
---|
| 64 | public ClipboardData get()
|
---|
| 65 | {
|
---|
| 66 | // Create an empty data structure for returning the gathered data
|
---|
| 67 | ClipboardData ret = new ClipboardData();
|
---|
| 68 |
|
---|
| 69 | // See which data formats are available for receipt
|
---|
| 70 | Set<DataFormat> availableFormats = _clipboard.getContentTypes();
|
---|
| 71 |
|
---|
| 72 | // Fill out the return structure
|
---|
| 73 | if (availableFormats.contains(expediteeDataFormat)) ret.data = _clipboard.getContent(expediteeDataFormat);
|
---|
| 74 | if (availableFormats.contains(DataFormat.IMAGE)) {
|
---|
| 75 | javafx.scene.image.Image jfxImage = _clipboard.getImage();
|
---|
| 76 | ret.imageRepresentation = JavaFXMiscManager.getIfUsingJavaFXImageManager().createImage(jfxImage);
|
---|
| 77 | }
|
---|
| 78 | if (availableFormats.contains(DataFormat.PLAIN_TEXT)) ret.stringRepresentation = _clipboard.getString();
|
---|
| 79 |
|
---|
| 80 | // Return it
|
---|
| 81 | return ret;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | }
|
---|